【问题标题】:Textbox not storing the information and others do if not null of empty文本框不存储信息,如果不为空,其他人会这样做
【发布时间】:2026-02-06 01:15:01
【问题描述】:

我试图让他们检查一个文本框是否为空,它不会在按下按钮时传递多个文本框的信息,例如:

 private void button1_Click(object sender, EventArgs e)
    {
        if (this.Controls.OfType<TextBox>().Any(t => string.IsNullOrWhiteSpace(t.Text)))
        {
             // what can i put here to exclude multiple
        }
        else
        {
            //if any of thouse are empty i dont want them to do this but if they are not empty i do want
            lablCinnamonset.Text = textBox1.Text; 
            lablMallardset.Text = textBox2.Text;
            lablAxisdeerSet.Text = textBox3.Text;
            lablBlackbuckSet.Text = textBox4.Text;
            lablMuledeerSet.Text = textBox5.Text;
            lablReddeerSet.Text = textBox6.Text;
            lablPumaSet.Text = textBox7.Text;
            lablWaterbuffaloSet.Text = textBox8.Text;
            lablJackrabbitSet.Text = textBox9.Text;
            lablCoyoteSet.Text = textBox10.Text;
            lablWhitetailSet.Text = textBox11.Text;
            lablBlacktailSet.Text = textBox12.Text;
            lablBlackbearSet.Text = textBox13.Text;
            lablRooseveltSet.Text = textBox14.Text;
            lablMooseSet.Text = textBox15.Text;
        }

我不想做,每个文本框的 if 语句,它必须是更好的方法。

谢谢大家

【问题讨论】:

  • 假设textBox5.Text。您要跳过lablMuledeerSet.Text = textBox5.Text; only 还是要跳过所有 个作业?
  • 我只想跳过那个框,就像你说的那样:如果textbox5.text 是空的,我想跳过lablMuledeerSet.Text= textbox5.Text; 谢谢你的快速回复

标签: c# if-statement textbox controls


【解决方案1】:

为什么不直接提取一个方法

  private static void AssignIfNotEmpty(Control target, Control source) {
    if (!string.IsNullOrWhiteSpace(source.Text))
      target.Text = source.Text;
  }

那就用吧

private void button1_Click(object sender, EventArgs e) {
  AssignIfNotEmpty(lablCinnamonset, textBox1); 
  AssignIfNotEmpty(lablMallardset, textBox2);
  AssignIfNotEmpty(lablAxisdeerSet, textBox3);
  AssignIfNotEmpty(lablBlackbuckSet, textBox4);
  AssignIfNotEmpty(lablMuledeerSet, textBox5);
  AssignIfNotEmpty(lablReddeerSet, textBox6);
  AssignIfNotEmpty(lablPumaSet, textBox7);
  AssignIfNotEmpty(lablWaterbuffaloSet, textBox8);
  AssignIfNotEmpty(lablJackrabbitSet, textBox9);
  AssignIfNotEmpty(lablCoyoteSet, textBox10);
  AssignIfNotEmpty(lablWhitetailSet, textBox11);
  AssignIfNotEmpty(lablBlacktailSet, textBox12);
  AssignIfNotEmpty(lablBlackbearSet, textBox13);
  AssignIfNotEmpty(lablRooseveltSet, textBox14);
  AssignIfNotEmpty(lablMooseSet, textBox15);
}

您可能想要组织您的控件,例如

public partial class MyForm : Form {
  private Dictionary<Label, TextBox> m_Correspondence = 
    new Dictionary<Label, TextBox>();

  public MyForm() {
    InitializeComponent();

    m_Correspondence.Add(lablCinnamonset, textBox1);
    m_Correspondence.Add(lablMallardset, textBox2);
    ...
    m_Correspondence.Add(lablMooseSet, textBox15);
  }

在这种情况下button1_Click 将非常简单:

private void button1_Click(object sender, EventArgs e) {
  foreach (var pair in m_Correspondence)
    AssignIfNotEmpty(pair.Key, pair.Value);
}

【讨论】:

  • 还有一个问题,我正在尝试创建字典,但我无法访问 M_Correspondence,所以我猜我将 Dictionary&lt;Label, TextBox&gt; m_Correspondence = new Dictionary&lt;Label, TextBox&gt;(); 放在错误的位置,你能给我一个方向吗?第一次使用 whit 词典。
  • @Sinesters:将m_Correspondence声明为private 字段,在InitializeComponent();之后的*constructor`中填写,请看我的编辑
  • 谢谢你,改变了我的编码方式,很高兴你花时间帮我一把。
【解决方案2】:

假设您可以使用“labl1”、“labl2”之类的内容更改各种“labl...”的名称或以某种方式映射它们(可能使用字典),那么您可以使用
查找名称函数。 示例:

for(int i=1; i<16; i++){
   if( (TextBox)this.FindName("textBox" + i.ToString()).Text != ""){
       //Here you can do what you want
   }
}

参考这里:https://docs.microsoft.com/it-it/dotnet/api/system.windows.frameworkelement.findname?view=netframework-4.8

【讨论】:

    最近更新 更多