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.
Related
I have 5 buttons and 5 labels next to each button. When i run the app i expect the first button to be enabled and the the rest disabled and greyed out with the labels. after i click the first button it should disable with the label and enable the second button, and so forth with all the other buttons.
this way is to long, is there a better way of doing this?
private void Form1_Load(object sender, EventArgs e)
{
btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = true;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
private void btn1_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = true;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = true;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
private void btn2_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = true;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = true;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
}
private void btn3_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = true;
btn5.Enabled = false;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = true;
lblStep5.Enabled = false;
}
private void btn4_Click(object sender, EventArgs e)
{
btn1.Enabled = false;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = true;
lblStep1.Enabled = false;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = true;
}
private void btn5_Click(object sender, EventArgs e)
{
btn1.Enabled = true;
btn2.Enabled = false;
btn3.Enabled = false;
btn4.Enabled = false;
btn5.Enabled = false;
lblStep1.Enabled = true;
lblStep2.Enabled = false;
lblStep3.Enabled = false;
lblStep4.Enabled = false;
lblStep5.Enabled = false;
}
Let all these buttons and labels are inside a container(if it doesn't mean that you can use this.Controls as well if the form contains these buttons and labels only). Let it be pnlContainer, Now you can try something like this:
public void ButtonController(Button buttonToEnable, Label labelToenable)
{
foreach (Control ctrl in panel1.Controls)
{
if (ctrl == buttonToEnable || ctrl == labelToenable)
{
ctrl.Enabled = true;
}
else
{
ctrl.Enabled = false;
}
}
}
So in Form1_Load you want to enable btn1 and lblStep1 so the call should be :
ButtonController(btn1,lblStep1);
For btn1_Click the method call will be like ButtonController(btn2,lblStep2);. in short, you can pass the button and label that you want to enable to this method, which will disable rest of controls in the container.
I am trying to disable all buttons I am using after a time period of inactivity using the timer from toolbox. I can currently get it to work by just disabling the relevant buttons in
private void timer1_Tick(object sender, EventArgs e)
but this disables in ten seconds regardless of activity or inactivity.
Is it possible for the timer to work only after 10 seconds of no activity?
Here is the code I am using.
private void timer1_Tick(object sender, EventArgs e) {
button1.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = false;
button7.Enabled = false;
button8.Enabled = false;
button9.Enabled = false;
button10.Enabled = false;
button11.Enabled = false;
button12.Enabled = false;
button13.Enabled = true;
button14.Enabled = true;
button14.Visible = false;
button15.Enabled = true;
button15.Visible = false;
button17.Enabled = false;
button17.Visible = false;
button18.Visible = false;
button19.Visible = false;
button20.Enabled = false;
textBox1.Visible = false;
textBox2.Visible = false;
textBox3.Visible = false;
textBox4.Visible = false;
checkBox1.Visible = false;
checkBox2.Visible = false;
label15.Visible = false;
label16.Visible = false;
label21.Visible = false;
}
Any help would be greatly appreciated.
Just add the following event to each button that might be clicked and be counted as 'active':
public void ResetTimer(object sender, EventArgs e)
{
timer.Stop();
timer.Start();
}
...
button1.Click += ResetTimer;
The Timer in Winforms doesn't have a Reset method (nor does any of the other timer classes), so you'll have to stop it first and then start it again.
Solution 1:
Reset Time every time user click any button.
Solution 2: ( Not Recommended )
Have a global variable LastActiveTime, Every-time somebody clicks any
button :
LastActiveTime = DateTime.Now()
Parallel running Timer/Thread will check every second if inactive time is more than 10
second and will do accordingly.
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;
}
}
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 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.