create a random loop until a statement is true - c#

What should I do if I wanted the program to generate a random number then re-read/loop the else if statement until it finds a statement that is similar to this if (button1.Text == ""), The random number only needs to go up to 9.
This is my code,
private void button1_Click(object sender, EventArgs e)
{
var rc = new Random();
storeRI = rc.Next(1, 9);
if (storeRI == 1)
{
if (button1.Text == "")
{
button1.Text = "X";
}
else
{
//Need to generate another random number
//And read the else if statement again... how?
}
}
else if (storeRI == 2)
{
if (button1.Text == "")
{
button1.Text = "X";
}
else
{
//Need to generate another random number
//And read the else if statement again... how?
}
}

private void button1_Click(object sender, EventArgs e)
{
var rc = new Random();
do
{
storeRI = rc.Next(1, 9);
if (storeRI == 1)
{
if (button1.Text == "")
{
button1.Text = "X";
}
}
else if (storeRI == 2)
{
if (button1.Text == "")
{
button1.Text = "X";
}
}
} while (button1.Text == "");
}

put the if statements in a while() loop. Then have a contidion that executes a break; statement to terminate the loop:
while(button1.Text == "")
{
if (storeRI == 1)
{
if (button1.Text == "")
{
button1.Text = "X";
}
else
{
//Need to generate another random number
storeRI = rc.Next(1, 9);
}
}
else if (storeRI == 2)
{
...
}
else
break;
}

var rc = new Random();
int[] numbers = { 1, 2 }; // numbers from your if..else blocks
do {
storeRI = rc.Next(1, 9);
if (!numbers.Contains(storeRI))
break; // not matched any if..else block
if (button1.Text == "")
{
button1.Text = "X";
break; // set text and break loop
}
} while(true); // generate new number and do check again

Related

Randomize index without repeating

I want to create a program which would show the flag in a picture box and a user would have to guess what flag it is. I have a problem that flags are repeating and I totally don't know how to fix it.
Here is a bit of my code:
private void button1_Click(object sender, EventArgs e)
{
Random random = new Random();
randomized = random.Next(0, 9);
//0 = Poland
//1 = France
//2 = Sweden
//3 = Germany
//4 = Portugal
//5 = Spain
//6 = Finland
//7 = Norway
//8 = Russia
//9 = Ukraine
if (randomized == 0)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Poland.png");
}
if (randomized == 1)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/France.png");
}
if (randomized == 2)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Sweden.jpg");
}
if (randomized == 3)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Germany.png");
}
if (randomized == 4)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Portugal.png");
}
if (randomized == 5)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Spain.png");
}
if (randomized == 6)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Finland.png");
}
if (randomized == 7)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Norway.png");
}
if (randomized == 8)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Russia.png");
}
if (randomized == 9)
{
ptbFlag.Image = Image.FromFile("C:/Users/Kacper/Desktop/FlagiDoGeo/Ukraine.png");
}
}
If you want to show a different image every time, you may need to keep track of which images you've already shown.
private readonly Random _random = new();
private const string PathPrefix = "C:/Users/Kacper/Desktop/FlagiDoGeo/";
private static List<string> _flags = Reinitialize();
private void button1_Click(object sender, EventArgs e)
{
var randomized = _random.Next(0, _flags.Count);
var image = _flags[randomized];
// delete the image that will be displayed.
_flags.Remove(image);
// if all images are displayed, the game restarts from the beginning.
if (_flags.Count == 0)
{
_flags = Reinitialize();
}
ptbFlag.Image = PathPrefix + image;
}
private static List<string> Reinitialize()
{
return new List<string>
{
"Poland.png",
"France.png",
"Sweden.jpg",
"Germany.png",
// ...
};
}

Comma misplaced on RightToLeft label

I'm trying to partially recreate the Windows 10 calculator but have run into some trouble. I have a code that looks like this for when the user enters a comma:
if (label_number.Text != null)
{
label_number.Text += ",";
}
else
{
label_number.Text = "0,";
}
So if the user enters the comma while the string is null I want the string to read out "0," but it only adds the "," to the label. And when I add a comma to a number, it comes out like this ",07839" instead of "07839,". Why is this happening? Like the title says my label is RightToLeft, but this also happens when that option is false.
Edit, here is the entire MouseClick EventHandler
'''
private void MouseClickEvent(object sender, MouseEventArgs e)
{
var obj = (sender as Label);
// Left click
if (e.Button == MouseButtons.Left)
{
if (obj.Name == "button_quit") // Quit
{
Application.Exit();
}
else if (obj.Name == "button_minimize") // Minimize
{
this.WindowState = FormWindowState.Minimized;
}
else if (obj.Name == "button_c") // Reset calc
{
label_input.Text = String.Empty;
label_number.Text = String.Empty;
}
else if (obj.Name == "button_ce") // Clear number
{
label_number.Text = String.Empty;
}
else if (obj.Name == "button_delete") // Delete
{
if (label_number.Text.Length == 1)
{
label_number.Text = String.Empty;
}
else if (label_number.Text.Length > 1)
{
label_number.Text = label_number.Text.Remove(label_number.Text.Length - 1, 1);
}
}
else if (obj.Name == "button_divide" || obj.Name == "button_multiply" || obj.Name == "button_subtract" || obj.Name == "button_add") // / * - +
{
switch (obj.Name)
{
case "button_divide":
break;
case "button_multiply":
break;
case "button_subtract":
break;
case "button_add":
break;
}
}
else if (obj.Name == "button_comma") // Comma
{
if (label_number.Text != String.Empty)
{
label_number.Text += ",";
}
else
{
label_number.Text = "0,";
}
}
else if (obj.Name == "button_equals") // Calculate equation
{
}
else // Number input
{
if (label_number.Text.Length < 13)
{
if (obj.Name != "button_0")
{
if (label_number.Text.Length == 1 && label_number.Text == "0")
{
label_number.Text = obj.Name[obj.Name.Length - 1].ToString();
}
else
{
label_number.Text += obj.Name[obj.Name.Length - 1].ToString();
}
}
else
{
label_number.Text += obj.Name[obj.Name.Length - 1].ToString();
}
}
}
}
}
'''

Wait 20 Seconds In Timer Before Executing Next Line Without Thread.Sleep.C#

I'm trying to wait for 20 seconds before adding + 1 value to i (Int), But i want to do it without Thread.Sleep.
This is my code, By the way I'm not a Pro programmer.
private void Refresh_App_TimerNH_Tick(object sender, EventArgs e)
{
label18.Text = "Timer Activated";
int i = 0;
i = i + 1;
if (i == 16)
{
i = 0;
}
else
{
}
if (i == 1)
{
webBrowser1.Refresh();
userIdLabel1.BackColor = Color.Red;
label20.Text = "+1";
//**i want to add 20 second gap here**
i = i + 1;
}
else
{
}
if (i == 2)
{
webBrowser2.Refresh();
userIdLabel2.BackColor = Color.Red;
label20.Text = "+2";
i = i + 1;
}
else
{
}
if (i == 3)
{
webBrowser3.Refresh();
userIdLabel3.BackColor = Color.Red;
label20.Text = "+3";
i = i + 1;
}
else
{
}
if (i == 4)
{
webBrowser4.Refresh();
userIdLabel4.BackColor = Color.Red;
label20.Text = "+4";
i = i + 1;
}
else
{
}
if (i == 5)
{
webBrowser5.Refresh();
userIdLabel5.BackColor = Color.Red;
label20.Text = "+5";
i = i + 1;
}
else
{
}
if (i == 6)
{
webBrowser6.Refresh();
userIdLabel6.BackColor = Color.Red;
label20.Text = "+6";
i = i + 1;
}
else
{
}
if (i == 7)
{
webBrowser7.Refresh();
userIdLabel7.BackColor = Color.Red;
label20.Text = "+7";
i = i + 1;
}
else
{
}
if (i == 8)
{
webBrowser8.Refresh();
userIdLabel8.BackColor = Color.Red;
label20.Text = "+8";
i = i + 1;
}
else
{
}
if (i == 9)
{
webBrowser9.Refresh();
userIdLabel9.BackColor = Color.Red;
label20.Text = "+9";
i = i + 1;
}
else
{
}
if (i == 10)
{
webBrowser10.Refresh();
userIdLabel10.BackColor = Color.Red;
label20.Text = "+10";
i = i + 1;
}
else
{
}
if (i == 11)
{
webBrowser11.Refresh();
userIdLabel11.BackColor = Color.Red;
label20.Text = "+11";
i = i + 1;
}
else
{
}
if (i == 12)
{
webBrowser12.Refresh();
userIdLabel12.BackColor = Color.Red;
label20.Text = "+12";
i = i + 1;
}
else
{
}
if (i == 13)
{
webBrowser13.Refresh();
userIdLabel13.BackColor = Color.Red;
label20.Text = "+13";
i = i + 1;
}
else
{
}
if (i == 14)
{
webBrowser14.Refresh();
userIdLabel14.BackColor = Color.Red;
label20.Text = "+14";
i = i + 1;
}
else
{
}
if (i == 15)
{
webBrowser15.Refresh();
userIdLabel15.BackColor = Color.Red;
label20.Text = "+15";
i = i + 1;
}
else
{
}
if (i == 16)
{
webBrowser16.Refresh();
userIdLabel16.BackColor = Color.Red;
label20.Text = "+16";
i = i + 1;
}
else
{
}
Refresh_App_TimerNH.Stop();
label18.Text = "Timer De-Activated";
Refresh_App_TimerNH.Start();
}
I think it might be easy but not for me, Because i'm new to c#
First off, Tim S's answer -- break up your logic into smaller chunks and simplify the timer logic -- is good. But to answer your specific question, which was "how do I delay between statements without Sleep", is: make the method async and then use await Task.Delay(...).
You are right to avoid Sleep; using it is a bad programming practice, and will hang your application. await Task.Delay does an asynchronous wait -- that is, the method returns immediately, the app keeps on processing UI messages, and when the delay is done, the program schedules the remainder of the method to execute later.
Note that during an asynchronous wait, by design messages keep on getting processed. If one of those messages causes your event handler to run again then you can get into the very confusing situation of having multiple control points in the same non-recursive method. Try to avoid that.
Right now your logic is convoluted, and probably very different from what you want it to do.
Your code will be much simpler if you put your webBrowserX and userIdLabelX items in some sort of list together.
public class MyPair
{
public WebBrowser WebBrowser { get; set; }
public Label UserIdLabel { get; set; }
}
private List<MyPair> pairs;
private int refreshIndex = 0;
private void StartTimer()
{
pairs = // populate pairs somehow
refreshIndex = 0;
var timer = new System.Windows.Forms.Timer();
timer.Interval = 20000
timer.Tick += MyTickHandler;
timer.Start();
label18.Text = "Timer Activated";
}
private void MyTickHandler(object sender, EventArgs e)
{
pairs[refreshIndex].WebBrowser.Refresh();
pairs[refreshIndex].UserIdLabel.BackColor = Color.Red;
label20.Text = "+" + (refreshIndex + 1);
refreshIndex = (refreshIndex + 1) % pairs.Count;
}
Note that this timer never deactivates, it loops through the list of pairs repeatedly.

ask for confirmation when checking a box

i currently have a form that has several check boxes, once all the boxes are checked or left empty, it sends it to the database the state of the check boxes
i want to add, if a box is not checked, when you check it, it should ask for confirmation window "you sure you wanna check this"
any help please?
this is the current code
public IMeditor(IMui IMui, IMuser U, string User)
{
InitializeComponent();
this.IMui = IMui;
imu = U;
if (imu.UID == 0)
{
Add.Text = "Add";
imu.name = user;
}
else
Add.Text = "Update";
AuthChat.Checked = imu.AuthChat == 1;
AuthTac1.Checked = imu.AuthTac1 == 1;
AuthTac2.Checked = imu.AuthTac2 == 1;
AuthTac3.Checked = imu.AuthTac3 == 1;
AuthTac4.Checked = imu.AuthTac4 == 1;
AuthTac5.Checked = imu.AuthTac5 == 1;
AuthTac6.Checked = imu.AuthTac6 == 1;
AuthTac7.Checked = imu.AuthTac7 == 1;
AuthTac8.Checked = imu.AuthTac8 == 1;
AuthTac9.Checked = imu.AuthTac9 == 1;
AuthTac10.Checked = imu.AuthTac10 == 1;
switch (imu.Transport.ToLower()) {
case "aim": Transport.SelectedIndex = 0; break;
case "gtalk": Transport.SelectedIndex = 1; break;
case "msn": Transport.SelectedIndex = 2; break;
case "yahoo": Transport.SelectedIndex = 3; break;
}
}
I agree with Izzy. Add something like
private void PrivateInitialize()
{
CheckBox chkBox = new CheckBox();
chkBox.Text = "Click me";
chkBox.CheckedChanged += new EventHandler(chkBox_CheckedChanged);
}
private void chkBox_CheckedChanged(object sender, EventArgs e)
{
MessageBox.Show("Nag, nag, nag! You take out the trash yet?");
}
You need to handle the event CheckedChangedEvent for the checkbox. You can find the sample code on MSDN. Also
this link for the complete understanding of checkbox.
Or else you can derive your own class from CheckBox control of windows forms then override the required function and implement your own logic and create all the checkboxes in your application from this derived class
public IMeditor(IMui IMui, IMuser U, string User)
{
InitializeComponent();
this.IMui = IMui;
imu = U;
if (imu.UID == 0)
{
Add.Text = "Add";
imu.name = user;
}
else
Add.Text = "Update";
AuthChat.Checked = imu.AuthChat == 1;
AuthTac1.Checked = imu.AuthTac1 == 1;
AuthTac2.Checked = imu.AuthTac2 == 1;
AuthTac3.Checked = imu.AuthTac3 == 1;
AuthTac4.Checked = imu.AuthTac4 == 1;
AuthTac5.Checked = imu.AuthTac5 == 1;
AuthTac6.Checked = imu.AuthTac6 == 1;
AuthTac7.Checked = imu.AuthTac7 == 1;
AuthTac8.Checked = imu.AuthTac8 == 1;
AuthTac9.Checked = imu.AuthTac9 == 1;
AuthTac10.Checked = imu.AuthTac10 == 1;
switch (imu.Transport.ToLower()) {
case "aim": Transport.SelectedIndex = 0; break;
case "gtalk": Transport.SelectedIndex = 1; break;
case "msn": Transport.SelectedIndex = 2; break;
case "yahoo": Transport.SelectedIndex = 3; break;
}
AuthChat.Click += new EventHandler(ClickHandler);
}
void ClickHandler(Object obj, EventArgs args)
{
if (obj == AuthChat && AuthChat.CheckState == CheckState.Checked)
{
DialogResult result = MessageBox.Show("Do you want to change?", "are you sure?", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
AuthChat.Checked = false;
}
}
}
public IMeditor(IMui IMui, IMuser U, string User)
{
InitializeComponent();
this.IMui = IMui;
imu = U;
if (imu.UID == 0)
{
Add.Text = "Add";
imu.name = user;
}
else
Add.Text = "Update";
AuthChat.Checked = imu.AuthChat == 1;
AuthTac1.Checked = imu.AuthTac1 == 1;
AuthTac2.Checked = imu.AuthTac2 == 1;
AuthTac3.Checked = imu.AuthTac3 == 1;
AuthTac4.Checked = imu.AuthTac4 == 1;
AuthTac5.Checked = imu.AuthTac5 == 1;
AuthTac6.Checked = imu.AuthTac6 == 1;
AuthTac7.Checked = imu.AuthTac7 == 1;
AuthTac8.Checked = imu.AuthTac8 == 1;
AuthTac9.Checked = imu.AuthTac9 == 1;
AuthTac10.Checked = imu.AuthTac10 == 1;
switch (imu.Transport.ToLower()) {
case "aim": Transport.SelectedIndex = 0; break;
case "gtalk": Transport.SelectedIndex = 1; break;
case "msn": Transport.SelectedIndex = 2; break;
case "yahoo": Transport.SelectedIndex = 3; break;
}
AuthChat.CheckedChanged += new EventHandler(CheckChangedHandler);
AuthChat1.CheckedChanged += new EventHandler(CheckChangedHandler);
}
void CheckChangedHandler(Object obj, EventArgs args)
{
if (obj == AuthChat)
{
MessageBox.Show("Checked changed for AuthChat");
}
else if (obj == AuthChat1)
{
MessageBox.Show("Checked changed for AuthChat1");
}
}
do it with javascript? add OnClientClick="return confirmCheck(this);"
<script language="javascript">
function confirmCheck(e){
return confirm("are you sure?");
// do other stuff
}
</script>
this should be better than redirecting people

Displaying random numbers in C#

I'm trying to design a code where one guess a number. I defined the range which number to display in my listbox. I started to write Random(1,10) but if I enter 11, it still writes in my listbox. How can I just write the number selected from my range, which is 1-10?
Here is a part of my code:
private void btnOk_Click(object sender, EventArgs e)
{
string yourNumber;
yourNumber = textBox1.Text.Trim();
int returnNumber = RandomNumber(1, 10);
int.TryParse(textBox1.Text, out returnNumber);
listBox1.Items.Add(returnNumber);
}
Additional question
If I would like to display a range of number like for example 1-10, how could I do the following? The user will type 11 the program will not accept that.
I made something like this:
int returnNumber = RandomNumber(1, 10);
string yourNumber;
yourNumber = textBox1.Text.Trim();
if(Int32.TryParse(textBox1.Text>=1)) && (Int32.TryParse(textBox1.Text<=10));
{
listBox1.Items.Add(yourNumber);
textBox1.Text = string.Empty;
}
Something is wrong in the program.
Update
For Nathaniel, I tried this one:
int returnNumber=RandomNumber(1,10);
int counter=1;
int yourNumber;
Int32.TryParse(textBox1.Text.Trim(), out yourNumber);
if (yourNumber >=1 && yourNumber <= 10)
{
listBox1.Items.Add(yourNumber);
}
else
{
MessageBox.Show("Please enter a number between 1-10");
}
What I would like to do is design a program for guessing a number. So this is the first part.
Update
Here is my final code, can it be improved? I think the next thing I'll do is to limit the times the user types the input. That means, they can only guess the right number 3 times or 5 times. Not sure where to implement it
namespace Guessing_Game
{
public partial class Form1 : Form
{
private static int randomNumber;
private const int rangeNumberMin = 1;
private const int rangeNumberMax = 10;
public Form1()
{
InitializeComponent();
randomNumber = GenerateNumber(rangeNumberMin, rangeNumberMax);
}
private int GenerateNumber(int min,int max)
{
Random random = new Random();
return random.Next(min, max);
}
private void btnOk_Click(object sender, EventArgs e)
{
int yourNumber = 0;
Int32.TryParse(textBox1.Text.Trim(), out yourNumber);
if (yourNumber>= rangeNumberMin && yourNumber<=rangeNumberMax)
{
listBox1.Items.Add(yourNumber);
if (yourNumber > randomNumber)
{
listBox2.Items.Add("No the Magic Number is lower than your number");
}
if (yourNumber < randomNumber)
{
listBox2.Items.Add("No, the Magic Number is higher than your number");
}
if(yourNumber==randomNumber)
{
listBox2.Items.Add("You guessed the Magic Number!");
btnRestart.Enabled = true;
}
}
else
{
MessageBox.Show("Please enter a number between " + rangeNumberMin + " to " + rangeNumberMax);
}
}
private void btnRestart_Click(object sender, EventArgs e)
{
listBox2.Items.Clear();
listBox1.Items.Clear();
textBox1.Text = null;
randomNumber = GenerateNumber(rangeNumberMin, rangeNumberMax);
btnRestart.Enabled = false;
}
}
The line:
int returnNunmber = RandomNumber(1, 10);
does nothing, because in the next line returnNumber is used as an output variable and will be whatever number is in textBox1. Remove the
int.TryParse(textBox1.Text, out returnNumber);
line and it will add a random number from 1 to 10 to your listbox.
EDIT::::
To answer you additional question, try:
private void btnOk_Click(object sender, EventArgs e)
{
string yourNumber;
yourNumber = textBox1.Text.Trim();
int returnNumber;
int.TryParse(textBox1.Text, out returnNumber);
if( returnNumber < 1 || returnNumber > 10) {
returnNumber = RandomNumber(1, 10);
}
listBox1.Items.Add(returnNumber);
}
Some minor changes to your code, condensing a couple lines and adding the limit code, utilizing the list of guesses as the counter:
namespace Guessing_Game
{
public partial class Form1 : Form
{
private static int randomNumber;
private const int rangeNumberMin = 1;
private const int rangeNumberMax = 10;
private const int maxGuesses = 5;
public Form1()
{
InitializeComponent();
randomNumber = GenerateNumber(rangeNumberMin, rangeNumberMax);
}
private int GenerateNumber(int min,int max)
{
Random random = new Random();
return random.Next(min, max);
}
private void btnOk_Click(object sender, EventArgs e)
{
int yourNumber = 0;
if (Int32.TryParse(textBox1.Text.Trim(), out yourNumber) &&
yourNumber>= rangeNumberMin && yourNumber<=rangeNumberMax)
{
listBox1.Items.Add(yourNumber);
if (yourNumber > randomNumber)
{
listBox2.Items.Add("No the Magic Number is lower than your number");
}
else if (yourNumber < randomNumber)
{
listBox2.Items.Add("No, the Magic Number is higher than your number");
}
else
{
listBox2.Items.Add("You guessed the Magic Number!");
textBox1.Enabled = false;
btnOk.Enable = false;
btnRestart.Enabled = true;
}
//Will stop on the 5th guess, but guards the case that there were more than 5 guesses
if(listBox1.Items.Count >= maxGuesses && yourNumber != randomNumber)
{
listBox2.Items.Add("You are out of guesses!");
textBox1.Enabled = false;
btnOk.Enable = false;
btnRestart.Enabled = true;
}
}
else
{
MessageBox.Show("Please enter a number between " + rangeNumberMin + " to " + rangeNumberMax);
}
}
private void btnRestart_Click(object sender, EventArgs e)
{
listBox2.Items.Clear();
listBox1.Items.Clear();
textBox1.Text = null;
randomNumber = GenerateNumber(rangeNumberMin, rangeNumberMax);
btnRestart.Enabled = false;
textBox1.Enabled = true;
btnOk.Enable = true;
}
}
}
Editted to prevent the "You're out of guesses" message when the number is correctly guessed on the last guess.
Lets take that piece by piece:
int returnNumber = RandomNumber(1, 10);
There is no inbuilt RandomNumber function; note that with the Random class, the end value is exclusive, so for a number in a range, you'll need something like:
static readonly Random rand = new Random();
static int Random(int min, int max) {
if(max < min) throw new ArgumentOutOfRangeException("max");
lock(rand) {
return rand.Next(min, max + 1);
}
}
However, you then throw away this value completely:
int.TryParse(textBox1.Text, out returnNumber);
The use of out means that the previous value of returnNumber is ignored completely. I'm not sure what your intent is, but it seems like you just want to check the value:
if(int.TryParse(textBox1.Text, out returnNumber)
&& returnNumber >= 1 && returnNumber <= 10)
{
listBox1.Items.Add(returnNumber);
}
I've tried to look at the last example, but it really isn't clear what you are trying to do - can you clarify?
(edited after question edit and comments)
You would need a counter, which you increment for failed tries - something like:
using System;
using System.Drawing;
using System.Windows.Forms;
class MyForm : Form {
Button btn;
ListBox lst;
TextBox tb;
const int MaxTries = 3, MaxNumber = 10;
int targetNumber, guessCount = 0;
public MyForm() {
targetNumber = new Random().Next(1, MaxNumber + 1);
Text = "Guess a number";
Icon = SystemIcons.Question;
Controls.Add(lst = new ListBox {Dock=DockStyle.Fill});
Controls.Add(btn = new Button {Text="Guess",Dock=DockStyle.Top});
Controls.Add(tb = new TextBox {Dock=DockStyle.Top});
btn.Click += btn_Click;
}
void btn_Click(object sender, EventArgs e) {
int userNumber;
if (int.TryParse(tb.Text.Trim(), out userNumber)) {
if (userNumber < 1 || userNumber > MaxNumber) {
lst.Items.Add("Did I mention... between 1 and " + MaxNumber);
} else {
if (userNumber == targetNumber) {
lst.Items.Add("Well done! You guessed well");
btn.Enabled = false; // all done
} else {
lst.Items.Add(targetNumber < userNumber
? "Try a bit lower" : #"It is bigger than that");
if (++guessCount >= MaxTries) {
btn.Enabled = false;
lst.Items.Add("Oops, should have picked more wisely");
}
}
}
} else {
lst.Items.Add("Nice; now give me a number");
}
}
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyForm());
}
}

Categories