I am making a game similar to "Make it Rain" or "Cookie Clicker." I have settings where you can purchase items to make each click of the cookie produce more cookies. I have been trying to make the game save the variable. For example, each click produces a cookie. If you have 100 cookies, you can buy a cookie cutter, so each click produces 5 cookies, and so on. I have booleans called cookiecutters, which are false. If the user buys the item, cookiecutters = true. I would like to save this variable to true so when the user closes and reopens the form, each click produces 5 cookies. This is what I have come up with, yet it doesn't work.
private void Form1_Load(object sender, EventArgs e)
{
if (cookiecutter == true)
{
cookiecutter = Convert.ToBoolean(Properties.Settings.Default.cookiecutter);
cookiecutter = Properties.Settings.Default.cookiecutter;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (cookiecutter== true)
{
Properties.Settings.Default.cookiecutter = cookiecutter = true;
}
Properties.Settings.Default.Save();
}
Thank you all for the help in advance!
Your code looks weird. Try to make these changes and make sure your initial value in the settings for cookiecutter is false.
private void Form1_Load(object sender, EventArgs e)
{
cookiecutter = Convert.ToBoolean(Properties.Settings.Default.cookiecutter);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.cookiecutter = cookiecutter;
Properties.Settings.Default.Save();
}
Almost.. you can get the value trough
Properties.Settings.Default.["cookiecutter"]
instead of
Properties.Settings.Default.cookiecutter
Related
I learned how to have a textbox and when the value is empty the button is disabled and when I enter a value in the textbox the button is enabled.
Now I want to have a login form that contains one textbox (for username) and another textbox (for password), so here I learned how to code.
But how should I write the code so that when the condition (both text boxes are empty) the button is disabled and when the condition is (both text boxes have values) the button is enabled.
Try this:
private void txtUserName_TextChanged(object sender, EventArgs e)
{
CheckFields();
}
private void txtPassword_TextChanged(object sender, EventArgs e)
{
CheckFields();
}
private void CheckFields()
{
btnLogin.Enabled = txtPassword.Text.Length == 0 || txtUserName.Text.Length == 0 ? false : true;
}
I assume that your username textbox is named, txtUserName, and your password textbox is named, txtPassword. Also the login button is named, btnLogin. I recommend setting the btnLogin enabled property to false when the form first loads. You can set that in the form's Load event:
private void Form1_Load(object sender, EventArgs e)
{
btnLogin.Enabled = false;
}
I think it would be better if you implement this logic on the client side (javascript), it is unnecessary to go to the server again and again for every text change.
you should add onclick function on username and password textboxes and implement the logic to check whether both have values then enable the button else disable it.
You should add the TextChanged event for both boxes and have it be something like this:
private void CheckTextboxes(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtUsername.Text) || string.IsNullOrWhiteSpace(txtPassword.Text))
{
button.Enabled = false;
return;
}
button.Enabled = true;
}
private void txtUsername_TextChanged(object sender, EventArgs e)
{
CheckTextboxes();
}
private void txtPassword_TextChanged(object sender, EventArgs e)
{
CheckTextboxes();
}
That way you ensure that if the user enters values on the textboxes in either order the button only enables if both have actual text written on them.
I know, i know there are lots and lots of questions asking on here about this error, each with their own response, but its easier to work off a response regarding your own code rather than someone else's
I have been working on this program for some time for a college assignment, and as soon as i started putting in the class to calculate the totals of things it now crashes,
I don't know where to look so i'll post my main code
enter code here
namespace Till
public partial class MainWindow : Window
{
Calculator calc = new Calculator();
public MainWindow()
{
InitializeComponent();
}
public bool User;
public bool tillopen = false;
private void button_Click(object sender, RoutedEventArgs e)
{
//button clone thingy
Button btn = (Button)sender;
label.Content = label.Content + btn.Content.ToString();
Console.Beep(); // makes the buttons beep
}
private void clear_Click(object sender, RoutedEventArgs e)
{
// Clear
label.Content = "";
}
private void Button_Submit_Click(object sender, RoutedEventArgs e)
{
// submit
listView.Items.Add(label.Content);
label.Content = "";
calc.setSoldItems(Convert.ToInt32(label.Content)); /// it breaks on this line///
}
private void button13_Click(object sender, RoutedEventArgs e)
{
//void sale
label.Content = "";
listView.Items.Clear();
}
private void button15_Click(object sender, RoutedEventArgs e)
{
//pound
label.Content = "1.00";
}
private void button12_Click(object sender, RoutedEventArgs e)
{
//till open close
tillopen = true;
}
private void button16_Click(object sender, RoutedEventArgs e)
{
Login m = new Login();
m.Show();
this.Close();
}
private void button14_Click(object sender, RoutedEventArgs e)
{
label.Content = "2.00"; // 2 pound
}
private void button17_Click(object sender, RoutedEventArgs e)
{
label.Content = calc.finish();
}
}
I have tried to re-create the error in another WPF (converting a to an int32) and it works fine, i know this is an issue with my code itself, i have tried using other machine and using different versions of visual studio itself, so we came to the assumption its this code itself and not a broken dll file
So before I sit down with my Teacher and spend all day going though my code step by step im asking around for help in order to save both of our time, This assignment is due in in 3 weeks, and it decides to break on me now.
thankies
To replicate this error, i press a number button on my Windows form, them hit the submit button i created (which starts the conversion) If a copy of my class which handles all of this is needed im happy to post it
In the method button_click, you have assigned value as
label.Content = label.Content + btn.Content.ToString();
which is a string value to the label and the values are concatenated instead of add.
and when you are reading it, you are converting it in Int32. which will give exception as it will not contain any integer value to it.
You can add the value like this:
label.Content = (Convert.ToInt32(label.Content) + Convert.ToInt32(btn.Content)).ToString();
and check before converting if the label has blank values in it, if it has do not convert them, and only convert the value if it has some values it will not give any error. Also do not assign any values other that numerical digits.
You are calculating:
Convert.ToInt32(label.Content)
but on the line before you set:
label.Content = "";
so this means you are calculating
Convert.ToInt32("")
which gives you a FormatException.
Perhaps you should use the value of label.Content before you overwrite it?
I'm trying to set the "txtMiles" textbox to focus after:
The form opens
When the "clear" button is clicked
I have tried using txtMiles.Focus(); but it doesn't seem to work for me.
CODE BEING USED ON THIS FORM
private void btnConvert_Click(object sender, EventArgs e)
{
//assigns variable in memory.
double txtMile = 0;
double Results;
try
{
// here is where the math happens.
txtMile = double.Parse(txtMiles.Text);
Results = txtMile * CONVERSION;
lblResults.Text = Results.ToString("n2");
txtMiles.Focus();
}
// if the user enters an incorrect value this test will alert them of such.
catch
{
//MessageBox.Show (ex.ToString());
MessageBox.Show("You entered an incorrect value");
txtMiles.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//This empties all the fields on the form.
txtMiles.Text = "";
txtMiles.Focus();
lblResults.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
// closes program
this.Close();
}
}
}
Thanks in advance for the help.
You should make sure your TabIndex is set and then instead of Focus(), try use Select(). See this MSDN link.
txtMiles.Select();
Also make sure there isn't a TabStop = true attribute set in the view file.
It's old, but someone could need this.
Control.Focus() is bugged. If it's not working, try workaround:
this.SelectNextControl(_controlname, true, true, true, true);
Change function parameters so they will work with your control, and remember about TabStop = true property of your control.
You already have your txtMiles focused after the clear-button click. As for the Startup, set txtMiles.Focus() in your load-method.
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}
using this solution worked perfectly...
txtMiles.Select();
I have a very simple program that has 4 buttons each button opens a url.
What i would like is to get each button to use Firefox as the default browser.
I know i can set it as the default browser and then it will work however if a user sets internet explorer as the defualt it will open in that instead.
These url only work in Firefox as it has certificate issues.
So essentially my question is how can i change my code to make sure that if you click on any of the 4 buttons, that it will open up in firefox and not internet explorer.
Here is my code
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=on");
BtnOff.BackColor = Color.Red;
Bnton.BackColor = Color.Chartreuse;
}
private void button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("http://192.168.2.56/dummy.htm?settings=save&user_active1=off");
Bnton.BackColor = Color.Red;
BtnOff.BackColor = Color.Chartreuse;
}
private void button3_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/pause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnDND.BackColor = Color.Chartreuse;
BtnQue.BackColor = Color.Red;
}
private void button4_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("https://217.20.xxx.xxx/api/agent/unpause.json?active_host=217.20.xxx.xxx&active_user=3012532&username=buildstore&password=cha9pU7U&client=snom");
BtnQue.BackColor = Color.Chartreuse;
BtnDND.BackColor = Color.Red;
}
Thanks for taking the time to look appreciate it.
you can use
System.Diagnostics.Process.Start(#"C:\Program Files (x86)\Mozilla Firefox\firefox.exe", "http://yoursite.com/dummy.htm");
you can keep the path of firefox.exe in config file instead of hard coding. Alternatively set the PATH environment variable.
guys!
I've got 2 forms in application - working form (frmMain) and form of settings (frmSettings).
There are two buttons on frmSettings - Save and Cancel. In frmMain I use the following approach to show the frmSettings:
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
// ...
}
The problem is I don't know, how to detect, which button was pressed on the frmMain - Save or Cancel. The further logic of the program depends on this fact. I need something like this:
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
if(/* frmSettings.SaveButton.WasClicked == true */)
{
InitializeServices();
}
// ...
}
Please, give me an advice, how to implement such kind of interaction between forms. Better without using global variables for saving buttons state.
Thanks beforehand.
ShowDialog returns a DialogResult object that allow you to know that. You have to:
On Save Button's click event, set this.DialogResult to DialogResult.OK
On Cancel Button's click event, set this.DialogResult to DialogResult.Cancel
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
if(frmSettings.ShowDialog() == DialogResult.OK)
{
InitializeServices();
}
//.......
}
Edited to manage the DialogResult as #tsiorn's answer: setting form's DialgoResult insted of setting that property on each button.
You chould use DialogResult to handle this. On your form settings window, you can set the result as so:
protected void btnSave_Click(object sender, EventArgs e) {
DialogResult = System.Windows.Forms.DialogResult.OK
this.close;
}
protected void btnCancel_Click(object sender, EventArgs e) {
DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.close;
}
Then ...
private void btnSettings_Click(object sender, EventArgs e)
{
frmSettings = new SettingsForm();
frmSettings.ShowDialog();
if(frmSettings.DialogResult == DialogResult.OK)
{
// save
InitializeServices();
}
// ...
}
Start with an enumeration of the possible values:
public enum ExitMethod
{
Other, //this should be first, as a default value
Save,
Cancel,
Error
}
Then make a property on SettingsForm of that type:
public ExitMethod ExitMethod { get; private set; }
In SettingsForm's save/exit methods set that property to the appropriate enum value, and in the main form you can read that property value.
in the frmSettings window you handle the Click events on the buttons. Then set the dialog result:
void frmSettings_Save_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
void frmSettings_Cancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
in the main form you do something like this to capture and evaluate the result:
DialogResult answer = frmSettings.ShowDialog();
if (answer == DialogResult.OK)
{
...
}
Additional information and usage can be found here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx