【问题标题】:Passing Values from one Form to another Form in a Button click在按钮单击中将值从一个表单传递到另一个表单
【发布时间】:2016-12-25 21:00:50
【问题描述】:

这是我的 2 个表格。

这些是表格1的代码-->

namespace Passing_Values
{
    public partial class Form1 : Form
    {
        string a="preset value";
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpenF2_Click(object sender, EventArgs e)
        {
            new Form2().Show();
        }

        public void set(string p)
        {
            MessageBox.Show("This is Entered text in Form 2 " + p);
            a = p;
            MessageBox.Show("a=p done! and P is: " + p + "---and a is: " + a);
            textBox1.Text = "Test 1";
            textBox2.Text = a;
            textBox3.Text = p;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show(a);
        }
    }
}

这些是表格2的代码-->

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

        private void button1_Click(object sender, EventArgs e)
        {
            string g;
            g = textBox1.Text;

            Form1 j = new Form1();
            j.set(g);
        }
    }
}

看图。你可以理解设计。

这就是我想做的。第一次我使用 Form1 中的按钮打开 Form2。然后我输入一个文本并单击按钮(“在 Form1 文本框中显示”)。单击该值时,应在 Form1 的 3 个文本框中看到该值。我使用消息框查看值是否通过。值从 Form2 传递到 Form1。但是这些值不会显示在这 3 个文本框中,但传递的值会显示在消息框中。通过查看代码可以理解 3 个文本框的原因。那么错误是什么?

【问题讨论】:

  • 您应该在 Form1 中创建要在关闭 Form2 时保留的变量,有几种方法可以做到这一点..但我不会采用 new Form2().Show(); 的方法。我会创建一个 var frm2 = new Form2(),然后在关闭 form2 时传递和/或创建变量来保存值,然后当您显示表单 2 时,我会调用 ShowDialog()` 方法,以便在关闭时返回到调用方法.. 然后我会调用 frm2.Dispose()` 希望这是有道理的
  • 对不起,我是 VS 或 C# 的新手,最好提供代码。所以我能理解!谢谢! @MethodMan
  • 你最好在谷歌上搜索一下如何做到这一点..这不是Code Factory Site,这并不难..所以不,我不会为你做这项工作..对不起这不是这个网站的工作方式。我给你解释了如何做到这一点..
  • @MethodMan 是对的。通过一些 Google 搜索,您会发现很多示例。例如codeproject.com/Articles/14122/Passing-Data-Between-Forms(10 秒找到,第一行)
  • 我用谷歌搜索。但是没有得到正确的答案。这就是我来这里的原因:/ BTW tnx 链接@DDD Soft

标签: c# winforms parameter-passing


【解决方案1】:

在你的Form2中放置一个static字符串

public static string s = string.Empty;

并且,在您的 Display in Form1 Textbox 按钮单击事件中,从字符串 s 中的文本框中获取值:

s = textBox1.Text;
Form1 f1 = new Form1();
f1.Show();

一次,Form1 再次出现,然后在Form1_Load 事件中,只需将您的Form2 的文本值传递给您的Form1 的文本框,其值由变量获取s:

foreach (Control text in Controls)
{
    if (text is TextBox)
    {
        ((TextBox)text).Text = Form2.s;
    }
}

【讨论】:

  • 你能foreach(Textbox text in Controls) 吗?
  • @khargoosh 你不能因为你需要将 base class 定义为 Control 并且 Textbox 不是一个基类这里是所有控件:)
【解决方案2】:

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

    private void button1_Click(object sender, EventArgs e)
    {
        fm2 = new Form2();
        fm2.Show();
        fm2.button1.Click += new EventHandler(fm2button1_Click);
    }
    private void fm2button1_Click(object sender, EventArgs e)
    {
        textBox1.Text = fm2.textBox1.Text;
    }


}

form2中的代码

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

textbox1button1的修饰符属性设置为public

【讨论】:

  • 即使我将按钮修饰符属性设置为公共,我仍然会收到此错误'Form' does not contain a definition for 'searchButton' and no accessible extension method 'searchButton' accepting a first argument of type 'Form' could be found (are you missing a using directive or an assembly reference?) 为什么?
【解决方案3】:

我会简单地将它传递给构造函数。 因此,form2 的代码将是:

public partial class Form2 : Form
{
    string _input;

    public Form2()
    {
        InitializeComponent();
    }

    public Form2(string input)
    {
        _input = input;
        InitializeComponent();

        this.label1.Text = _input;
    }
}

Form1 中的调用将是:

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

【讨论】:

    【解决方案4】:

    其实我有一个对象要传递。所以我这样做了

    表格1-->

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

    在form2-->

    public partial class Form2 : Form
        {
            Form1 a;
            public Form2(Form1 b)
            {
                a = b;
                InitializeComponent();
    
            }
            private void button1_Click(object sender, EventArgs e)
            {
                string g;
                g = textBox1.Text;
    
    
                a.set(g);
                this.Close();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      相关资源
      最近更新 更多