How to change text in a textbox on another form in

2019-01-12 07:31发布

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();


    }

7条回答
2楼-- · 2019-01-12 07:54

I also had the same doubt, So I searched on internet and found a good way to pass variable values in between forms in C#, It is simple that I expected. It is nothing, but to assign a variable in the first Form and you can access that variable from any form. I have created a video tutorial on 'How to pass values to a form'

Go to the below link to see the Video Tutorial.

Passing Textbox Text to another form in Visual C#

查看更多
爷、活的狠高调
3楼-- · 2019-01-12 07:56

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

查看更多
贪生不怕死
4楼-- · 2019-01-12 08:03

Try this.. :)

Form1 f1 = (Form1)Application.OpenForms["Form1"];
TextBox tb = (TextBox)f1.Controls["TextBox1"];
tb.Text = "Value";
查看更多
混吃等死
5楼-- · 2019-01-12 08:04

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();
}
查看更多
三岁会撩人
6楼-- · 2019-01-12 08:05

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.

查看更多
欢心
7楼-- · 2019-01-12 08:12

In the designer code-behind file simply change the declaration of the text box from the default:

private System.Windows.Forms.TextBox textBox1;

to:

protected System.Windows.Forms.TextBox textBox1;

The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member (for more info, see this link).

查看更多
登录 后发表回答