Set value of Textbox

G

Guest

This is for a Winform.

What is the code to send text to a text box in another form? In my search
form when the user click OK, I want to sent a string to a text box in
frmDataEntry. The code I have below does not work.

private void cmdOk_Click(object sender, System.EventArgs e)
{
frmDataEntry f = new frmDataEntry();
f.txtDealerInfo.Text = "test1";

this.DialogResult = DialogResult.OK;
}
 
G

Guest

Hi Mike,
it is usually not a good idea to expose your textbox control directly on
the form and let other forms access it. What happens if you want to change
the name of your textbox, then other code wil break, it is best to
encapsulate it inside a call to a method that describes it purpose.

For example, suppose I have a form of type frmTest, which contains a textbox
called textBox1, I would add a public method to frmTest that allows the text
to be changed (I could also use a property if it is just a set/get operation,
like so:

//inside frmTest
public void SetTextBox(string latestText)
{
this.textBox1.Text = latestText;
}

now from my other form I would create an instance of frmTest and change the
textbox text like:

//inside frmMain
private void button1_Click(object sender, System.EventArgs e)
{
frmTest myTestForm = new frmTest();

myTestForm.SetTextBox("some new text");

myTestForm.Show();
}


Hope that helps point you in the right direction of what you are trying to do.

Mark R Dawson
 
G

Guest

Thanks, I think this will work.

I put your code in and stepped through with the debugger, and see the text
value change for the text box. But the text box does not show the change. I
think a refresh is need. I tried the following code but still the text box
does not show anything.

public void SetTextBox(string LatestText)
{
this.txtDealerInfo.Text = LatestText;
this.txtDealerInfo.Update();
this.txtDealerInfo.Refresh();
this.Update();
this.Refresh();


}
 
K

Kevin Yu [MSFT]

Hi Cadel,

I tried this on my machine, it works fine. Did you call f.Show or
f.ShowDialog to show this form after you have set the textbox text?

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

frmDataEntry calls frmDealerSearch. frmDataEntry has the text box.

When the user clicks OK on frmDealerSearch, I want the text box in
frmDataEntry to change.
To answer your question, I don't use f.Show or f.ShowDialog because the form
is already opened.
 
K

Kevin Yu [MSFT]

Hi Cadel,

It seems that you need to call the SetTextBox on already created instance
of frmDataEntry. It would be helpful if you could build a simple example
that can reproduce the problem and send it to me. Removing 'online' from
the no spam alias is my real email. Thank you!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

With you direction Kevin, it now works.

I added the code "this" in the form call.
frmDealerSearch f = new frmDealerSearch(this);

I also add the following code in frmDealerSearch

public frmDataEntry ResultForm;
public frmDealerSearch(frmDataEntry frmResult)

Now, any call to ResultForm will give me access to that instance.

Thanks for your help.
 
K

Kevin Yu [MSFT]

You're welcome, Cadel.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top