How to change text in a textbox on another form in Visual C#?

48,185

Solution 1

When you create the new form in the button click event handler, you instantiate a new form object and then call its show method.

Once you have the form object you can also call any other methods or properties that are present on that class, including a property that sets the value of the textbox.

So, the code below adds a property to the Form2 class that sets your textbox (where textbox1 is the name of your textbox). I prefer this method method over making the TextBox itself visible by modifying its access modifier because it gives you better encapsulation, ensuring you have control over how the textbox is used.

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    public string TextBoxValue
    {
        get { return textBox1.Text;} 
        set { textBox1.Text = value;}
    }                       
}

And in the button click event of the first form you can just have code like:

private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.TextBoxValue = "SomeValue";
    form2.Show();
}

Solution 2

You can set "Modifiers" property of TextBox control to "Public"

Solution 3

Try this.. :)

Form1 f1 = (Form1)Application.OpenForms["Form1"];
TextBox tb = (TextBox)f1.Controls["TextBox1"];
tb.Text = "Value";

Solution 4

I know this was long time ago, but seems to be a pretty popular subject with many duplicate questions. Now I had a similar situation where I had a class that was called from other classes with many separate threads and I had to update one specific form from all these other threads. So creating a delegate event handler was the answer.

The solution that worked for me:

  1. I created an event in the class I wanted to do the update on another form. (First of course I instantiated the form (called SubAsstToolTipWindow) in the class.)

  2. Then I used this event (ToolTipShow) to create an event handler on the form I wanted to update the label on. Worked like a charm.

I used this description to devise my own code below in the class that does the update:

public static class SubAsstToolTip
{
    private static SubAsstToolTipWindow ttip = new SubAsstToolTipWindow();
    public delegate void ToolTipShowEventHandler();
    public static event ToolTipShowEventHandler ToolTipShow;

    public static void Show()
    {
        // This is a static boolean that I set here but is accessible from the form.
        Vars.MyToolTipIsOn = true; 
        if (ToolTipShow != null)
        {
            ToolTipShow();
        }
    }

    public static void Hide()
    {
        // This is a static boolean that I set here but is accessible from the form.
        Vars.MyToolTipIsOn = false;
        if (ToolTipShow != null)
        {
            ToolTipShow();
        }
    }
}

Then the code in my form that was updated:

public partial class SubAsstToolTipWindow : Form
{
    public SubAsstToolTipWindow()
    {
        InitializeComponent();
        // Right after initializing create the event handler that 
        // traps the event in the class
        SubAsstToolTip.ToolTipShow += SubAsstToolTip_ToolTipShow;
    }

    private void SubAsstToolTip_ToolTipShow()
    {
        if (Vars.MyToolTipIsOn) // This boolean is a static one that I set in the other class.
        {
            // Call other private method on the form or do whatever
            ShowToolTip(Vars.MyToolTipText, Vars.MyToolTipX, Vars.MyToolTipY);     
        }
        else
        {
            HideToolTip();
        }

    }

I hope this helps many of you still running into the same situation.

Share:
48,185

Related videos on Youtube

neuromancer
Author by

neuromancer

Updated on August 11, 2020

Comments

  • neuromancer
    neuromancer almost 4 years

    In Visual C# when I click a button, I want to load another form. But before that form loads, I want to fill the textboxes with some text. I tried to put some commands to do this before showing the form, but I get an error saying the textbox is inaccessible due to its protection level.

    How can I set the textbox in a form before I show it?

     private void button2_Click(object sender, EventArgs e)
        {
    
            fixgame changeCards = new fixgame();
            changeCards.p1c1val.text = "3";
            changeCards.Show();
    
    
        }
    
  • Andrew Keith
    Andrew Keith over 14 years
    +1 even though its possible to change the modifiers on the textbox to public, its a better design to set the text using a property so that the OOP principles of information hiding is not broken. (in this case, hiding the textbox control from being modified outside the object).
  • Dirk
    Dirk over 10 years
    Just like your other answer: links are no answers in most cases, especially links to a video.