C# checkbox and mouse click event - c#

I am self teaching myself C# and ran into a problem I haven't seem to find an answer too. I have a Form that when I mouse click the check box the state goes to true but also immediately triggers the mouse click event I have code follows:
private void uxCheckBoxMouseClick(object sender, MouseEventArgs e)
{
//MouseEventArgs me = (MouseEventArgs) e;
if (uxMouseCopyCheckBox.Checked)
{
MessageBox.Show("Test");
uxMouseCopyCheckBox.Checked = false;
}
}
I have searched the stack overflow and Google and found similar items but not in C# but no luck on the fixed solution. What I want to do it use the first click to change the check box to true without triggering the mouse click event. I want to delay the event to the 2nd mouse click and not the first.
I have tried the following:
for loop
Clicks == 2 with if statement
subscribing but at a loss on what to use

Instead of the Click event you could subsribe to the CheckedChanged event :
The Handler will look look exactly like yours :
private void uxMouseCopyCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (!uxMouseCopyCheckBox.Checked)
{
MessageBox.Show("Test");
uxMouseCopyCheckBox.Checked = false;
}
}
The only difference is that we want the Message box to be shwon only on the second click so when you will uncheck the checkbox.
Be careful though, if you change the default state of the checkbox, it will no longer work.
If you want a really robust solution Grant's one is IMHO the best, mine was just here to show you how to adapt your code for it to work

Just use a boolean variable as a flag.
private bool wasAlreadyClickedOnce;
private void uxCheckBoxMouseClick(object sender, MouseEventArgs e)
{
if (!wasAlreadyClickedOnce)
{
wasAlreadyClickedOnce = true;
return;
}
if (uxMouseCopyCheckBox.Checked)
{
MessageBox.Show("Test");
uxMouseCopyCheckBox.Checked = false;
}
}

Try using the Click event instead of CheckedChanged event to check or uncheck the CheckBox and then you can use the MouseClick event for other stuff.

Related

Reject Enter from activating the click action

I would like to help with my new clicker game that I'm working on and I've stumbled upon a problem with adding a value to the "playerPoints" which is from launch 0. You need to click a button which is called "button_click" which will add +1 (++) to your "playerPoints". But there is a bug when you click the button and then hold the enter button it will act like a little auto clicker which I don't want. Is there a way how to prevent the enter key to add value when it is pressed or held down? Thanks in exchange.
int playerPoints = 0;
public void button_click_Click(object sender, EventArgs e) // main click button
{
playerPoints++;
label_points.Text = playerPoints.ToString() + " BITS";
}
The problem has been solved by an great idea by: Dan Byström
If you don't want to make the button respond to keys like enter.
Use a simple image or label and link that function to the image_click or label_click
again thanks alot guys!
When you click on the subject Button, it becomes the form's ActiveControl. As part of the form's internal processing in setting the ActiveControl, the Form.UpdateDefaultButton Method is called.
Remarks
The UpdateDefaultButton method determines which button on the form raises its Click event when the user presses ENTER, according to the following priority:
To avoid having the subject button becoming the Default Button, override the form's UpdateDefaultButton method with something like this:
protected override void UpdateDefaultButton()
{
if (ActiveControl == button_click)
{
return;
}
else
{
base.UpdateDefaultButton();
}
}
Hook the KeyDown event instead of the Click event.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.keydown
Button.KeyDown += Button_KeyDown;
// Handle the KeyDown event
private void Button_KeyDown(object sender, KeyEventArgs e)
{
//increment your counter
}

C# WPF/XAML Preview Mouse Event for DataGrid

I'm using a WPF DataGrid with c#/xaml in Visual Studio 2013.
With SelectionMode="Extended", I'm able to multi-select rows in the grid.
I have a requirement where clicks on one of the columns of the grid are to be ignored relative to row selection.
I setup a PreviewMouseLeftButtonDown event that gets called.
Since it's a preview event, at the time of the event is processed, the selection in the grid hasn't changed yet.
I'm able to determine the row and column of the click, so I can determine a click has been made in a column that I don't want
I want to be able to abort the click event at that point so that no change is made to the current selected items in the grid. Is that possible?
In the mouse down event I tried something like:
private void GridCtrl_MouseDown(object sender, MouseButtonEventArgs e)
{
// ... Other code
e.Handled = true;
}
But, despite being marked as handled, it still continues and performs the row selection.
I also have a 'SelectionChanged' event that I see that it later gets into.
I think you actually need to handle both tunneling events - one for PreviewLeftMOuseButtonDown and another for PreviewSelectionChanged.
My advice is create a flag, let's call it:
bool _cancelSelectionChange = false;
Then, in your Mouse handler:
private void GridCtrl_MouseDown(object sender, MouseButtonEventArgs e)
{
_cancelSelectionChange = false;
// ... Other code
_cancelSelectionChange = true;
e.Handled = true;
}
Finally, in your selection change handler for the tunneling event:
private void GridCtrl_PreviewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
e.Handled = _cancelSelectionChange;
}

Setting myButton.Enabled = true causes radiobutton's CheckedChanged handler to fire

I have absolutely no programmatic links or properties set such that my CheckedChanged fires as a result of anything except checking the radio button.
However, when I click a different, unrelated button, the button's click handler fires (this is expected). In this click handler, the button disables itself (it re-enables on a different button's click), which then triggers myRadioButton_CheckedChanged handle for an unrelated radiobutton fires.
The call stack that I'm seeing is essentially
myRadioButton_CheckedChanged (...)
myButton_Click(...)
Main(...)
The line in myButton_Click that is triggering the myRadioButton_CheckedChanged is apparently
myButton.Enabled = false;
The related code is:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
// L-R
if (radioButton1.Checked == true)
{
orientation_left = 3;
pictureBox2.Invalidate();
Debug.Print("left {0}", orientation_left);
}
}
private void select1_Click(object sender, EventArgs e)
{
Debug.Print("select1click");
if (select1Down == false)
{
// ... stuff
select1.Enabled = false; // Causing the CheckedChanged to fire
select2.Enabled = false;
select1Down = true;
}
}
Ok, got it.
Check the TabOrder on your Button and RadioButton.
Seems that when you disable the Button, the focus is shifted to the next control, which is probably your RadioButton, causing it to become checked.
On my test From, all I had to do was to make sure that the RadioButton's TabOrder was not right after the Button.
Cheers
EDIT:
This seems to be a known problem as I just found this MSDN thread: http://social.msdn.microsoft.com/Forums/windows/en-US/77fbec3b-1f63-42e1-a200-19b261b63794/the-radiobutton-clicked-event-is-fired-without-the-radio-button-beeing-clicked-?forum=winforms
Okay, it's kinda hacky but it works without changing anything to the tab order:
private void select1_Click(object sender, EventArgs e)
{
if (!select1Down)
{
// ... stuff
SendKeys.SendWait("{Tab}");
select1.Enabled = false;
select2.Enabled = false;
select1Down = true;
}
}

How to detect if an event is sent by an argument

I don't know if it is called an argument (i.e. textbox1.text = "Hello";).
I have a control and there is a text box in it. It has a dropdown box that opens when the text is changed. But when I update the text in the text box that box drops down.
I need a way to make it so it only drops down if someone manually does it.
TBAddressBar.ABText.Text = getCurrentBrowser().Source.ToString();
and
public void ABText_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender == 1*)
{
ABDropDown.Visibility = Visibility.Visible;
}
else
{
ABDropDown.Visibility = Visibility.Collapsed;
}
}
If someone manually does it, presumably they are using keypresses to do so. In that case, use KeyDown or KeyUp events to show the dropdown instead.
What I have done in the past is use a boolean variable that I set when I update my textboxes programically to bypass the TextChangedEvent.
i.e.
bool loading;
....
loading =true;
TBAddressBar.ABText.Text = getCurrentBrowser().Source.ToString();
loading = false;
public void ABText_TextChanged(object sender, TextChangedEventArgs e)
{
if(loading) return;
....
}
Simple, just remove the code from your TextChanged Event.
Anyway you got the basic idea.. Now do your dropdown logic in KeyPress event, since it accepts only characters and not the modifiers. So it behaves closer to your requirement. Not that you cant handle the same using KeyDown and KeyUp, you can, but more code..

c# checkbox problem

I want to minimize the amount of code i have to write for this small problem. I have 1 textbox that has a relationship with 2 checkboxes as yes and no. The textbox on form load is set to disabled. When the yes checkbox is changed this event occurs -
private void checkYes1_CheckedChanged(object sender, EventArgs e)
{
textBox14.Enabled = true;
checkNo1_cbx.Checked = false;
}
and when the no checkbox is changed -
private void checkNo1_cbx_CheckedChanged(object sender, EventArgs e)
{
textBox14.Enabled = false;
checkYes1_cbx.Checked = false;
}
Although another problem is that i have to press yes twice to get it to check.
This is for a question on a form and so far it goes up to 11 questions and more will be added in the future. So my 2 problems so far is -
How can I fix the problem when the checkbox is changed I have to press it again to check it.
Is it possible to improve this code to minimize the amount of code i will have to write in the future.
private void checkYes1_CheckedChanged(object sender, EventArgs e)
{
OnCheck(true);
}
private void checkNo1_cbx_CheckedChanged(object sender, EventArgs e)
{
OnCheck(false);
}
private void OnCheck(bool yes)
{
textBox14.Enabled = yes;
checkNo1_cbx.CheckedChanged -= checkNo1_cbx_CheckedChanged;
checkNo2_cbx.CheckedChanged -= checkYes1_CheckedChanged;
checkNo1_cbx.Checked = !yes;
checkNo2_cbx.Checked = yes;
checkNo1_cbx.CheckedChanged += checkNo1_cbx_CheckedChanged;
checkNo2_cbx.CheckedChanged += checkYes1_CheckedChanged;
}
However consider using RadioBox instead of CheckBox because you want to if one being checked uncheck the other..
Edit: In your previous design, you get it wrong changed I have to press it again to check it because of you have two event handlers assigned to each of the check boxes. now at your code when the first one checked, you are disable the text box and make the other unchecked, but when you call the other unchecked Checked = false you are calling the second check box event handler also so it will enable the text and make the first one disable... you should remove the event handler by -= when updating at your code if you don't want the handler handler to be triggered again.. And that what I am doing in the code sample provided.
Why are you using 2 checkoboxes ? One checkbox (check1) would be enough:
private void check1_CheckedChanged(object sender, EventArgs e)
{
textBox14.Enabled = check1.Checked;
}
EDIT:
Assuming that each question mean 1 textbox, then you need 1 checkbox per textbox... this could be further improved by using a more complex approach
unless there is a reason that you are making a round trip back and forth to the server to disabled a textbox on a checkbox selection change, why don't you just do that all on the client side via javascript?
I agree with Yahia. If you do need to explicitly provide the two options though, then you should consider using RadioButtons.

Categories