firstly thanks for taking your time to give me a hand.
So here is what I'm trying to do, I have a on / off switch.
If it's checked it has a tick.
If it's not checked it has a cross.
So I'm trying to make the Visibility set to false if it's got a cross and visibility to true if it's got a tick.
Here is my code.
private void on_off_CheckedChanged(object sender, EventArgs e)
{
if (on_off.Checked == true) return;
{
groupBox1.Visible = true;
statistics_text.Visible = true;
}
if (on_off.Checked == false) return;
{
groupBox1.Visible = false;
statistics_text.Visible = false;
}
}
But for some strange reason this doesn't seem to work.
Here's your code, formatted so you can see more easily what's going on:
if (on_off.Checked == true)
return;
groupBox1.Visible = true;
statistics_text.Visible = true;
if (on_off.Checked == false)
return;
groupBox1.Visible = false;
statistics_text.Visible = false;
I can't imagine you want to just return if the CheckBox is checked.
This is more likely what you wanted. Remove the return statement and make sure only one block or the other execute with an else statement.
if (on_off.Checked == true)
{
groupBox1.Visible = true;
statistics_text.Visible = true;
}
else
{
groupBox1.Visible = false;
statistics_text.Visible = false;
}
Even more succinctly:
groupBox1.Visible = on_off.Checked;
statistics_text.Visible = on_off.Checked;
Related
I have a couple of text boxes that i have initially hidden and what I want to do is as the users type I would like the next text box along with the label that goes with it to appear to notify of the next question.
At the same time if they change their mind and delete their response to the first question the next text box and label will disappear once the text has been deleted.
Here is my current code:
private void CreditScoreBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (char.IsControl(e.KeyChar) || char.IsDigit(e.KeyChar))
e.Handled = false;
else
e.Handled = true;
if(CreditScoreBox.Text == "")
{
MakeBox.Visible = false;
MakeLabel.Visible = false;
ModelBox.Visible = false;
ModelLabel.Visible = false;
CreditLevelLabel.Visible = false;
}
else
{
MakeBox.Visible = true;
MakeBox.Enabled = true;
MakeLabel.Visible = true;
CreditLevelLabel.Visible = true;
}
I have tried using the TextChanged event with the same result.
I created a Form, with 2 textbox and 1 label. If text exist in textbox1 then label1 and textbox2 become available:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
textBox2.Enabled = false;
textBox2.Visible = false;
label1.Visible = false;
}
else
{
textBox2.Enabled = true;
textBox2.Visible = true;
label1.Visible = true;
}
}
please i need a help i'm writing a code for hangman game and i want just to change the icon of the form , i try that using the icon property but it doesn't work any more ,please how i can do this ,and also i do the following project-->properties--->icon and manifest but also it doesn't work this is my code for the game it work perfectly :
private void button1_Click(object sender, EventArgs e)
{
stage_count++;//strat from stage 1
if (!int.TryParse(textBox1.Text.ToString(), out value))
return;//if user inter non_integer value do no thing and wait for new entry
if (key ==value )//directly if user guesses the key he/she wins the game
{
//view the seventh picture in hangman game
button1.Enabled = false;
textBox1.Text = "";
textBox1.Enabled = false;
pictureBox7.Visible = true;
pictureBox1.Visible = false;
pictureBox2.Visible = false;
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
groupBox1.Visible = true;
label1.Text = "Great... You Got It ^_^ ";
}
if (key > value)//guide the user to the correct answer
label1.Text = "Should Be Greater than" + value.ToString();
if (key < value)//guide the user to the correct answer
label1.Text = "Should Be Less than" + value.ToString();
if (key != value)
{
switch (stage_count)
{//this switch statement used to do some thing in each stage
case 1:
//view the first picture in hangman game
pictureBox1.Visible = true;
pictureBox2.Visible = false;
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
break;
case 2:
//view the second picture in hangman game
pictureBox1.Visible = false;
pictureBox2.Visible = true;
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
break;
case 3:
//view the third picture in hangman game
pictureBox1.Visible = false;
pictureBox2.Visible = false;
pictureBox3.Visible = true;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
break;
case 4:
//view the fourth picture in hangman game
pictureBox1.Visible = false;
pictureBox2.Visible = false;
pictureBox3.Visible = false;
pictureBox4.Visible = true;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
break;
case 5:
//view the fifth picture in hangman game
pictureBox1.Visible = false;
pictureBox2.Visible = false;
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = true;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
break;
default:
//view the sixth picture in hangman game
//it's the last try so diable the button and show the result
button1.Enabled = false;
textBox1.Text = "";
textBox1.Enabled = false;
groupBox1.Visible = true;
label1.Text = "YOU LOSE ,, IT WAS : " + key.ToString();
pictureBox1.Visible = false;
pictureBox2.Visible = false;
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = true;
pictureBox7.Visible = false;
break;
}//end switch
//to make empty focused textbox
textBox1.Text = "";
textBox1.Focus();
}//end if
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{// if user want to playe new game the programm must enables the important controls and
//make the visible property equals to false for all the pictures boxes
if (radioButton1.Checked == true)
{
stage_count = 0;
key = (1 + obj.Next(99));
textBox1.Enabled = true;
textBox1.Text = "";
textBox1.Focus();
button1.Enabled = true;
label1.Text = "Guess a number btween 1-100";
pictureBox1.Visible = false;
pictureBox2.Visible = false;
pictureBox3.Visible = false;
pictureBox4.Visible = false;
pictureBox5.Visible = false;
pictureBox6.Visible = false;
pictureBox7.Visible = false;
groupBox1.Visible = false;
radioButton1.Checked = false;
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
// if user won't to play again just close it
if (radioButton2.Checked == true)
{
radioButton2.Checked = false;
this.Close();
}
}
}
}
GO Properties window and change "Icon" property with your own icon.
You need to perform following steps
Add you icon to resouce file of project
than Asssing the created resource to you icon property .
Icon = Resources.adobe_reader_icon;
You have to set it in the properties of your project.
If you start your application by pressing F5 in VisualStudio and the icon doesn't appear, take a look in the Debug-(Release) Directory. There should be the correct icon
The icon of a form is only displayed if:
- ControlBox is true
- FormBorderStyle is not None
- Text is not empty
If so, you should then be able to set it with this.Icon = ...
Also the icon should be available as project resource. For this, you can just edit the forms Icon property, and in the following dialog import the icon from a .ico file.
<pre>
application root directory
application resources directory **LIKE** ../../resources/dropbox.ico
this.Icon = new System.Drawing.Icon("../../dropbox.ico");
</pre>
First Solution then Properties of the project Add .ico file
Then open Form which you want icon
In Form property you can find Icon Field
There you can change Icon your windows form.
I want to create a checkbox, If that is checked then it should display the dropdown. If not checked then it should hide the dropdown. This is how my code looks in Form.Designer.cs file.
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Items.AddRange(new object[] {
"Database 1",
"Database 2",
"Database 3"});
this.comboBox2.Location = new System.Drawing.Point(165, 436);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(150, 21);
this.comboBox2.TabIndex = 13;
this.comboBox2.Text = "Database";
and My checkbox code in other file is
if (checkBox1.CheckState == CheckState.Checked)
{
}
Use Visible
this.comboBox2.Visible = false;
Which would hide comboBox2.
if (checkbox1.CheckState == CheckState.Checked)
{
this.combobox2.Visible = True;
}
else (checkbox1.CheckState == CheckState.Unchecked)
{
this.combobox2.Visible = False;
}
Your going to want something like this
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
comboBox2.Visible = true;
if (checkBox1.Checked == false)
comboBox2.Visible = false;
And you will want to set the comboBox2 to visible = false in the properties tab, that should hopefully work.
Or just in one line:
comboBox2.Visible = (checkbox1.CheckState == CheckState.Checked)
If someone experiences the following:
The head of the dropwdownlist is hidden, after setting it to yourCombo.visible = false; but the list itself is still visible, then you can add the following:
yourComboBox.DroppedDown = false;
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 an aspx page where i dynamically add a radiobuttonlist with OnSelectedIndexChanged event. In the event i check for the selected items. i have 2 items.
For the first item,the event is firing well, However if i choose the other option the event is not firing: below the code..
The event is only firing is i change from "Some provided" to "All provided" the other way it is not working
Adding the RBL:
RadioButtonList dControl_b = new RadioButtonList();
dControl_b.ID = "rbl_MinCriteria";
dControl_b.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
dControl_b.CssClass = "Font";
dControl_b.Font.Name = "Arial";
dControl_b.Font.Size = 8;
dControl_b.ToolTip = "";
dControl_b.SelectedIndex = -1;
dControl_b.SelectedIndexChanged += new EventHandler(rbl_MinCriteria_SelectedIndexChanged);
dControl_b.AutoPostBack = true;
Checking the selected item:
if(rbl_MinCriteria.SelectedItem.ToString() == "All provided")
{
cbl_MinimumCriteria.Items[0].Selected = true;
cbl_MinimumCriteria.Items[1].Selected = true;
cbl_MinimumCriteria.Items[2].Selected = true;
cbl_MinimumCriteria.Items[3].Selected = true;
cbl_MinimumCriteria.Enabled = false;
//*************************************************************
if (ddl_CountryOccurence.SelectedValue != "Please choose")
{
ddl_CountryOccurence.Enabled = false;
}
else
{
ddl_CountryOccurence.Enabled = true;
}
//*************************************************************
if (tb_DueDate.Text != "")
{
tb_DueDate.Enabled = false;
}
else
{
tb_DueDate.Enabled = true;
}
OtherControlI.Enabled = false;
OtherControlII.Enabled = false;
OtherControlIII.Enabled = false;
}
if (rbl_MinCriteria.SelectedItem.ToString() == "Some provided")
{
cbl_MinimumCriteria.Items[0].Selected = false;
cbl_MinimumCriteria.Items[1].Selected = false;
cbl_MinimumCriteria.Items[2].Selected = false;
cbl_MinimumCriteria.Items[3].Selected = false;
cbl_MinimumCriteria.Enabled = true;
//*************************************************************
if (ddl_CountryOccurence.SelectedValue != "Please choose")
{
ddl_CountryOccurence.Enabled = false;
}
else
{
ddl_CountryOccurence.Enabled = true;
}
//*************************************************************
if (tb_DueDate.Text != "")
{
tb_DueDate.Enabled = false;
}
else
{
tb_DueDate.Enabled = true;
}
OtherControlI.Enabled = false;
OtherControlI.SelectedIndex = -1;
OtherControlII.Enabled = false;
OtherControlII.SelectedIndex = -1;
OtherControlIII.Enabled = false;
OtherControlIII.SelectedIndex = -1;
}
Any help and Comment is much appreciated
This is for people who find this question from Google:
On the RadioButtonList, set the AutoPostBack property to true.
RadioButtonList OnSelectedIndexChanged
I have this problem and solved it.
For raising onselectedindexchanged event of RadioButtonList , check below items:
<asp:RadioButtonList ID="rdlCondition" runat="server" AutoPostBack="True"
onselectedindexchanged="rdlCondition_SelectedIndexChanged">
and in the Page_Load set them with code :
rdlCondition.AutoPostBack = true;
rdlCondition.SelectedIndexChanged += new EventHandler (rdlCondition_SelectedIndexChanged);
Looking at the code above there seems to be alot of code reuse. I reorganized your code a bit (assuming you didnt leave anything out). Keep in mind I never tested it.
protected void rbl_MinCriteria_SelectedIndexChanged(object sender,EventArgs e)
{
if (rbl_MinCriteria.SelectedIndex<0) return; //If nothing is selected then do nothing
OtherControlI.Enabled = false;
OtherControlII.Enabled = false;
OtherControlIII.Enabled = false;
if(rbl_MinCriteria.SelectedItem.ToString() == "All provided")
{
cbl_MinimumCriteria.Items[0].Selected = true;
cbl_MinimumCriteria.Items[1].Selected = true;
cbl_MinimumCriteria.Items[2].Selected = true;
cbl_MinimumCriteria.Items[3].Selected = true;
cbl_MinimumCriteria.Enabled = false;
}
if (rbl_MinCriteria.SelectedItem.ToString() == "Some provided")
{
cbl_MinimumCriteria.Items[0].Selected = false;
cbl_MinimumCriteria.Items[1].Selected = false;
cbl_MinimumCriteria.Items[2].Selected = false;
cbl_MinimumCriteria.Items[3].Selected = false;
cbl_MinimumCriteria.Enabled = true;
OtherControlI.SelectedIndex = -1;
OtherControlII.SelectedIndex = -1;
OtherControlIII.SelectedIndex = -1;
}
//*************************************************************
if (ddl_CountryOccurence.SelectedValue != "Please choose")
{
ddl_CountryOccurence.Enabled = false;
}
else
{
ddl_CountryOccurence.Enabled = true;
}
//*************************************************************
if (tb_DueDate.Text != "")
{
tb_DueDate.Enabled = false;
}
else
{
tb_DueDate.Enabled = true;
}
}
I know this doesn't help your current problem but this is just a suggestion. If you could post the code where your actually adding the values to the list I could help a bit more.
EDIT: Your problem could be your not setting the value of your items, only the text. Try using rbl_MinCriteria.SelectedItem.Text =="All provided" instead.
I've made a sample aspx page, and added one panel in .aspx like below:
<asp:Panel ID="Panel1" runat="server"></asp:Panel>
And in code behind, I've added following code:
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList dControl_b = new RadioButtonList();
dControl_b.ID = "rbl_MinCriteria";
dControl_b.RepeatDirection = System.Web.UI.WebControls.RepeatDirection.Horizontal;
dControl_b.CssClass = "Font";
dControl_b.Font.Name = "Arial";
dControl_b.Font.Size = 8;
dControl_b.ToolTip = "";
dControl_b.SelectedIndex = -1;
dControl_b.SelectedIndexChanged += new EventHandler(rbl_MinCriteria_SelectedIndexChanged);
dControl_b.AutoPostBack = true;
dControl_b.Items.Add(new ListItem("All provided"));
dControl_b.Items.Add(new ListItem("Some provided"));
Panel1.Controls.Add(dControl_b);
}
protected void rbl_MinCriteria_SelectedIndexChanged(object sender,EventArgs e)
{
RadioButtonList rbl_MinCriteria = (RadioButtonList)Panel1.FindControl("rbl_MinCriteria");
if(rbl_MinCriteria.SelectedItem.ToString() == "All provided")
{
}
if (rbl_MinCriteria.SelectedItem.ToString() == "Some provided")
{
}
}
The event is FIRING EVERY TIME the radio button listitem is changed.
So, I'm afraid, you have done something wrong elsewhere. Good luck.