【问题标题】:C# How can I save a value in a TextBox as a string?C#如何将文本框中的值保存为字符串?
【发布时间】:2020-08-19 23:35:00
【问题描述】:

我的 form3 中有 TextBox eingabe 现在我的计划是将 TextBox eingabe 保存为字符串,然后在我的 form4 上给出字符串 这怎么可能?我试过了:

//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)
    {

    }
}

这行不通。 请不要评判我,我对这一切都很陌生...

【问题讨论】:

  • 你使用哪个框架? WPF 还是 WinForms?
  • 还有label1是什么类型的?是Label 还是TextBlock
  • 查找.Text.Value 属性
  • @Ackdari 我使用 Windows 窗体框架。 label1是一个Label
  • 这能回答你的问题吗? C# get string from textbox

标签: c# winforms


【解决方案1】:

首先您必须获取文本 (eingabe.Text),然后在导航到新表单时将其发送到新表单

【讨论】:

  • 喜欢这个? //form3.cs string help = eingabe.Text //form4.cs Properties.Settings.Default.help= eingabe.Text;
【解决方案2】:

在 Form3 上编写如下代码:

inputtext= eingabe.text

使 Form4 构造函数参数化,并将 inputtext 值作为 Form4 对象中的参数传递给 Form4。

另一种方式: 在 Form4 上创建一个公共属性假设您在 Form4 上有 X 属性。在Form3上设置X值如下

objectForm4.X=eingabe.text

【讨论】:

  • 你需要.Text(大写T),因为C#区分大小写
【解决方案3】:

我找到了解决办法:

    public static string help;

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

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

在表格 4 中:

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

【讨论】:

  • 好的,现在它可以帮助你。但是你有没有意识到,如果没有form3的实例,那么form4中的代码就不会像你期望的那样工作。也许您应该考虑使用一些“全局”可用对象来保存这样的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
  • 2021-10-10
  • 2015-03-03
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多