简体   繁体   English

将值从一种形式发送到另一种形式而不返回到形式 1

[英]Sending values from one form to another without returning to form 1

I've been trying to figure out how to have a user input a string into a text box in Form1 and then hit a "Done" button and display Form2 with a Label containing the text from the text box in Form1.我一直在试图弄清楚如何让用户在 Form1 中的文本框中输入一个字符串,然后点击“完成”按钮并显示 Form2 和一个包含来自 Form1 中文本框的文本的标签。

I had come across this question which almost solved my problem ( Send values from one form to another form ) but the answer to the above question showed you how to hit a button, display Form2, then input a string, and hit another button returning to form 1 where the label displayed the text inputted in Form2.我遇到了这个问题,它几乎解决了我的问题( 将值从一种形式发送到另一种形式),但上述问题的答案向您展示了如何点击按钮,显示 Form2,然后输入一个字符串,然后点击另一个按钮返回表单 1,其中标签显示在 Form2 中输入的文本。

I want to be able to input the string into a text box in Form 1 and when you hit a button display Form 2 with the label containing the string from the text box in Form 1.我希望能够将字符串输入到 Form 1 的文本框中,当您点击按钮时,将显示 Form 2,其中的标签包含来自 Form 1 的文本框中的字符串。

I've spent a good hour or so trying to deconstruct the tiny bit of code from the linked question and get it to work how I want, but I just can't seem to understand it.我花了一个小时左右的时间试图从链接的问题中解构一小段代码并让它按照我想要的方式工作,但我似乎无法理解它。 If theres anyone who could either explain how to get a label in Form2 to contain a string from a text box in Form1 or show me the code necessary so I may look over it and understand it, it'd be greatly appreciated.如果有人可以解释如何在 Form2 中获取标签以包含来自 Form1 中文本框的字符串,或者向我展示必要的代码以便我查看并理解它,我们将不胜感激。 As I currently cannot seem to get a grasp of how the code works in the previous question I linked.因为我目前似乎无法掌握我链接的上一个问题中的代码是如何工作的。

This is the bit of code I used from the above linked question这是我从上面链接的问题中使用的代码

// 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; }
}

In m understanding you want to get around with the data across forms?在我的理解中,您想处理跨表单的数据?

If you want to get the data and retrieve it forms you can try this如果你想获取数据并检索它的表格,你可以试试这个

Create a class name called Globals创建一个名为 Globals 的类名

public class Globals
{
private static string _myData;
public static string MyData
{
    get
    {
        // Reads are usually simple
        return _myData;
    }
    set
    {
        _myData = value;
    }
 }

}

in your Form1 set a value in a global variable在您的 Form1 中,在全局变量中设置一个值

Globals.myData = "someText"; //set the value

in your Form2 get the value在您的 Form2 中获取值

//example we have a lable
label1.Text = Globals.myData //this returns the value "someText"

You can try something like this你可以试试这样的

In Form2在 Form2 中

public void SetText(string text)
    {
        this.label1.Text = text;
    }

And In Form1在 Form1 中

private void Form1Button1_Click(object sender, EventArgs e)
    {
        using (Form2 form2 = new Form2())
        {
            form2.SetText(this.textBox1.Text);
            form2.ShowDialog();
        }
    }

If you don't want a modal dialog then change the Form1 code like below如果您不想要模态对话框,请更改如下 Form1 代码

Form2 form2 = new Form2();
    public Form1()
    {
        InitializeComponent();
    }
private void Form1Button1_Click(object sender, EventArgs e)
    {
        form2.SetText(this.textBox1.Text);
        form2.Show();
    }
  1. Create Form1 with a label ( label1 ) and a button ( button1 )创建带有标签 ( label1 ) 和按钮 ( button1 ) 的 Form1
  2. Create Form2 with a textbox ( textBox1 )使用文本框 ( textBox1 ) 创建 Form2

Form1.cs表格1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this.SetTextFromForm2, label1.Text);
            form2.ShowDialog();
        }

        private void SetTextFromForm2(string str)
        {
            label1.Text = str;
        }
    }
}

Explain :解释 :

Form2 form2 = new Form2(this.SetTextFromForm2, label1.Text);

Send 2 informations to Form2 : method to change Text of label on Form1, and current text of that label向 Form2 发送 2 个信息:更改 Form1 上标签文本的方法,以及该标签的当前文本

Form2.cs表格2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

public delegate void SetTextToForm1(string str);

namespace WindowsFormsApp1
{

    public partial class Form2 : Form
    {
        public SetTextToForm1 setTextToForm1 { get; set; }

        public Form2(SetTextToForm1 SetTextMethod, string strTextFromForm1)
        {
            InitializeComponent();

            this.setTextToForm1 = SetTextMethod;
            this.textBox1.Text = strTextFromForm1;
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            setTextToForm1(textBox1.Text);
        }
    }
}

Explain :解释 :

public delegate void SetTextToForm1(string str);

"A delegate is a reference type variable that holds the reference to a method" “委托是一个引用类型变量,它保存对方法的引用”

setTextToForm1(textBox1.Text);

Call Form1's method to change text of label on Form1调用 Form1 的方法来更改 Form1 上的标签文本


Answer your question :回答你的问题:

This bit of code did basically exactly what I'm looking for, but do you know if theres any way to essentially reverse it?这段代码基本上完全符合我的要求,但是您知道是否有任何方法可以从根本上扭转它? I see when you input text into the textBox in Form 2 it does put that into the label, so it works perfectly.我看到当您在 Form 2 中的 textBox 中输入文本时,它确实将其放入了标签中,因此它可以完美运行。 But is there a way to have the text box in Form 1 and when you hit the button just have Form 2 pop up with the label containing the text from the text box in Form 1?但是有没有办法让 Form 1 中的文本框出现,当你点击按钮时,Form 2 就会弹出,其中包含来自 Form 1 中文本框的文本的标签?

  1. Create Form1 with a textbox (textBox1) and a button (button1)创建带有文本框 (textBox1) 和按钮 (button1) 的 Form1
  2. Create Form2 with a label (label1)创建带有标签 (label1) 的 Form2

Form1.cs表格1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

public delegate void SetTextToForm2(string str);

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        Form2 form2 = new Form2();

        public SetTextToForm2 setTextToForm2 { get; set; }

        public Form1()
        {
            InitializeComponent();

            setTextToForm2 = form2.SetTextFromForm1;
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            setTextToForm2(textBox1.Text);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            form2.Visible = !form2.Visible;
        }
    }
}

Form2.cs表格2.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        public void SetTextFromForm1(string str)
        {
            label1.Text = str;
        }
    }
}

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

相关问题 将值从一个表单发送到另一个表单 - Send values from one form to another form 如何将值从一种形式传递到另一种形式,而无需在C#中创建形式的新实例? - how to pass a values from one form to another form without creating new instance of a form in c#? 将文本框字符串从一种形式发送到另一种形式的列表框 - Sending a textbox string from one form to a listbox in another form 从数据库将字符串从一种形式发送到另一种形式 - Sending String from Database from one Form to another 单击按钮将值从一个表单传递到另一个表单 - Passing Values from one Form to another Form in a Button click 将datagridview的检查值从一种形式转移到另一种形式datagridview - tranfer checked values of datagridview from one form to another form datagridview C# 将值从一个表单传递到另一个打开的表单 - C# Passing values from one form to another open form 一种形式到另一种形式,而不会丢失表格 1 c# 中先前输入的值的值 - One form to another without losing the values to previous entered values in form 1 c# 将自定义事件从一页发送到另一页 - Sending custom event form one page to another 在不重新打开表单的情况下从另一表单调用一种表单的事件/方法 - Calling event/methods of one form from another form without reopening form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM