C
C#6mo ago
drewhosick

WPF and variables that I need access to in a Button_Click event

Trying to figure out how to create a random number for a guessing game. Obviously I need to know where to put it so it's accessible to the program and generates a number when the program loads and before I can use the button_Click event on a button. I've attached the code for the random number. I found Window_Loaded but that doesn't seem to be working. I also tried putting it in the Main Window area and without luck. Sorry, still new. Where would it go so it could be accessible to the different events I can setup?
No description
11 Replies
drewhosick
drewhosick6mo ago
No description
drewhosick
drewhosick6mo ago
Also tried this
No description
Mayor McCheese
Mayor McCheese6mo ago
Okay first, this isn't the usual wpf way; this is winforms way; wpf the preferred way is mvvm. That being said, something to research later for you. That being said $code
MODiX
MODiX6mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
drewhosick
drewhosick6mo ago
This is what I had...
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

}
int guess;

private void Window_Load(object sender, RoutedEventArgs e)
{
Random rand = new Random();
guess = rand.Next(1, 100);
}


private void Button_Click(object sender, RoutedEventArgs e)
{
if (Convert.ToInt32(GuessInput.Text) < 1 || Convert.ToInt32(GuessInput.Text) > 100)
{
DisplayTextBox.Text = "Please choose a number between 1 and 100 and input it in the box below.";
}
else if (Convert.ToInt32(GuessInput.Text) == guess)
{
DisplayTextBox.Text = $"Congratulations! {GuessInput.Text} was the right number.";
}
else if (Convert.ToInt32(GuessInput.Text) < guess)
{
DisplayTextBox.Text = $"{GuessInput.Text} is too high. Try again.";
}
else
{
DisplayTextBox.Text = $"{GuessInput.Text} is too low. Try again.";

}
}
}
}
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

}
int guess;

private void Window_Load(object sender, RoutedEventArgs e)
{
Random rand = new Random();
guess = rand.Next(1, 100);
}


private void Button_Click(object sender, RoutedEventArgs e)
{
if (Convert.ToInt32(GuessInput.Text) < 1 || Convert.ToInt32(GuessInput.Text) > 100)
{
DisplayTextBox.Text = "Please choose a number between 1 and 100 and input it in the box below.";
}
else if (Convert.ToInt32(GuessInput.Text) == guess)
{
DisplayTextBox.Text = $"Congratulations! {GuessInput.Text} was the right number.";
}
else if (Convert.ToInt32(GuessInput.Text) < guess)
{
DisplayTextBox.Text = $"{GuessInput.Text} is too high. Try again.";
}
else
{
DisplayTextBox.Text = $"{GuessInput.Text} is too low. Try again.";

}
}
}
}
Mayor McCheese
Mayor McCheese6mo ago
so what I would end up with most likely is something like....
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int guess = 0;

public MainWindow()
{
InitializeComponent();
Random rand = new Random();
guess = rand.Next(1, 100);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
if (Convert.ToInt32(GuessInput.Text) < 1 || Convert.ToInt32(GuessInput.Text) > 100)
{
DisplayTextBox.Text = "Please choose a number between 1 and 100 and input it in the box below.";
}
else if (Convert.ToInt32(GuessInput.Text) == guess)
{
DisplayTextBox.Text = $"Congratulations! {GuessInput.Text} was the right number.";
}
else if (Convert.ToInt32(GuessInput.Text) < guess)
{
DisplayTextBox.Text = $"{GuessInput.Text} is too high. Try again.";
}
else
{
DisplayTextBox.Text = $"{GuessInput.Text} is too low. Try again.";

}
}
}
}
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int guess = 0;

public MainWindow()
{
InitializeComponent();
Random rand = new Random();
guess = rand.Next(1, 100);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
if (Convert.ToInt32(GuessInput.Text) < 1 || Convert.ToInt32(GuessInput.Text) > 100)
{
DisplayTextBox.Text = "Please choose a number between 1 and 100 and input it in the box below.";
}
else if (Convert.ToInt32(GuessInput.Text) == guess)
{
DisplayTextBox.Text = $"Congratulations! {GuessInput.Text} was the right number.";
}
else if (Convert.ToInt32(GuessInput.Text) < guess)
{
DisplayTextBox.Text = $"{GuessInput.Text} is too high. Try again.";
}
else
{
DisplayTextBox.Text = $"{GuessInput.Text} is too low. Try again.";

}
}
}
}
NB: This isn't the WPF way but I said this earlier and Convert.ToInt32 is really dated. $tryparse
MODiX
MODiX6mo ago
When you don't know if a string is actually a number when handling user input, use int.TryParse (or variants, e.g. double.TryParse)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
TryParse returns a bool, where true indicates successful parsing. Remarks: - Avoid int.Parse if you do not know if the value parsed is definitely a number. - Avoid Convert.ToInt32 entirely, this is an older method and Parse should be preferred where you know the string can be parsed. Read more here
drewhosick
drewhosick6mo ago
Ok thank you. I will continue to work on it. I did end up using Int32.Prase and followed someone'se tutorial to try it out but I guess it's still nott he WPF way. I'll have to learn more about it. As of right now I have this working:
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Guess_Number_Game
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int lives = 10;
private int random = 0;

public MainWindow()
{
InitializeComponent();
Random rnd = new Random();
random = rnd.Next() % 100;
}

private void txbInput_KeyDown(object sender, KeyEventArgs e)
{
if(lives == 0)
{
lblFrom.Content = "You";
lblTo.Content = "lose";
return;
}
if(e.Key == Key.Enter)
{
lives--;
int userGuessed = Int32.Parse(txbInput.Text);
if ( userGuessed == random)
{
lblFrom.Content = "You";
lblTo.Content = "win";
return;
}
if (userGuessed < random)
{
lblFrom.Content = userGuessed;
lblStatus.Content = $"Remaining Lives {lives}";
}
else
{
lblTo.Content = userGuessed;
lblStatus.Content = $"Remaining Lives {lives}";
}
}

}
}
}
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Guess_Number_Game
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int lives = 10;
private int random = 0;

public MainWindow()
{
InitializeComponent();
Random rnd = new Random();
random = rnd.Next() % 100;
}

private void txbInput_KeyDown(object sender, KeyEventArgs e)
{
if(lives == 0)
{
lblFrom.Content = "You";
lblTo.Content = "lose";
return;
}
if(e.Key == Key.Enter)
{
lives--;
int userGuessed = Int32.Parse(txbInput.Text);
if ( userGuessed == random)
{
lblFrom.Content = "You";
lblTo.Content = "win";
return;
}
if (userGuessed < random)
{
lblFrom.Content = userGuessed;
lblStatus.Content = $"Remaining Lives {lives}";
}
else
{
lblTo.Content = userGuessed;
lblStatus.Content = $"Remaining Lives {lives}";
}
}

}
}
}
Mayor McCheese
Mayor McCheese6mo ago
@drewhosick when I say it's not the WPF way, most folks using WPF use an idea calld MVVM $mvvm guess no tag so mvvm is a different programming paradigm, still c# of course.
drewhosick
drewhosick6mo ago
Yeah, I started reading about it but I am not very familiar with it yet. I will read more and continue to learn. Thanks for your time
Mayor McCheese
Mayor McCheese6mo ago
It's not easy to start with, but once it makes sense it's good to know