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;
}
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();
}
}
I have a form, on which i placed 2 radio buttons. My issue is I need it to function in a way where if one is clicked, the other will be unclicked. I have the following code however it gets stuck in an inifinite loop once you do the first click and I do understand why. Wanted to see if any of you guys know how to go about making this in c#? I'm fairly new to c#
public partial class Form1 : Form
{
public Form1()
{
radAllCols.CheckedChanged += new EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged += new EventHandler(this.radSelCols_Checked);
}
private void radAllCols_Checked(object sender, EventArgs e)
{
if (radAllCols.Checked == true)
{
radAllCols.Checked = false;
radSelCols.Checked = true;
}
}
private void radSelCols_Checked(object sender, EventArgs e)
{
if (radSelCols.Checked == true)
{
radSelCols.Checked = false;
radAllCols.Checked = true;
}
}
}
If the radio buttons have different RadioGroup values you have to first, unregister the Checked event, change the Checked property value and re-register the Checked event.
private void radAllCols_Checked(object sender, EventArgs e)
{
if (radAllCols.Checked == true)
{
radAllCols.CheckedChanged -= new
EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged -= new
EventHandler(this.radSelCols_Checked);
radAllCols.Checked = false;
radSelCols.Checked = true;
radAllCols.CheckedChanged += new
EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged += new
EventHandler(this.radSelCols_Checked);
}
}
private void radSelCols_Checked(object sender, EventArgs e)
{
if (radSelCols.Checked == true)
{
radAllCols.CheckedChanged -= new
EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged -= new
EventHandler(this.radSelCols_Checked);
radSelCols.Checked = false;
radAllCols.Checked = true;
radAllCols.CheckedChanged += new
EventHandler(this.radAllCols_Checked);
radSelCols.CheckedChanged += new
EventHandler(this.radSelCols_Checked);
}
}
The code above is for very custom scenarios and should be avoided as much as possible. The radio boxes should behave the way you want automatically. Make sure you have the same RadioGroup property value on both of them.
RadioButtons placed in the same parent control (like a panel) behave this way by default.
There is no need to use a checked event for this.
Setting values of the radAllCols.Checked = true property fires the radAllCols_Checked event this causes your infinite "loop"
Since you are trying to uncheck the same radioButton to checked
private void radSelCols_Checked(object sender, EventArgs e)
{
if (radSelCols.Checked == true)
{
radSelCols.Checked = false; // reversed
radAllCols.Checked = true; // reversed
}
}
If you use GroupBox Container Element for same Radio Buttons that you want select one of them, you don`t need handle check state of Radio Buttons manually, When you select a Radio Button all the other Radio Buttons in the same group will be unchecked.
Your rest of the code is fine but you need to change your code in Checked methods() as mentioned below to prevent infinite loop and then it will work fine:
private void radAllCols_Checked(object sender, EventArgs e)
{
radAllCols.Checked = !radSelCols.Checked;
}
private void radSelCols_Checked(object sender, EventArgs e)
{
radSelCols.Checked = !radAllCols.Checked;
}
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;}
}
Situation is as follows:
I have a ButtonA, which is currently not enabled.
I have 5 Radiobuttons of which one is always checked.
I want to enable ButtonA when a different Radiobutton gets selected.
I thought about doing something like
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
private void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
... and so on.
There is probably a more elegant solution and im missing it.
You can use a single method as an event handler for all radiobuttons:
private void RadioButtonChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
private void HandleEvents()
{
this.RadioButton1.CheckedChanged += RadioButtonChanged;
this.RadioButton2.CheckedChanged += RadioButtonChanged;
this.RadioButton3.CheckedChanged += RadioButtonChanged;
}
Or a loop to do the same thing:
private void RadioButtonChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
private void HandleEvents()
{
foreach(var rb in new[] {RadioButton1, RadioButton2, RadioButton3})
rb.CheckedChanged += RadioButtonChanged;
}
Or even a lambda event handler set in a loop:
private void HandleEvents()
{
foreach(var rb in new[] {RadioButton1, RadioButton2, RadioButton3})
rb.CheckedChanged += (o,e) => ButtonA.Enabled = true;
}
How about just having 1 event handler like this:
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
ButtonA.Enabled = true;
}
Then you can use the form designer to assign this method name to the CheckedChanged events of all the radio buttons.
This will save you having to repeat the code for each radio button.
Another trick is to add all your radio buttons to a group box or panel in the designer, then they will all be children of that control.
Then you can add the following code to your Load event handler for the form:
foreach(var radioButton in radioGroupBox.Controls.Cast<Control>()
.Where(i => i is RadioButton)
.Cast<RadioButton>())
{
radioButton.CheckedChanged += RadioButton_CheckedChanged;
}
This way, if you add more RadioButtons to the group, you don't need to change any code.
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;
}