Reputation: 31
I am doing a program that has multiple buttons and textboxes. The program I am creating now is not even 50% complete but the code lines is over 5000 making the program too big. Is there a way to combine these for loop statements:
For the first textbox when it is clicked.
var btn = new[] { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14 };
for (int i = 0; i < 14; i++)
{
if (txt1.Text == btn[i].Text)
{
txt1.Text = "";
btn[i].Visible = true;
break;
}
}
For the second textbox when it is clicked.
var btn = new[] { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14 };
for (int i = 0; i < 14; i++)
{
if (txt2.Text == btn[i].Text)
{
txt2.Text = "";
btn[i].Visible = true;
break;
}
}
and many more textbox.
Upvotes: 1
Views: 122
Reputation: 29006
Take a close look into the code that you are using, The thing which changing based on TextBox is the TextBox itself, so that you can accept it as an argument to the function and wrap the rest of statements inside the function. Then it will check the conditions and Change visibility of the button. which may look like the following:
var arrayButtons = new Button[] { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14 };
// Let the array be Global so that we can avoid defining the same every call
public void ChangeButtonVisibility(TextBox currentText)
{
for (int i = 0; i < arrayButtons.Length; i++)
{
if (currentText.Text == arrayButtons[i].Text)
{
currentText.Text = "";
arrayButtons[i].Visible = true;
break;
}
}
}
So that you can call the method like this when TextBox1 Clicked:
ChangeButtonVisibility(TextBox1);
Like this for TextBox2 Clicked
ChangeButtonVisibility(TextBox2);
Upvotes: 4
Reputation: 1000
i would suggest you avoid writing all the specific click events and make one click event for all of your textboxes.
in order to do that you have to add a pointer to the tag of the textbox,which points to corresponding button. i would do that in the load event of the form like this :
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Tag = btn1;
textBox2.Tag = btn2;
textBox3.Tag = btn3;
textBox4.Tag = btn4;
textBox5.Tag = btn5;
textBox6.Tag = btn6;
}
now that you have the buttons as tags of your controls, all you gotta do is make a click event for all your textboxes, cast sender, and check the control versus its tag :
private void allTextboxes_Click(object sender, EventArgs e)
{
if ((sender as TextBox).Text == ((sender as TextBox).Tag as Button).Text)
{
(sender as TextBox).Text = "";
((sender as TextBox).Tag as Button).Visible = true;
}
}
i would strongly prefer this kind of code over loops.
Upvotes: 0
Reputation: 22876
Or you can make another array with the text boxes and loop over both arrays:
var txts = new[] { txt1, txt2, txt3, txt4, txt5, txt6, txt7, txt8, txt9, txt10, txt11, txt12, txt13, txt14 };
var btns = new[] { btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14 };
foreach(var txt in txts)
{
foreach(var btn in btns)
{
if (txt.Text == btn.Text)
{
txt.Text = "";
btn.Visible = true;
break;
}
}
}
Upvotes: 0