Combining for loop - c#

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.

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

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

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.

Related

Need to link two button texts to eachother?

I have two table layout panels with 26 buttons each. I need to link the two button texts to each other. I have two Lists containing one language for one set of buttons and another language for another set of buttons. I tried to link the two lists together in order to set their translations but it didnt work. So for example if you click on one button the text "Hello" will show and another button "Salut". If its a match they must either disappear.
Random random = new Random();
List<string> EngBasicPhrases = new List<string>()
{
"Hello", "How are you?", "Hot", "Thank you", "Welcome",
"Let's go", "My name is...", "Cold", "Good luck",
"Congratulations", "Bless you","I forgot","Sorry","I'm fine",
"It's no problem","Don't worry","Here it is","What?","Of course",
"Boy","Girl","Man","Woman","Friend","Almost","Late"
};
List<string> FrBasicPhrases = new List<string>()
{
"Salut","Ca va?","Chaud", "Merci", "Bienvenu", "Allons-y","Je m'appelle","Du froid",
"Bonne chance","Felicitations","A vos souhaits","J'ai oublie","Desole","Je vais bien",
"Ce n'est pas grave","Ne t'en fais pas","Voila","Comment?","Bien sur","Un garcon","Une fille",
"Un home","Une femme","Un ami","Presque","En retard"
};
Button firstClicked, secondClicked;
public Game()
{
InitializeComponent();
AssignWordsToSquares();
EngBasicPhrases.AddRange(FrBasicPhrases);
}
private void Button_Click(object sender, EventArgs e)
{
if (firstClicked != null && secondClicked != null)
return;
Button clickedButton = sender as Button;
if (clickedButton == null)
return;
if (clickedButton.ForeColor == Color.Black)
return;
if(firstClicked == null)
{
firstClicked = clickedButton;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedButton;
secondClicked.ForeColor = Color.Black;
CheckForWinner1();
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
}
else
timer1.Start();
}
private void CheckForWinner1()
{
Button button1;
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
button1 = tableLayoutPanel1.Controls[i] as Button;
if (button1 != null && button1.ForeColor == button1.BackColor)
return;
}
MessageBox.Show("Congratulations!");
}
private void Button_Click2(object sender, EventArgs e)
{
if (firstClicked != null && secondClicked != null)
return;
Button clickedButton = sender as Button;
if (clickedButton == null)
return;
if (clickedButton.ForeColor == Color.Black)
return;
if (firstClicked == null)
{
firstClicked = clickedButton;
firstClicked.ForeColor = Color.Black;
return;
}
secondClicked = clickedButton;
secondClicked.ForeColor = Color.Black;
CheckForWinner2();
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
}
else
timer1.Start();
}
private void CheckForWinner2()
{
Button button2;
for (int i = 0; i < tableLayoutPanel2.Controls.Count; i++)
{
button2 = tableLayoutPanel2.Controls[i] as Button;
if (button2 != null && button2.ForeColor == button2.BackColor)
return;
}
MessageBox.Show("Congratulations!");
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
firstClicked.ForeColor = firstClicked.BackColor;
secondClicked.ForeColor = secondClicked.BackColor;
firstClicked = null;
secondClicked = null;
}
private void AssignWordsToSquares()
{
Button button1 = button2;
int randomNumber;
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
if (tableLayoutPanel1.Controls[i] is Button)
button1 = (Button)tableLayoutPanel1.Controls[i];
else
continue;
randomNumber = random.Next(0, EngBasicPhrases.Count);
button1.Text = EngBasicPhrases[randomNumber];
EngBasicPhrases.RemoveAt(randomNumber);
}
for (int i = 0; i < tableLayoutPanel2.Controls.Count; i++)
{
if (tableLayoutPanel2.Controls[i] is Button)
button2 = (Button)tableLayoutPanel2.Controls[i];
else
continue;
randomNumber = random.Next(0, FrBasicPhrases.Count);
button2.Text = FrBasicPhrases[randomNumber];
FrBasicPhrases.RemoveAt(randomNumber);
}
}
Ok, so you seem to have two lists to use for ordinal comparison. So, you don't really need to match the words, but the indexes of both lists. That makes sense. But, then it all gets confusing.
Your code does the following right now:
Create a list of English phrases
Create a list of French phrases
Start form
loop through English panel buttons
add a random English phrase to a button in control panel
remove phrase from English list
loop through French panel buttons
add a random French phrase to a button in control panel
remove phrase from French list
Add all remaining French phrases to the list of remaining English phrases (this step seems unnecessary either way)
So now you have your form loaded. If you have less buttons in your panels than you have words in your list, you might have ended up with words in either panel that cannot be matched.
Now you run into this:
private void Button_Click2(object sender, EventArgs e)
{
[...]
if (firstClicked.Text == secondClicked.Text)
{
firstClicked = null;
secondClicked = null;
}
[...]
this is the only place I see an attempt to compare the values of the button. But, by this point there is no way they'll ever match. "Hello" is a string that will never match "Salut" for example. And, since the lists are no longer intact, they can't be used to help match them. Other wise you could just find phrases in their respective lists and check if indexes match.
To allow this, you need to change how you add the button text in your code. Instead of using the list directly create an array from your list. Something like this:
string[] phrases= EngBasicPhrases.ToArray();
for (int i = 0; i < tableLayoutPanel1.Controls.Count; i++)
{
if (tableLayoutPanel1.Controls[i] is Button)
button1 = (Button)tableLayoutPanel1.Controls[i];
else
continue;
randomNumber = random.Next(0, phrases.Count - 1);
button1.Text = phrases[randomNumber];
phrases.RemoveAt(randomNumber);
}
phrases= FrBasicPhrases.ToArray();
for (int i = 0; i < tableLayoutPanel2.Controls.Count; i++)
{
if (tableLayoutPanel2.Controls[i] is Button)
button2 = (Button)tableLayoutPanel2.Controls[i];
else
continue;
randomNumber = random.Next(0, phrases.Count);
button2.Text = phrases[randomNumber];
phrases.RemoveAt(randomNumber);
}
There are some other problems in how you're working with the variables and attempting to use colors to determine state of the button, or infer meaning. But, I'll let you work through those. I recommend using breakpoints and stepping through the code to see the state of your variables and intercept events.
I'd use Dictionary. I would link values like this:
button1.Text == dict[button2.Text]

Turn textboxes and buttons visible when the number of tracks it's selected in a combobox

I'm trying to turn textboxes and buttons visible when the number of tracks it's selected in a combobox.
For example: when I select 3, just 3 textboxes and the 3 respective buttons to select the tracks are enabled. How can I change this code that I've made to a simple foreach or a for?
if (numero_faixas == 1) {
txtFaixa1.Visible = true;
btnFaixa1.Visible = true;
} else if (numero_faixas == 2) {
txtFaixa1.Visible = true;
btnFaixa1.Visible = true;
txtFaixa2.Visible = true;
btnFaixa2.Visible = true;
} else if (numero_faixas == 3) {
txtFaixa1.Visible = true;
btnFaixa1.Visible = true;
txtFaixa2.Visible = true;
btnFaixa2.Visible = true;
txtFaixa3.Visible = true;
btnFaixa3.Visible = true;
}
You can reduce the lines of code by changing your conditions, so you don't have to reference the same control so many times:
if (numero_faixas > 0)
{
txtFaixa1.Visible = true;
btnFaixa1.Visible = true;
}
if (numero_faixas > 1)
{
txtFaixa2.Visible = true;
btnFaixa2.Visible = true;
}
if (numero_faixas > 2)
{
txtFaixa3.Visible = true;
btnFaixa3.Visible = true;
}
To use a foreach loop, you could cast the Controls collection to an IEnumerable<Control> and then, using System.Linq;, you can filter on controls of type TextBox and Button, where the control name contains "Faxia". Then, in the loop body, we can use int.TryParse to try to convert the last character of the control name to an int, and if that succeeds, then set the control to Visible if the control number is less than numero_faixas + 1:
foreach (Control control in Controls.Cast<Control>()
.Where(c => (c is Button || c is TextBox) && c.Name.Contains("Faixa")))
{
// Get the number associated with this control and compare it to numero_faixas
int controlNumber;
if (int.TryParse(control.Name.Substring(control.Name.Length - 1), out controlNumber) &&
controlNumber < numero_faixas + 1)
{
control.Visible = true;
}
}
Here's a proof of concept that you could respin using your business rules.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ShowHideButtons_47439046
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitOurThings();
}
private void InitOurThings()
{
//lets create a combo box with options to select
ComboBox combo = new ComboBox();
combo.Location = new Point(5, 5);//place it somewhere
//add selectable items
for (int i = 0; i < 10; i++)
{
combo.Items.Add(i);
}
combo.SelectedValueChanged += Combo_SelectedValueChanged;//the event which will handle the showing/hidding
Controls.Add(combo);//add the combo box to the form
//lets create some buttons and textboxes
int btnx = 5;
int btny = combo.Height + combo.Location.Y + 5;
for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.Location = new Point(btnx, btny);
btn.Name = i.ToString();
btn.Text = i.ToString();
Controls.Add(btn);
btny += btn.Height + 5;
TextBox txtbx = new TextBox();
txtbx.Location = new Point(btn.Location.X + btn.Width + 5, btn.Location.Y);
txtbx.Name = i.ToString();
txtbx.Text = i.ToString();
Controls.Add(txtbx);
}
}
private void Combo_SelectedValueChanged(object sender, EventArgs e)
{
int selectedValue = int.Parse(((ComboBox)sender).SelectedItem.ToString());
foreach (Control item in Controls)
{
//show/hide the controls based on their Name being Equal Or Smaller than the selectedItem
if (item is TextBox)
{
int itemNumber = int.Parse(item.Name);
item.Visible = itemNumber <= selectedValue ? true : false;
}
if (item is Button)
{
int itemNumber = int.Parse(item.Name);
item.Visible = itemNumber <= selectedValue ? true : false;
}
}
}
}
}

Get control of Radio Button

I have radio buttons, around 200, whose ID are KP1, KP2, KP3... KP200. I would like to run a for loop to check if they are checked or not.
I get a crash at the line no 9:
RbId = CtrlId;
I would like to extract the Radio Button Ctrl from the string similar to how it's done using javascript ie.,
document.getElementById("<%=ID%>").
Please advise.
Code:
int i;
RadioButton RbId = null;
string CtrlId = null;
char[] KPList = new char[200];
for (i = 1; i <= 200; i++)
{
CtrlId = "KP"+i.ToString();
RbId = CtrlId;
if(RbId.Checked)
{
KPList[i] = (char)j;
break;
}
}
You can use FindControl() method in for it:
Control ctrl = this.FindControl(CtrlId);
if (ctl is RadioButton)
{
RadioButton rdBtn = ctrl as RadioButton;
// now do whatever here
if(rdBtn.Checked)
{
}
}
Try this:
foreach (RadioButton rdbtn in myDiv.Controls.OfType<RadioButton>())//Assume the RadioButtons are inside a div tag called myDiv
{
if(rdbtn.Checked)
{
....
}
}
It would be even better like this:
foreach (RadioButton rdbtn in myDiv.Controls.OfType<RadioButton>().Where(rdbtn => rdbtn.Checked))
{
}

How to Show AutoComplete Programmatically without entering a text

C# TextBox
AutoCompleteCustomSource has a List<string>,
AutoCompleteMode = Suggest.
I can see the List when I type a Letter.
How to show entire list without Typing a Letter Programmatically? This must be done while the User presses the Down Arrow Key in the TextBox.
Is there any Win32 API Available?
My Solution
I refined a Better Solution.
Add a ListBox Control to the form and make it as Visible = false
int curSelIndex = -1;
The below given Code will be executed Form_Load Event.
txtEmpId.AutoCompleteCustomSource.AddRange(EmpIds.ToArray());
lstAutoComplete.Items.Clear();
lstAutoComplete.Items.AddRange(EmpIds.ToArray());
txtEmpId.KeyDown += (ks, ke) =>
{
if (!(ke.KeyCode == Keys.Down ||
ke.KeyCode == Keys.Up ||
ke.KeyCode == Keys.Enter))
{
lstAutoComplete.Visible = false;
return;
}
ke.Handled = true;
if (ke.KeyCode == Keys.Enter)
{
if (lstAutoComplete.Visible)
{
var str = lstAutoComplete.SelectedItem + "";
// Process the Selected Item and set to TextBox.
}
}
if (!lstAutoComplete.Visible && txtEmpId.Focused)
{
var loc = txtEmpId.Location;
loc.Y += txtEmpId.Height;
lstAutoComplete.Location = loc;
lstAutoComplete.Size = txtEmpId.Size;
lstAutoComplete.Height = 100;
lstAutoComplete.SelectedIndex = 0;
curSelIndex = 0;
lstAutoComplete.Visible = true;
}
else if(lstAutoComplete.Visible && txtEmpId.Focused)
{
if (ke.KeyCode == Keys.Down)
{
curSelIndex++;
if (curSelIndex >= lstAutoComplete.Items.Count)
curSelIndex = lstAutoComplete.Items.Count - 1;
if (lstAutoComplete.Items.Count > 0)
lstAutoComplete.SelectedIndex = curSelIndex;
}
else if (ke.KeyCode == Keys.Up)
{
curSelIndex--;
if (curSelIndex < 0)
curSelIndex = 0;
if (lstAutoComplete.Items.Count > 0)
lstAutoComplete.SelectedIndex = curSelIndex;
}
}
};
txtEmpId.Leave += (ls, le) => lstAutoComplete.Visible = false;
I didn't find any API for your problem, so I just make a my own suggestion box by using ListBox to show when the Down Arrow Key is pressed, when you do other operation, it disappeares. I hope it is useful to you. code sample is bellow:
//string datasource
List<string> strList = null;
//suggestion listbox
ListBox sugBox = null;
public FrmTextSuggest()
{
InitializeComponent();
//setting the textbox control
strList = new List<string>()
{
"USA",
"England",
"China",
"Japan",
"Korea",
"India",
"France",
"Canada"
};
var autoCollection = new AutoCompleteStringCollection();
autoCollection.AddRange(strList.ToArray());
this.txtCountry.AutoCompleteCustomSource = autoCollection;
this.txtCountry.AutoCompleteMode = AutoCompleteMode.Suggest;
this.txtCountry.AutoCompleteSource = AutoCompleteSource.CustomSource;
//register the Down Arrow Key event
this.txtCountry.KeyDown += new KeyEventHandler(txtCountry_KeyDown);
}
void txtCountry_KeyDown(object sender, KeyEventArgs e)
{
//show the your own suggestion box when pressing down arrow and the text box is empty
if (e.KeyCode == Keys.Down && txtCountry.Text.Trim().Equals(""))
{
sugBox = new ListBox();
//define the box
sugBox.Width = txtCountry.Width;
Point p = txtCountry.Location;
p.Y += txtCountry.Height;
sugBox.Location = p;
sugBox.Items.AddRange(strList.ToArray());
//copy the value to the textbox when selected index changed.
sugBox.SelectedIndexChanged += new EventHandler(sugBox_SelectedIndexChanged);
//show box
if (sugBox.Items.Count > 0)
{
sugBox.SelectedIndex = 0;
this.Controls.Add(sugBox);
sugBox.Focus();
}
}
//remove and hide your own suggestion box when other operation
else
{
if (sugBox != null && this.Controls.Contains(sugBox))
{
this.Controls.Remove(sugBox);
sugBox.Dispose();
sugBox = null;
}
}
}
void sugBox_SelectedIndexChanged(object sender, EventArgs e)
{
string selText = this.sugBox.SelectedItem.ToString();
if (!string.IsNullOrEmpty(selText))
{
this.txtCountry.Text = selText;
}
}
here is my result of test:

Referencing buttons with numbers

Is there a way to reference buttons using a numerical value in C#? I am trying to manipulate the text on buttons using one reusable method. Here is my current coding:
One button click method (there are a total of 16):
private void Card1_Click(object sender, EventArgs e)
{
buff = CardClick(1);
if (buff != null)
{
Card1.Text = buff;
}
}
And the reusable method (the code does have holes, it's in development):
private string CardClick(int card)
{
guesses[g++] = card; //alternate g
if ((guesses[0] != null) && (guesses[1] != null))
{
//Reset Card guesses[0]
//Reset Card guesses[1]
return null;
}
else
{
if (card > 8)
{
return map[2, card];
}
else
{
return map[1, card];
}
}
You can also use Controls.Find() to get a reference to your desired button based on its name:
int i = 1;
Control[] matches = this.Controls.Find("Card" + i.ToString(), true);
if (matches.Length > 0 && matches[0] is Button)
{
Button btn = (Button)matches[0];
// ... do something with "btn" ...
btn.PerformClick();
}
You can use an array of buttons
Button[] buttonArray = new Button[10];
You can get all the buttons from your form by Type and then extract an array:
public Button[] AllButtons()
{
var buttons = new List<Button>();
foreach (var control in this.Controls)
{
if (control.GetType() == typeof(Button))
buttons.Add((Button)control);
}
return buttons.ToArray();
}

Categories

Resources