简体   繁体   English

如何从Form2更新Form1中的文本框?

[英]How to update textbox in form1 from form2?

I have 2 Windows Forms. 我有2个Windows窗体。

In first, the main window form, has multiline textbox and a button. 首先,主窗口窗体具有多行文本框和一个按钮。 The button opens the second form in which I can add data to array by using AddEntry object. 该按钮将打开第二个窗体,在该窗体中,我可以使用AddEntry对象将数据添加到数组。

In second form I have textboxes and a button (btnAddEntry) which should update the content of the textbox from first form. 在第二种形式中,我有文本框和一个按钮(btnAddEntry),应从第一种形式中更新文本框的内容。

When data is entered I want to display data in textbox from the first form. 输入数据后,我想从第一种形式的文本框中显示数据。

The problem is that code I came up with doesn't seem to work. 问题是我想出的代码似乎不起作用。

How would I solve this? 我该如何解决?

To get the BASICs working, do the following.. Create a new project. 要使BASIC工作,请执行以下操作。创建一个新项目。 Nothing of your current code, windows, etc... The default project will create a form "Form1" leave it alone for now. 您当前的代码,Windows等都没有任何内容。默认项目将创建一个表单“ Form1”,暂时不做任何处理。

Add a NEW form to the project, it will default to "Form2"... Put a single textbox on it and a single button on it. 向项目中添加一个新窗体,它将默认为“ Form2” ...在其上放一个文本框,在其上放一个按钮。 For grins, and clarification to differentiate between the object names, change the name of the controls on Form2 to "txtOnForm2", and "btnOnForm2" (case sensitive for my sample, and readability vs "txtonform2" all lower case). 为了咧嘴笑,并澄清区分对象名称,请将Form2上控件的名称更改为“ txtOnForm2”和“ btnOnForm2”(对我的示例区分大小写,而可读性与“ txtonform2”均为小写)。 Now, on the form, right-click and click "View Code". 现在,在表单上,​​右键单击并单击“查看代码”。 It will bring you to the other "Partial class" declaration where your constructors are found. 它将带您到找到构造函数的其他“局部类”声明。 Add the following, dont worry about compile errors as the other half will be when we put code into Form1 next... 添加以下内容,不要担心编译错误,因为另一半将在我们接下来将代码放入Form1中时...

// specifically typecasting the TYPE of form being passed in, 
// not just a generic form.  We need to see the exposed elements
Form1 CalledFrom;
// Ensure to do the : this() to make sure default behavior
// to initialize the controls on the form are created too.
public Form2(Form1 viaParameter) : this()
{
  CalledFrom = viaParameter;
}

private void btnOnForm2_Click(object sender, EventArgs e)
{
  CalledFrom.ValuesByProperty = this.txtOnForm2.Text;
  MessageBox.Show( "Check form 1 textbox" );

  string GettingBack = CalledFrom.ValuesByProperty;
  MessageBox.Show( GettingBack );

  CalledFrom.SetViaMethod( "forced value, not from textbox" );
  MessageBox.Show( "Check form 1 textbox" );

  GettingBack = CalledFrom.GetViaMethod();
  MessageBox.Show( GettingBack );
}

Save and close the Form2 designer and code window. 保存并关闭Form2设计器和代码窗口。

Now, open Form1. 现在,打开Form1。 Put a single textbox and a single button on it. 在其上放置一个文本框和一个按钮。 The default names for the controls will be "textbox1" and "button1" respectively. 控件的默认名称分别为“ textbox1”和“ button1”。 Leave it as is. 保持原样。 Double click on the button (on form1). 双击按钮(在form1上)。 It will bring up a snippet for code for that button. 它将为该按钮调出一段代码。 Paste the following 粘贴以下内容

private void button1_Click(object sender, EventArgs e)
{
   Form2 oFrm = new Form2(this);
   oFrm.Show();
}

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

public void SetViaMethod(string newValue)
{ this.textBox1.Text = newValue; }

public string GetViaMethod()
{ return this.textBox1.Text; }

Now, save the forms and run them. 现在,保存表单并运行它们。 Click button on first form, calls the second with the already created instance of itself, not a new SECOND instance of itself. 单击第一个窗体上的按钮,使用本身已创建的实例(而不是其自身的新SECOND实例)调用第二个窗体。 The second form will be displayed. 将显示第二个表单。 Shift the windows so you can see both. 移动窗口,以便您可以同时看到两个窗口。

Enter some text in the textbox second window and click the button... follow the back/forth of what is coming/going. 在文本框的第二个窗口中输入一些文本,然后单击按钮...遵循即将发生/正在发生的事情。

This has been answers other times, similar principles... 这是其他时间的答案,类似的原则...

Take a look at similar question 看看类似的问题

To "Pass-back" information from: Form2 to Form1, you will need to create a public method on it to expose what you want to touch. 要将信息从Form2传递回Form1,您将需要在其上创建一个公共方法来公开您想要触摸的内容。 Then from the second form (which basically has a pointer instance to the first, call that method with whatever you want to do / act upon. This could also be done with doing a public property to act upon. 然后从第二种形式(基本上有一个指向第一种形式的指针实例),用您想执行的任何操作来调用该方法。也可以通过执行一个公共属性来完成。

From what you may already have working... 从您可能已经在工作的...

public partial class YourFirstForm
{
    // example to expose a method on first form and pass IN a value
    public void SetMyObject( string ValueFromSecondForm )
    {
       this.txtBox1.Text = ValueFromSecondForm;
    }

    // example via a property you are trying to set... identical in results
    public string ViaSetAsProperty
    { set { this.txtBox1.Text = value; } }

    // Now, the reverse, lets expose some things from form 1 to the second...
    public string GetMyObjectText()
    {
       return this.txtBox1.Text;
    } 

    // or via a GETTER property... 
    public string GettingText
    { get { return this.txtBox1.Text; } }


    // However, if you want to allow both set and get to form 1's values, 
    // do as a single property with both getter / setter exposed..
    public string TextContent
    {  get { return this.txtBox1.Text; }
       set { this.txtBox1.text = value; }
    }
}

Now, for how to get from your SECOND form 现在,关于如何从第二张表格中获取

public partial class YourSecondForm { Form ObjRefToFirstForm; 公共局部类YourSecondForm {表格ObjRefToFirstForm;

public YourSecondForm( Form passedInVar )
{
   // preserve the first form
   ObjRefToFirstForm = passedInVar;
}

// Now, however you want to handle... via a button click, the text change event, etc
public void SendDataToForm1()
{
   // via calling the METHOD on form 1 that is public
   ObjRefToFirstForm.SetMyObj( this.SomeOtherTextbox.Text );

   // via a SETTER on form 1
   ObjRefToFirstForm.ViaSetAsProperty = this.SomeOtherTextbox.Text;


   // sample to GET something from form 1 via method
   string TestGet = ObjRefToFirstForm.GetMyObjectText();

   // or from the exposed property
   TestGet = ObjRefToFirstForm.GettingText;


   // Now, try via the one property that has both a setter and getter
   ObjRefToFirstForm.TextContent = this.SomeOtherTextbox.Text;
   TestGet = ObjRefToFirstForm.TextContent;
}

} }

Hopefully this exposes ways to both get and set content between forms for you... both as method() approach and/or get/set via properties. 希望这为您提供了在表单之间获取和设置内容的方法……既可以作为method()方法,也可以通过属性来获取/设置。

Your problem is that MainWindow mainWindow = new MainWindow() creates a new version of your MainWindow not a reference to the already existing version. 您的问题是MainWindow mainWindow = new MainWindow()创建MainWindow的新版本,而不是对现有版本的引用。 In your MainWindow form, when opening your 2nd form you need to pass a reference along to the 2nd form by passing it into the Show method (which stores it in a variable called owner of type object) like this: 在MainWindow窗体中,打开第二个窗体时,您需要通过将其传递到Show方法(将其存储在称为类型对象的所有者的变量中)中来传递第二个窗体的引用,如下所示:

AddEntryWindow addEntryWindow = new AddEntryWindow();
addEntryWindow.ShowDialog(this);

Then you can reference the textbox like this: 然后,您可以像这样引用文本框:

foreach (AddEntry list in addedEntry)
{
     // Displaying and formating the output in text box in MainWindow.         
     ((MainWindow)owner).txtDisplayFileContent.Text += txtUserName.Text;
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM