Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What problem i am facing
C#
public class MyController
      {
          public void Thread_ContinuousChecker()
          {
              Form1 f = new Form1();
//here i want to access the form controls without using new ,
//any suggestion?
//bcz when i use it goes to his original position(at start time position)
//but i want to store the control setting here.

              f.Hide();
              if (f.checkBox3.Checked)
              {
                  Thread th = new Thread(f.files);
                  th.Start();
              }

}
}
}
Posted
Updated 17-Mar-15 2:38am
v2

Controls on a Form, in Windows Forms, are instantiated as private members of the Form; that is something you should not over-ride by, for example, setting the definition of a Control in the Designer.cs Class to 'Public.

So, you can only "get at" the instances of Controls in the instance of a Form by:

1. Your creating public properties that expose them in their hosting Form:
C#
public Form1()
{
    InitializeComponent();
    TheCheckBox = this.checkBox3;
}

// use this to access 'checkBox3 from "outside" the Form
public TheCheckBox { private set; get; }
2. Your drilling-down into the Controls Collection of the Form instance and getting a reference to them.
C#
CheckBox TheCheckBox = null;

var result = f.Controls.Find("checkBox3", searchAllChildren: true);

// note required cast here if search is successful ...
if (result.Length > 0) TheCheckBox = result[0] as CheckBox;
 
Share this answer
 
Use "static" member.
MSDN[^]
static Form1 f;
 
Share this answer
 
Comments
Aditya Chauhan 17-Mar-15 9:14am    
how this work when i click on the button

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900