I had a confusion let me explain it in brief. I have a form i placed one textbox and a command button. and I have one plain class.
class1 code.
class Class1
{
string s = "hi";
Form1 form1 = new Form1();
public void cl()
{
form1.textBox1.Text = s;
}
}
form1 code
private void button1_Click(object sender, EventArgs e)
{
Class1 class1 = new Class1();
class1.cl();
}
what my program should do is when i click on the b开发者_StackOverflow社区utton the text in the textbox should become "hi". My error is when I click the button nothing happens.
In Class1
you are creating a new instance of Form1
instead of using the instance that already exists. In the button handler you can instead pass in a reference to the current form like this:
class Class1
{
string s = "hi";
Form1 form1;
public Class1( Form1 targetForm )
{
form1 = targetForm;
}
public void cl()
{
form1.textBox1.Text = s;
}
}
private void button1_Click(object sender, EventArgs e)
{
Class1 class1 = new Class1( this );
class1.cl();
}
You are creating a new Form1 instance, so you are changing the text on a form that's not being displayed, nor is the form where the button was actually clicked. You could do what you want by passing the form as a parameter to the method call:
class Class1
{
string s = "hi";
public void cl(Form1 form1)
{
form1.textBox1.Text = s;
}
}
private void button1_Click(object sender, EventArgs e)
{
Class1 class1 = new Class1();
class1.cl(this);
}
But it might be better form (depends on the actual case) to do the changes on form appearance only in the form code itself, using helper classes to generate the data only, for example:
class Class1
{
string s = "hi";
public string cl()
{
return s;
}
}
private void button1_Click(object sender, EventArgs e)
{
Class1 class1 = new Class1();
this.textBox1.Text = class1.cl();
}
You are creating a new Form1 object and setting the textbox value, and you are not setting the opened form's textbox value.
You probably should just call textbox1.Text = "x";
.
If in your c1 function you call Show()
method you will see a new form with the textbox filled.
You are missing a class1.ShowDialog()
method call at the end of your click handler.
Add the code for displaying the form. form1.ShowDialog() will display the form as a Modal dialog. Modal being you cannot do anything else to the originating form until this form has closed.
You're getting the behavior where nothing is happening because the form is not visible. Once you call ShowDialog() all code execution will be on that form and you won't be able to make calls to it until you close the form.
class Class1
{
string s = "hi";
Form1 form1 = new Form1();
public void cl()
{
form1.textBox1.Text = s;
form1.ShowDialog(); // Add this line to show the form.
}
}
why do you not catch your errors ?
class Class1
{
private Form1 _Instance = null;
public Form1 Instance
{
get{return _Instance;}
set{_Instance = value;}
}
public void cl()
{
if(Instance == null)
{
try
{
Instance = new Form1();
}catch (SomeException e)
{
//Log e.ToString();
return;
}
}
Instance.textBox1.Text = "hiya";
}
}
This way you your application will be less error prone and easier to debug.
精彩评论