【问题标题】:Passing Textbox Values one form to another form in Windows Forms在 Windows 窗体中将文本框值从一个窗体传递到另一个窗体
【发布时间】:2020-11-24 22:57:37
【问题描述】:

我正在尝试从用户那里获取用于绘制圆形(为此我使用半径输入)和矩形( Edge1 和 Edge2 )输入的输入值。我想在另一个表单上传递这些文本框值来绘制它们。 如何将这些值移动到属性部分中的另一种形式或任何解决方案的想法?

顺便说一下,表单在同一个项目中。

我定义了一个接口并实现了有关计算的流程。我的代码在下面并且有效,谢谢您的回复。

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assignment2
{
    class CircleShape : Shape, IShape
    {
        public int radius;
        public CircleShape(int radius)
        {
            this.radius = radius;


        }
        public double CalculateArea()
        {
            return Math.PI * radius * radius;
        }

        public double CalculatePerimeter()
        {
            return 2 * Math.PI * radius;
        }
    }
}

【问题讨论】:

  • 这是这里最常见的问题之一。已经有大量可用的答案。只需搜索该网站,您就会找到它们。
  • 更具体。给表格起个名字,告诉我们哪个是绘图的,哪个有输入的。还告诉我们哪种形式创建了另一种形式。信息是否需要实时?...还是仅在关闭输入表单后才进行绘图?
  • 这能回答你的问题吗? passing values between forms (winforms)

标签: c# winforms visual-studio-2019 windows-forms-designer


【解决方案1】:

一种方法是在您的第二个表单上使用一个方法来返回任何所需的详细信息

using (DrawForm drawForm = new DrawForm ())
{
    drawForm.ShowDialog(); //will wait for the form to close before continuing
    var userResults = drawForm.GetResults(); //returns whatever you need the form to return
    return userResults;
}

仅当第二种形式是某种弹出形式时,上述逻辑才有效。如果您希望两种形式都保持不变,那就有点不同了。您可能希望确保两个表单都可以相互访问。当您创建绘图时,请务必保存对它的引用。并在您的drawform的构造函数中添加一个参数以包含主表单。

  DrawForm drawForm = new DrawForm(this);//now the forms can talk to each other

当用户点击“开始”时,在您的绘图表单中

 mainForm.TriggerFinishedDrawing(drawingObject);

有几种方法可以实现这种行为,您只需要确定哪种流程在您的用例中最有意义

【讨论】:

    【解决方案2】:

    将数据从一个表单传递到另一个表单的一种简单方法是您可以使用Application.OpenForms Property

    首先,定义一个TextBox property 来访问Form1 中的TextBox 实例。

    // Form1.cs
    public TextBox TB
    {
        get { return textBox1; }
        set { textBox1 = value; }
    }
    

    然后调用Application.OpenForms获取Form1实例。

    // Form2.cs
    Form1 fr = (Form1)Application.OpenForms["Form1"];
    if (fr != null)
    {
        Form1 f1 = (Form1)fr;
        // get text form Form1.textbox1
        MessageBox.Show(f1.TB.Text);
    }
    

    如果通过点击Form1中的按钮打开Form2,并在Form2显示时传递值,可以通过constructor实现。

    // Form1.cs
    private void btnOpenForm2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(textBox1.Text);
        f2.Show();
    }
    
    // From2.cs
    string textformForm1;
    public Form2(string str)
    {
        InitializeComponent();
        textformForm1 = str;
    }
    
    private void btnShowText_Click(object sender, EventArgs e)
    {
        MessageBox.Show(textformForm1);
    }
    

    同样,您也可以使用公共属性。

    // Form1.cs
    private void btnOpenForm2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.TextformForm1 = textBox1.Text;
        f2.Show();
    }
    
    // Form2.cs
    public string TextformForm1 { get; set; }
    
    private void btnShowText_Click(object sender, EventArgs e)
    {
        MessageBox.Show(TextformForm1);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多