I'm using this right now to clear a tic tac toe table on Microsoft Windows Forms
button1.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = true;
button7.Enabled = true;
button8.Enabled = true;
button9.Enabled = true;
button1.Text = "";
button2.Text = "";
button3.Text = "";
button4.Text = "";
button5.Text = "";
button6.Text = "";
button7.Text = "";
button8.Text = "";
button9.Text = "";
but it's too long so I wanted to use a for loop like this
for (int i = 1; i < 10; i++)
{
button[i].Enable = true;
button[i].Text = "";
}
but it doesn't work so could anyone suggest a solution?
you can iterate over Controls array of parent of buttons, for example you can move all buttons to a Panel and use foreach to iterate over all controls inside it:
foreach(var control in Panel1.Controls) {
if (control is Button button)
{
button.Text = "";
button.Enabled = true;
}
}
I see what you are thinking, but you cannot dynamically change the name of the variable you are trying to edit in a for loop like that.
In a for loop, it will iterate through a starting value until it reaches a stopping condition. for loops are great for when you have a data structure like a List or an Array where you can enumerate through to get or set values.
The code you have for the for loop is starting to enumerate through either an Array or List that has a name of button. To fix this, I recommend to add the buttons to an Array and then you can use a foreach loop to set the values:
foreach (Button button in buttons){
button.Enabled = true;
button.Text = "";
}
Related
i`m trying to find a efficient way to define conditions for radio-buttons.
After selecting a Radio-Button i should disable or fill the text-boxes. Following an example:
private void Transportauftrag_CheckedChanged(object sender, EventArgs e)
{
groupBox1.Visible = true;
groupBox8.Visible = false;
StartGerät.Text = "";
StartBlockFach.Text = "";
StartTiefe.Text = "";
ZielGerät.Text = "";
ZielBlockFach.Text = "";
ZielTiefe.Text = "";
FehlerFehler.Text = "****";
FehlerBereich.Text = "****";
FehlerInfo.Text = "****";
Transportbefehl.Text = "";
Breite.Text = "";
Tiefe.Text = "";
Höhe.Text = "";
Typ.Text = "";
Folgeauftrag.Text = "";
Behälternummer.Text = "";
Reserve.Text = "";
StartGerät.Enabled = true;
StartBlockFach.Enabled = true;
StartTiefe.Enabled = true;
ZielGerät.Enabled = true;
ZielBlockFach.Enabled = true;
ZielTiefe.Enabled = true;
FehlerFehler.Enabled = false;
FehlerBereich.Enabled = false;
FehlerInfo.Enabled = false;
Transportbefehl.Enabled = true;
Breite.Enabled = true;
Tiefe.Enabled = true;
Höhe.Enabled = true;
Typ.Enabled = true;
Folgeauftrag.Enabled = true;
Behälternummer.Enabled = true;
Reserve.Enabled = true;
}
So now i should do this 7 times.
And for this i need over 400 Line of Codes in my WindowsFormsApp-File.
Are they any better way / solution to do this?
ok here are some suggestions. Basically if faced with such a problem then it indicates that you need an organizational structure. Otherwise one gets lost. As it appears from the picture of your GUI you already have GroupBoxes that organize this. All the labels and Textboxes inside that groupbox are in groupBox1.Controls. You can get them out in 1 blow even by type:
List<TextBox> allTextBoxesFromGroupBox1 = groupBox1.Controls.OfType<TextBox>().ToList();
Now you can iterate over it and write the same value for each Textbox:
foreach (TextBox tb in allTextBoxesFromGroupBox1)
{
tb.Text = "";
}
But you better make a method from it:
private void FillAllTextBoxes(GroupBox groupBox, string text)
{
List<TextBox> allTextBoxesFromGroupBox1 = groupBox.Controls.OfType<TextBox>().ToList();
foreach (TextBox tb in allTextBoxesFromGroupBox1)
{
tb.Text = "";
}
}
Now you can simply call it and pass the necessary input:
FillAllTextBoxes(groupBoxStart, "");
FillAllTextBoxes(groupBoxZiel, "");
FillAllTextBoxes(groupBoxFehler, "****");
FillAllTextBoxes(groupBoxParameter, "");
At this point we got rid of 17 Lines and introduced only 4
Proceeding... The next step would be the enabling of the controls. It seems that you treat again all Textboxes inside the same groupbox also the same. So we can introduce another parameter for the enabling part into the method:
private void FillAllTextBoxes(GroupBox groupBox, string text, bool enable)
{
List<TextBox> allTextBoxesFromGroupBox1 = groupBox.Controls.OfType<TextBox>().ToList();
foreach (TextBox tb in allTextBoxesFromGroupBox1)
{
tb.Text = "";
tb.Enabled = enable;
}
}
and this allows us to remove the rest of the lines entirely without introducing new lines of code:
FillAllTextBoxes(groupBoxStart, "", true);
FillAllTextBoxes(groupBoxZiel, "", true);
FillAllTextBoxes(groupBoxFehler, "****", false);
FillAllTextBoxes(groupBoxParameter, "", true);
So now you are down at 4 lines (I see only 4 groupboxes in your screenshot) of code
i having a little advance with Command Button
lets say i having a form with
label1
label2
label3
And one command button of course
before click the command button
label1.Visible = true;
label2.Visible = false;
label3.Visible = false;
if i click the command button
label1.Visible = false;
label2.Visible = true;
label3.Visible = false;
and then i click again
label1.Visible = false;
label2.Visible = false;
label3.Visible = true;
and repeated again, then back to first before i click the command button
i didnt have any reference on this, so i just make a duplicated command button with same position in form, and i use the visible to get the another command button to be clicked
but it looks not cool,
is there any way better than i make?
you can make one button, and count the number of clicks the user has done, depending on the mod operation you can invoke any of the three method you provide, something like this:
int count = 1; //count clicks
private void ButtonClick(object sender, EventArgs e)
{
if(count%2 == 0)
{
label1.Visible = false;
label2.Visible = true;
label3.Visible = false;
}
else if(count%3 == 0){
label1.Visible = false;
label2.Visible = false;
label3.Visible = true;
}
else{
label1.Visible = true;
label2.Visible = false;
label3.Visible = false;
}
count++;
}
Something similar to this will allow you to only have one command button.
public int visibleLabel = 1;
private void commandButton_Click(object sender, EventArgs e)
{
this.visibleLabel += 1;
if (this.visibleLabel == 4) this.visibleLabel = 1
label1.visble = false;
label2.visble = false;
label3.visble = false;
switch (this.visibleLabel)
{
case 1:
label1.visble = true;
break;
case 2:
label2.visble = true;
break;
case 3:
label3.visble = true;
break;
}
}
Alternatively, you might want to consider having a single label and changing the text (assuming they are in the same locations as well)
Why dont you use a flag to track the current status of the command button? If I understand your question right you want a single command button that sets label2 to visible at first click and label3 visible at second click and label1 visible on third click. Do something like this in the command button click handler:
switch (status)
{
case 0:
{
label1.Visible = false;
label2.Visible = true;
label3.Visible = false;
status = 1;
break;
}
case 1:
{
label1.Visible = false;
label2.Visible = false;
label3.Visible = true;
status = 2;
break;
}
case 2:
{
label1.Visible = true;
label2.Visible = false;
label3.Visible = false;
status = 0;
break;
}
}
Some of the code is redundant, and of course you should have a integer variable called status somewhere in your class. Anyway, this should be fairly understandable and I hope it helps.
I've just started programming meaning I'm new... I've made a tic tac toe game using else if statements.
The code below is code that will reset certain values within my game, I'm sure there's but I can't find an answer - How do I shorten this block of code?
btn1.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
btn4.Enabled = true;
btn5.Enabled = true;
btn6.Enabled = true;
btn7.Enabled = true;
btn8.Enabled = true;
btn9.Enabled = true;
btn1.Text = "";
btn2.Text = "";
btn3.Text = "";
btn4.Text = "";
btn5.Text = "";
btn6.Text = "";
btn7.Text = "";
btn8.Text = "";
btn9.Text = "";
btn1.BackColor = default(Color);
btn2.BackColor = default(Color);
btn3.BackColor = default(Color);
btn4.BackColor = default(Color);
btn5.BackColor = default(Color);
btn6.BackColor = default(Color);
btn7.BackColor = default(Color);
btn8.BackColor = default(Color);
btn9.BackColor = default(Color);
You should use an array, or better yet, a generic list which will hold all your buttons.
For example:
List<Button> buttonList = new List<Button>();
// Add buttons to list...
buttonList.Add(btn1);
buttonList.Add(btn2);
...
Then, you can iterate on the list like this:
foreach (Button btn in buttonList)
{
btn.Text = "";
btn.BackColor = default(Color);
}
The code above will run on all the buttons in the list and for each of them clear the text and set the BackColor.
This should be done with arrays, instead of btn1, you would have btn[], and then you can access them with a for loop:
for (i=0; i<9; i++) {
btn[i].Enabled = true;
btn[i].Text = "";
btn[i].Color = default(Color);
}
and these 5 lines of code contain all the code you have above.
reset for all button at control
foreach(var item in this.Controls)
{
if (item is Button)
{
item.Enabled = true;
item.Text = "";
item.BackColor = default(Color);
}
}
I have my code written like so:
private void radioSelectButton_Click(object sender, EventArgs e)
{
if (pteriRadio.Checked) // Selecting avatar to be displayed from here on out.
//This avatar will also be displayed on the game board.
{
pteriBox1.Visible = true;
xweetokBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Pteri Inventory");
player1avatar = "pteri";
}
else if (xweetokRadio.Checked)
{
xweetokBox1.Visible = true;
pteriBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Xweetok Inventory");
player1avatar = "xweetok";
}
else if (ixiRadio.Checked)
{
ixiBox1.Visible = true;
pteriBox1.Visible = false;
xweetokBox1.Visible = false;
label1.Text = ("Ixi Inventory");
player1avatar = "ixi";
}
characterSelectBox.Visible = false;
radioSelectButton.Visible = false;
characterSelectBox2.Visible = true;
radioSelectButton2.Visible = true;
}
It seems as though the visibility changes should display when I have it like this, with the changes within the button click but outside of the if statements (It doesn't matter what the user selects, once selected the option to select needs to disappear for that user.)Yet the visibility changes don't execute. What am I missing here?
If I nest the if-statements as suggested by a previous person, here's what I have:
private void radioSelectButton_Click(object sender, EventArgs e)
{
if (pteriRadio.Checked) // Selecting avatar to be displayed from here on out.
//This avatar will also be displayed on the game board.
{
pteriBox1.Visible = true;
xweetokBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Pteri Inventory");
player1avatar = "pteri";
if (xweetokRadio.Checked)
{
xweetokBox1.Visible = true;
pteriBox1.Visible = false;
ixiBox1.Visible = false;
label1.Text = ("Xweetok Inventory");
player1avatar = "xweetok";
if (ixiRadio.Checked)
{
ixiBox1.Visible = true;
pteriBox1.Visible = false;
xweetokBox1.Visible = false;
label1.Text = ("Ixi Inventory");
player1avatar = "ixi";
}
characterSelectBox.Visible = false;
radioSelectButton.Visible = false;
characterSelectBox2.Visible = true;
radioSelectButton2.Visible = true;
}
}
}
Now, not only do the visible items not swap over, two of the character choices don't display.
It looks like the radio buttons here can be checked/unchecked together. I think you should use separate if statements for each condition instead of else if.
You need to Invalidate your form to make it redraw after you change the properties. Assuming the radioSelectButton_Click method is in the same form, calling this.Invalidate() at the end of the method should force the redraw.
i have radio buttons that autopostback and set panels to either visible or invisible. The entire page is in an update panel so that i can force it to update and show the invisible changes. The radio buttons are also in update panels.
It works fine except for one thing - my javascript went out the window! It can't find any of my controls after the panel is updated.
Is there some way i can fix this?
Panel PnlPersonInjury = (Panel)FormView1.FindControl("PnlPersonInjury");
Panel pnlPropertyDamage = (Panel)FormView1.FindControl("pnlPropertyDamage");
RadioButton CTypeP = (RadioButton)FormView1.FindControl("RadioButton1");
RadioButton CTypeC = (RadioButton)FormView1.FindControl("RadioButton2");
RadioButton LossLossP = (RadioButton)FormView1.FindControl("RadioButton3");
RadioButton LossLossI = (RadioButton)FormView1.FindControl("RadioButton4");
if (LossLossI.Checked)
{
// pnlPropertyDamage.Enabled = false;
PnlPersonInjury.Enabled = true;
PnlPersonInjury.Visible = true;
pnlPropertyDamage.Visible = false;
InjSummmary.Visible = false;
PropSummary.Visible = false;
}
else
{
pnlPropertyDamage.Enabled = true;
PnlPersonInjury.Enabled = false;
PnlPersonInjury.Visible = false;
pnlPropertyDamage.Visible = true;
InjSummmary.Visible = false;
PropSummary.Visible = false;
}
if (CTypeC.Checked)
{
cPanel.Enabled = true;
pPanel.Enabled = false;
cPanel.Visible = true;
pPanel.Visible = false;
}
else
{
cPanel.Enabled = false;
pPanel.Enabled = true;
cPanel.Visible = false;
pPanel.Visible = true;
}
UpdatePanel20.Update();
UpdatePanel2.Update();
I left some of the instantiation of some controls out - so that is not an issue.
Without seeing the JavaScript, or knowing what part of this code is related to the error, I'd guess that this line is part of your problem:
PnlPersonInjury.Visible = false;
If a server-side control is hidden, it doesn't render anything to the client-side markup.