This is my code
int Moneycount;
private void mButton_Click(object sender, EventArgs e)
{
Moneycount++;
MoneyCount.Text = Moneycount.ToString();
}
private void level1_Click(object sender, EventArgs e)
{
}
"mButton" is incremented by 1 every time I press it,
so I want it to be incremented by 2 instead of 1 after I press "level1"
So if I understand you correctly, you want mButton to increment MoneyCount by one every time you press it. But after pressing level1, mButton should increment by 2 instead.
Try this:
int Moneycount;
int amountToIncrement = 1;
private void mButton_Click(object sender, EventArgs e)
{
Moneycount += amountToIncrement;
MoneyCount.Text = Moneycount.ToString();
}
private void level1_Click(object sender, EventArgs e)
{
amountToIncrement = 2;
}
Try
Moneycount += 2;
this increments by two
Instead of level1
being an actual Button
, it might be useful to make it a Checkbox
instead, and then make it look like a button by setting the Appearance
property to Button
in the designer. Here is a simple example where clicking on mButton
looks at the Checked
state of level1
to determine the correct increment value. At the same time, toggling the checked state of level1
sets the Increment
value of the NumericUpDown
control so that the Up/Down arrows also use the correct value of 1 or 2.
Example of coding the MainForm
that has the behavior you describe:
public partial class MainForm : Form , INotifyPropertyChanged
{
public MainForm()
{
InitializeComponent();
level1.Appearance = Appearance.Button;
numericUpDown.DataBindings.Add(
nameof(NumericUpDown.Value),
this,
nameof(Moneycount),
false,
DataSourceUpdateMode.OnPropertyChanged);
}
private void mButton_Click(object sender, EventArgs e)
{
Moneycount += level1.Checked ? 2 : 1;
}
private void level1_CheckedChanged(object sender, EventArgs e)
{
// Clicking up/down arrows increments 1 or 2
numericUpDown.Increment = level1.Checked ? 2 : 1;
}
public int Moneycount
{
get => _moneyCount;
set
{
if(!Equals(_moneyCount, value))
{
_moneyCount = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Moneycount)));
}
}
}
int _moneyCount = 0;
public event PropertyChangedEventHandler PropertyChanged;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.