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;
}
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;
}
Try
Moneycount += 2;
this increments by two
Related
I am working on Winforms with C#.
I have a problem with the logic, there are two different methods that I need to call, so that if I click the button, the first action should get applied and if I click the same button again, the second action should get applied.
This is not the exact code but I have an idea something like this:
private void button1_Click(object sender, EventArgs e)
{
if(button1.click==true)
{
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}
Here I have two problems regarding whether the button is already clicked:
If it's not clicked the Imgsave() should get activated.
If button is clicked the Imgsave() should get closed.
Can anyone please help me with this? Thanks.
You need to keep state somewhere. You can do this:
private bool buttonClicked = false;
private void button1_Click(object sender, EventArgs e)
{
if(!buttonClicked)
{
buttonClicked = true;
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}
This assumes you never going to click it a third time. If you are, you would need to handle that in some way.
I'd have either a class level variable track the number of times a button is clicked:
private bool _unclicked = false;
private void button1_Click(object sender, EventArgs e)
{
if(!_unclicked)
{
_unclicked = true; //toggle so next time the ELSE will be performed
fileNumber = 1;
ImgSave();
}
else
{
_unclicked = false; //toggle it off again
ImgSave.exit();
}
}
, or I'd store it in the .Tag of the button:
private void button1_Click(object sender, EventArgs e)
{
if(!button1.Tag.ToString() == "unclicked")
{
button1.Tag = "clicked"; //toggle so next time the ELSE will be performed
fileNumber = 1;
ImgSave();
}
else
{
button1.Tag = "unclicked"; //toggle it off again
ImgSave.exit();
}
}
You could also remove one event handler and add another:
private void button1_FirstClick(object sender, EventArgs e)
{
button1.Clicked -= button1_FirstClick;
button1.Clicked += button1_SecondClick;
fileNumber = 1;
ImgSave();
}
private void button1_SecondClick(object sender, EventArgs e)
{
button1.Clicked -= button1_SecondClick;
button1.Clicked += button1_FirstClick;
ImgSave.exit();
}
I've always been less of a fan of adding and removing event handlers to achieve things like this but it's quite a clean solution
You should save your state in a variable. Your state will change after first click and you can change the state of Clicking button with calling ConditionChanger() method anytime.
For example you may need change the state of variable when you clicked a second button.
private void ConditionChanger(){
myState = !myState;
}
Your variable :
private bool myState = false;
And your click event :
private void button1_Click(object sender, EventArgs e)
{
if(!myState)
{
myState = true;
fileNumber = 1;
ImgSave();
}
else
{
ImgSave.exit();
}
}
It goes like this, I have a numericUpDown which has a max value of 4. I want to make it so if the value is 1 only one button appears, if value is 2, two buttons appears, etc. How can I manage to do this in code ? I am using a timer and on every tick it checks if the value of the numericUpDown changes and if its changed it adds buttons, but how can I do the opposite thing, if value decreases, remove buttons? For example if I have the value of 4 and I already have 4 buttons appeared if I decrease with ONE, only one button should go away. How can I do this ?
private void timer1_Tick(object sender, EventArgs e)
{
if (numericUpDown1.Value == 1)
{
metroComboBox3.Show();
}
else if (numericUpDown1.Value == 2)
{
metroComboBox4.Show();
}
}
Simply do a double click on you numericUpDown in design, you dont need a timer.
You will get private void numericUpDown1_ValueChanged
Afther that your code should look like this:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown1.Value == 1)
{
metroComboBox3.Show();
}
else if (numericUpDown1.Value == 2)
{
metroComboBox4.Show();
}
}
If you have to do it with a timer then this is the way to go:
private void timer1_Tick(object sender, EventArgs e)
{
var buttons = new [] { button1, button2, button3, button4, };
for (var i = 0; i < buttons.Length; i++)
{
buttons[i].Visible = numericUpDown1.Value - 1 >= i
}
}
But I would use the numericUpDown1.ValueChanged event and do this:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
var buttons = new [] { button1, button2, button3, button4, };
for (var i = 0; i < buttons.Length; i++)
{
buttons[i].Visible = numericUpDown1.Value - 1 >= i
}
}
I need to know how to get mouse position when I press a key (insert).
This is what I trying to do:
I have a form1 with one buuton, when you press that button it call another form. But before call the form2 i need to get mouse position from an external application. To do this, the user must hover the cursor over requested position and press 'INSERT'.
public partial class _CalibrateGeneralStep2 : Form
{
public _CalibrateGeneralStep2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
///// HERE I NEED TO WAIT UNTIL USER PRESS 'INSERT' KEY BEFORE CALL _CalibrateGeneralStep3 /////
_CalibrateGeneralStep3 frm = new _CalibrateGeneralStep3();
frm.Show();
}
}
I try with keypress and keydown but I dont know use it well.
Thanks... sorry if my english is not good...
You can use
System.Windows.Forms.Cursor.Position: "It represents the current cursor position in screen co-ordinates"
Note: Please refer to the example to see how it works
You can use the KeyDown Event of the form (You can add it from the Designer to be sure it's wired properly)
Since you cannot just wait for the key press event inside your button2_Click, I've used a private field to store the fact that the button have been pressed. Now each time the user press Insert, you check if the button have been pressed and the cursor position. If both are correct, generate the new form.
I've defined the needed cursor position with the 2 constants at the top of the class, and you should also choose a better name for "hasButton2BeenClicked", depending of your business context haha.
public partial class _CalibrateGeneralStep2 : Form
{
private const int NEEDED_X_POSITION = 0;
private const int NEEDED_Y_POSITION = 0;
private bool hasButton2BeenClicked = false;
public _CalibrateGeneralStep2()
{
InitializeComponent();
KeyPreview = true;
}
private void button1_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button2_Click(object sender, EventArgs e)
{
hasButton2BeenClicked = true;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert && IsCursorAtTheCorrectPosition() && hasButton2BeenClicked)
{
GoToNextStep();
}
}
private bool IsCursorAtTheCorrectPosition()
{
return Cursor.Position.X == NEEDED_X_POSITION && Cursor.Position.Y == NEEDED_Y_POSITION;
}
private void GoToNextStep()
{
this.Hide();
new _CalibrateGeneralStep3().Show();
}
}
Made a simple app which using a timer, counts the number of mouse clicks on a panel for a given duration... simple enough, all working, except it seems to fail to count quickly enough to register all the mouse clicks?
I am literally incrementing a private int value on the click event of the panel, and showing a message box with the results on tick. Any Ideas? Code below...
Matt.
public partial class Form1 : Form
{
int click = 0;
public Form1()
{
InitializeComponent();
}
private void panel1_Click(object sender, EventArgs e)
{
click++;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void btnReset_Click(object sender, EventArgs e)
{
timer1.Stop();
txtClicks.Text = "";
txtTime.Text = "";
click = 0;
}
private void btnGo_Click(object sender, EventArgs e)
{
click = 0;
timer1.Interval = int.Parse(txtTime.Text) * 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
MessageBox.Show(txtClicks.Text + " seconds up, No of clicks:" + click.ToString());
}
}
Use the MouseDown Event. That'll handle every time and negate the need to handle both Click and DoubleClick.
except it seems to fail to count quickly enough to register all the mouse clicks?
may be you should handle Mouse DoubleClick event as well as Mouse Click?
I would put money on it that some of the clicks are coming through so fast that...... they count as a double click.
If you add a double click handler, and increment the counter twice while in that handler, does it produce a more accurate result?
On my GUI (Graphical User Interface), I have a button named Enter and a label.
When I click Enter, I want my result to be shown in the label. How do I do that?
For windows forms use the .Text property on the label:
private void btnEnter_Click(object sender, EventArgs e)
{
int themeaningoflifeuniverseandeverything = 420 / 10;
lblResult.Text = themeaningoflifeuniverseandeverything.ToString();
}
See exampe: ButtonEvent.zip
Double click on the button in the designer. This should create you a handler function for the buttons click event, something like this:
private void Button_Click(object sender, EventArgs e)
{
}
then in the function add the code to set the text of the lable:
lable.Text = myResult;
you should end up with something like this:
private void Button_Click(object sender, EventArgs e)
{
lable.Text = myResult;
}
As you said you have an int which is a percentage, and you have the value 0, I suspect that you are having problems getting the percentage not writing the value to the lable. so you might want to look at this question as I suspect that you have something like:
int valuea;
int valueb;
int result= valuea/valueb*100;
in this example if valuea=45 and valueb=100 the value of result will be 0, not 45.
int result = 0; // declare a private variable to hold the result
// Event handler for Click of Enter button
private void Enter_Click(object sender, EventArgs e)
{
result = Add(10,20); // set result to result of some function like Add
label.Text = result.ToString();
}
private int Add(int a, int b)
{
return a + b;
}
NOTE: I assume you are a beginner working with Winforms.
In winforms or webforms:
label.Text = Enter.Text;
Double click on button to generate event and write following code
private void Button_Click(object sender, EventArgs e)
{
lable1.Text = ur result; //result must be in string format otherwise convert it to string
}