How to allocate two different functions to the same button? - c#

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();
}
}

Related

How to enable button after all textboxes are not empty in c# winforms?

How can I make button property set to enabled=true after all my textboxes are not empty?
I'm learning programming and my apps are simple.
I know how to enable this property when one of my textboxes have text but this is not the case.
Use case is that user need to put data in both textboxes and after that will be able to click btn.
How in most simple way can I validate all form and then enable button?
There are just 2 tb:
https://i.imgur.com/JUslNWE.png
You need to create a TextBox_TextChanged event and subscribe to all text boxes.
private void TextBox_TextChanged(object sender, EventArgs e)
{
int notEmptyTextBoxCount = 0;
int textBoxCount = 0;
foreach (var item in Controls)
{
if (item is TextBox txtb)
{
textBoxCount++;
if (txtb.Text != String.Empty)
notEmptyTextBoxCount++;
}
}
if (textBoxCount == notEmptyTextBoxCount)
button.Enabled = true;
else
button.Enabled = false;
}
Thanks guys for all feedback.
I have managed to do this this way:
private void ValidateTextBoxes()
{
if (loginTextBox.Text.Length != 0 && passTextBox.Text.Length != 0)
{
generateHashBtn.Enabled = true;
}
else
{
generateHashBtn.Enabled = false;
}
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}
private void TextBox2_TextChanged(object sender, EventArgs e)
{
ValidateTextBoxes();
}

C# winforms TreeView sometimes selects wrong item

I have winforms project with TreeView in it. Sometimes when I select some item other Item gets selected. I am very sure that there is not any code which could override the selection.
https://www.dropbox.com/s/qutmu0tmrkjspc6/disablederrortreeview.rar?dl=0
Here is example project. I need to update the treeview so there is timer which regurarly updates its content (so once per second items gets deselected) but when clicking fast enough sometimes it select different item, that I didn't click.
Anyone knows what the problem could be? Is there any workaround/fix, that could prevent this?
Thank you
Jakub
Two courses of action:
You can either: While the selection is happening disable the Tick event:
bool selecting = false;
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
selecting = true;
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
selecting = false;
// maybe you want to call the Tick now:
// timer1_Tick(null, null);
}
private void timer1_Tick(object sender, EventArgs e)
{
if (selecting) return;
// do your stuff
..
}
Alternatively you could disable selecting while the Tick is running:
bool ticking = false;
TreeNode clickedNode = null;
private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = ticking;
clickedNode = e.Node;
}
private void timer1_Tick(object sender, EventArgs e)
{
ticking = true;
// do your stuff
ticking = false;
// try to select the clicked node
if (clickedNode != null)
{ treeView1.SelectedNode = clickedNode; clickedNode = null;}
}

I need to use a if statement on a button click object within a timer tick?

Basically in the code below where i highlight, i would like to know how to use drawBricks method in the timer tick when that button(btnDisplayBricks) is pressed. Because i am using a timer tick and picturebox for paper drawings etc i cannot just simply call the method from within the button click event because the paper then clears in the timer only allowing me to display the bricks before timer1 starts any ideas.
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (btnDisplayBricks_Click[0] = true) ///code here problem
//then call method
DrawBricks(paper);
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
}
}
}
problem is in your equation, you should use == instead of =
if (btnDisplayBricks_Click[0] == true)
also move methodbtnDisplayBricks_Click outside of timer1_Tick
private bool buttonClicked = false;
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (buttonClicked)
{
DrawBricks(paper);
// maybe you want to set buttonclicked to false again, but specs are not clear to me
// buttonClicked = false;
}
}
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
buttonClicked = true;
}

Making more than an event on a button

Please, I want to know how to make two events on one button like: when I first click on the button display an image and while still in debugging mode the second time I click
display another image. What are some ways to do this?
You can make something like:
protected void Button1Click(object sender, EventArgs e)
{
if (Img1.Visible == false)
{
Img1.Visible = true;
}
else
{
Img2.Visible = true;
}
}
I don't think you need two (or more) events to do what you want, you only need to trace how many times you clicked the button, for example using an instance variable.
private int clicks = 0;
protected void myButton_Click(object sender, EventArgs e)
{
if(clicks == 1)
{
// do something
}
if(clicks == 2)
{
// do other things
}
if(clicks > 2)
{
// something else
}
clicks++;
}
What about something like this: just make sure to declare the counter outside the button
int clickedCount = 0;
private void button1_Click(object sender, EventArgs e)
{
clickedCount++;
if (clickedCount % 2 == 0) { pictureBox1.ImageLocation = #"path"; } else { pictureBox1.ImageLocation = #"path"; }
}

How to disable Events?

I createbtn1_MouseDown Event.
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
btn1.BackgroundImage = Properties.Resources.click;
}
I want to disable that btn1_MouseDownt Event behind this RadioButton when it checked. I mean to say when Radiobutton checked then this btn1_MouseDown event should not work.
private void r1_CheckedChanged(object sender, EventArgs e)
{
if (r1.Checked == true)
clock = 5;
radioButton2.Enabled = false;
radioButton3.Enabled = false;
radioButton4.Enabled = false;
}
Is that possible ?
inside r1_checkeChanged add:
btn1.MouseDown -=btn1_MouseDown;
You can remove the event:
btn1.MouseDown -= btn1_MouseDown
Of course don't forget to add it back again when you need it.
However, instead of doing that I would change your event handler's code to check the state of the radio button:
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
if (!r1.Checked)
btn1.BackgroundImage = Properties.Resources.click;
}
1st option can be to remove the event at radio button check_change event like this:
btn1.MouseDown -=btn1_MouseDow;
but i dont prefer this way.
Second is to add a flag at start of this event and check whether to execute the event or not
like this:
bool flag;
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
if (flag) return;
btn1.BackgroundImage = Properties.Resources.click;
}

Categories

Resources