I'm trying to make that theirs a check if a textbox is empty, it wont pass the information on multiple textboxes on a button press, example:
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;
}
I don't want to do, a if statement for each textbox, it has to be a better way.
Thank you guys
Why don't just extract a method?
private static void AssignIfNotEmpty(Control target, Control source) {
if (!string.IsNullOrWhiteSpace(source.Text))
target.Text = source.Text;
}
Then use it
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);
}
You may want to organize your controls e.g.
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);
}
In this case button1_Click
will be very simple:
private void button1_Click(object sender, EventArgs e) {
foreach (var pair in m_Correspondence)
AssignIfNotEmpty(pair.Key, pair.Value);
}