C# 将值从一种形式发送到另一种形式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1559770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:51:24  来源:igfitidea点击:

Send values from one form to another form

c#winformsmodal-dialog

提问by Nagu

I want to pass values between two Forms (c#). How can I do it?

我想在两个表单(c#)之间传递值。我该怎么做?

I have two forms: Form1 and Form2.

我有两种形式:Form1 和 Form2。

Form1 contains one button. When I click on that button, Form2 should open and Form1 should be in inactive mode (i.e not selectable).

Form1 包含一个按钮。当我单击该按钮时,Form2 应该打开,Form1 应该处于非活动模式(即不可选择)。

Form2 contains one text box and one submit button. When I type any message in Form2's text box and click the submit button, the Form2 should close and Form1 should highlight with the submitted value.

Form2 包含一个文本框和一个提交按钮。当我在 Form2 的文本框中键入任何消息并单击提交按钮时,Form2 应该关闭并且 Form1 应该突出显示提交的值。

How can i do it? Can somebody help me to do this with a simple example.

我该怎么做?有人可以用一个简单的例子帮助我做到这一点。

回答by Jesper Palm

There are several solutions to this but this is the pattern I tend to use.

对此有几种解决方案,但这是我倾向于使用的模式。

// Form 1
// inside the button click event
using(Form2 form2 = new Form2()) 
{
    if(form2.ShowDialog() == DialogResult.OK) 
    {
        someControlOnForm1.Text = form2.TheValue;
    }
}

And...

和...

// Inside Form2
// Create a public property to serve the value
public string TheValue 
{
    get { return someTextBoxOnForm2.Text; }
}

回答by Naruto

declare string in form1 public string TextBoxString;

在 form1 公共字符串 TextBoxString 中声明字符串;

in form1 click event add

在form1点击事件中添加

private void button1_Click(object sender, EventArgs e)
    {
        Form1 newform = new Form1();
        newform = this;
        this.Hide();
        MySecform = new Form2(ref newform);
        MySecform.Show();
    }

in form2 constructer

在 form2 构造函数中

public Form2(ref Form1 form1handel)
    {
        firstformRef = form1handel;
        InitializeComponent();
    }

in form2 crate variable Form1 firstformRef;

在 form2 crate 变量 Form1 firstformRef 中;

private void Submitt_Click(object sender, EventArgs e)
    {
        firstformRef.TextBoxString = textBox1.Text;
        this.Close();
        firstformRef.Show();

    }

回答by armannvg

I've worked on various winform projects and as the applications gets more complex (more dialogs and interactions between them) then i've started to use some eventing system to help me out, because management of opening and closing windows manually will be hard to maintain and develope further.

我从事过各种 winform 项目,随着应用程序变得越来越复杂(它们之间有更多的对话和交互),然后我开始使用一些事件系统来帮助我,因为手动打开和关闭窗口的管理将很难保持和进一步发展。

I've used CABfor my applications, it has an eventing systembut it might be an overkill in your case :) You could write your own eventsfor simpler applications

我已经将CAB用于我的应用程序,它有一个事件系统,但在您的情况下它可能是一种矫枉过正:) 您可以为更简单的应用程序编写自己的事件

回答by nadi

private void button1_Click(object sender, EventArgs e)
{
        Form2 frm2 = new Form2(textBox1.Text);
        frm2.Show();    
}


 public Form2(string qs)
    {
        InitializeComponent();
        textBox1.Text = qs;

    }

回答by OverflowReturns

Form1 Code :

表格 1 代码:

private void button1_Click(object sender, EventArgs e)
{
            Form2 f2 = new Form2();
            f2.ShowDialog();
            MessageBox.Show("Form1 Message :"+Form2.t.Text); //can put label also in form 1 to show the value got from form2
}

Form2 Code :

表格 2 代码:

        public Form2()
        {
            InitializeComponent();
            t = textBox1;                        //Initialize with static textbox
        }
        public static TextBox t=new TextBox();   //make static to get the same value as inserted
        private void button1_Click(object sender, EventArgs e)
        {

            this.Close();

        }

It Works!

有用!

回答by shweta

Define a property

定义属性

public static class ControlID {  
    public static string TextData { get; set; }
}

In the form2

在里面 form2

private void button1_Click(object sender, EventArgs e)
{  
    ControlID.TextData = txtTextData.Text;   
}

Getting the data in form1and form3

获取数据form1form3

private void button1_Click(object sender, EventArgs e)
{   
    string text=  ControlID.TextData;   
}

回答by vishu_heak

How to pass the values from form to another form

如何将值从表单传递到另一个表单

1.) Goto Form2 then Double click it. At the code type this.

1.) 转到 Form2 然后双击它。在代码中输入这个。

public Form2(string v)
{
  InitializeComponent();
  textBox1.Text = v;
}

2.) Goto Form1 then Double click it. At the code type this. //At your command button in Form1

2.) 转到 Form1 然后双击它。在代码中输入这个。//在Form1中的命令按钮上

private void button1_Click(object sender, EventArgs e)
{
   Form2 F2 = new Form2(textBox1.Text);
   F2.Show();
}

回答by Manuel

you can pass as parameter the textbox of the Form1, like this:

您可以将 Form1 的文本框作为参数传递,如下所示:

On Form 1 buttom handler:

在 Form 1 按钮处理程序上:

private void button2_Click(object sender, EventArgs e)
{
Form2 newWindow = new Form2(textBoxForReturnValue);
newWindow.Show(); 
}

On the Form 2

在表格 2 上

public static TextBox textBox2; // class atribute

public Form2(TextBox textBoxForReturnValue)
{
    textBox2= textBoxForReturnValue;
}

private void btnClose_Click(object sender, EventArgs e)
{

    textBox2.Text = dataGridView1.CurrentCell.Value.ToString().Trim();
    this.Close();
}

回答by Hassan Raza

Declare a public string in form1

声明一个公共字符串 form1

public string getdata;

In button of form1

在按钮 form1

form2 frm= new form2();
this.hide();
form2.show();

To send data to form1you can try any event and code following in that event

要向form1您发送数据,您可以尝试该事件中的任何事件和代码

form1 frm= new form1();
form1.getdata="some string to be sent to form1";

Now after closing of form2and opening of form1, you can use returned data in getdatastring.

现在在关闭form2和打开 之后form1,您可以使用getdata字符串中的返回数据。

回答by Sujith H S

After a series of struggle for passing the data from one form to another i finally found a stable answer. It works like charm.

经过一系列将数据从一种形式传递到另一种形式的斗争后,我终于找到了一个稳定的答案。它就像魅力一样。

All you need to do is declare a variable as public static datatype 'variableName'in one form and assign the value to this variable which you want to pass to another form and call this variable in another form using directly the form name (Don't create object of this form as static variables can be accessed directly) and access this variable value.

您需要做的就是public static datatype 'variableName'以一种形式声明一个变量,并将值分配给要传递给另一种形式的该变量,并直接使用表单名称在另一种形式中调用此变量(不要将这种形式的对象创建为可以直接访问静态变量)并访问此变量值。

Example of such is,

这样的例子是,

Form1

表格1

public static int quantity;
quantity=TextBox1.text; \Value which you want to pass

Form2

表格2

TextBox2.Text=Form1.quantity;\ Data will be placed in TextBox2