简体   繁体   English

C# 如何将文本框中的值保存为字符串?

[英]C# How can I save a value in a TextBox as a string?

I have the TextBox eingabe in my form3 Now my plan is to save the TextBox eingabe as a String and than give out the String on my form4 How is this possible?我的form4中有 TextBox eingabe现在我的计划是将 TextBox eingabe保存为字符串,然后在我的form3上给出字符串 这怎么可能? I tried:我试过了:

//form3.cs    
    public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
    }

    private void eingabe_TextChanged(object sender, EventArgs e)
    {

    }

    private void openWindow(object sender, EventArgs e)
    {
        this.Hide();
        Form4 form4 = new Form4();
        form4.ShowDialog();
        String help = eingabe.Text;
    }
}

//form4.cs
    public partial class Form4 : Form
{
    public Form4()
    {
        InitializeComponent();
    }

    private void Form4_Load(object sender, EventArgs e)
    {
        ausgabe = help;
    }

    private void ausgabe_Click(object sender, EventArgs e)
    {

    }
}

That doesn't work.那是行不通的。 Please don't judge me I am new to all this...请不要评判我,我对这一切都很陌生...

Write code like below on Form3:在 Form3 上编写如下代码:

inputtext= eingabe.text

Make Form4 constructor parametrized and pass inputtext value to Form4 as argument in Form4 object.使 Form4 构造函数参数化,并将 inputtext 值作为 Form4 object 中的参数传递给 Form4。

Another way: Create a public property on Form4 lets say you have X property on Form4.另一种方式:在 Form4 上创建一个公共属性假设您在 Form4 上有 X 属性。 set X value as below on Form3在 Form3 上设置 X 值如下

objectForm4.X=eingabe.text

First you have to get the text (eingabe.Text) then send it to new form when navigate to new form首先,您必须获取文本(eingabe.Text),然后在导航到新表单时将其发送到新表单

I found the solution:我找到了解决方案:

    public static string help;

    private void openWindow(object sender, EventArgs e)
    {
        help = eingabe.Text;

        this.Hide();
        Form4 form4 = new Form4();
        form4.ShowDialog();
    }

And in Form 4:在表格 4 中:

    private void Form4_Load(object sender, EventArgs e)
    {
        ausgabe.Text = Form3.help;
    }

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

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