文本框不存储信息,如果不为空,则其他文本框会存储信息

Textbox not storing the information and others do if not null of empty

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

 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 语句,它必须是更好的方法。

谢谢大家

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

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

为什么不直接提取方法

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