Cancel dropdown behavior on click - c#

This may seem like a strange request, but I want to cancel the normal behavior of a dropdown control regarding opening or closing the list. I have several dropdowns on a form and it seemed tedious to have to hit the arrow exactly to open the list. I added code in the click event handler to open or close the list depending on its current state:
private void customerCodeComboBox_Click(object sender, EventArgs e)
{
if (customerCodeComboBox.DroppedDown == false)
customerCodeComboBox.DroppedDown = true;
else
customerCodeComboBox.DroppedDown = false;
}
This works just fine unless you actually click on the dropdown arrow. Then it opens and closes immediately since it fires the normal open and then my code closes it! :(
Is there a way to cancel the normal behavior? e.Cancel was not available in the EventArgs. Is there a better way to open the list when a mouse clicks anywhere in the field?

Related

Detecting which button was clicked when closing a winform?

I have this event handler in my winform:
private void SaveToDXF_FormClosing(object sender, FormClosingEventArgs e)
{
// Make sure the user has selected at-least one layer
if (listBoxLayers.SelectedItems.Count == 0)
{
_AcAp.Application.ShowAlertDialog("Please select one or more layers.");
e.Cancel = true;
}
}
Originally I was using the OK button click handler but I quickly found out that there did not seem to be a way to cancel out of actually closing the form. Then I read on SO a suggestion to use FormClosing. This works fine but ...
If the user closing the form by pressing the Cancel button this event still fires. That makes sense. But I only want to perform this validation check and cancel closing the form if they click the OK button.
How do we do this?
Sounds like you can check the this.DialogResult in the closing handler: it will be different depending which button was clicked (whatever you set as the DialogResult in the properties grid).
e.g. "don't do the check if they are cancelling" could be like
if (this.DialogResult != DialogResult.Cancel && listBoxLayers.SelectedItems.Count == 0)
If you have more checks to do it might be simple to just "if cancelling then return" as the event handler first line

Activate Click and MouseDown events at the same time?

I am trying to create a form where the user selects one of 2 radio buttons, "fast" and "slow", then presses a "go" button on the form. It should work as follows:
When "fast" is selected and "go" is pressed, the user needs to continue holding down the button in order to make the player move and the player stops when the user releases their finger from the button. For this, I am using the MouseDown and MouseUp events.
If "slow" is selected, the user can only move one step at a time, regardless of how long the button is held down. for this, I am using the Click event.
To test this concept, I put a MessageBox in the Click and MouseDown events to see how they work together. When I click on the button, however, I am only seeing the Mousedown event get triggered, regardless of which radio button is selected and the Click event is only triggered when the go button is in focus and I press enter.
How can I make it so that both events get called simultaneously (and then I can put the respective if statement in to differentiate between "fast" and "slow" radio buttons)? (The button is wired up to these events by double-clicking on their respective handler in the design window, not hardcoded).
private void go_button_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Mousedown"); // Doesn't show in console window in Release mode, still trying to solve this
MessageBox.Show("Mosuedown");
}
private void go_button_Click(object sender, EventArgs e)
{
Console.Out.WriteLine("go_buttonclick"); // Doesn't show in console window in Release mode, still trying to solve this
Console.WriteLine("go_buttonclick"); // Doesn't show in console window in Release mode, still trying to solve this
MessageBox.Show("go_buttonclick");
}
I found what I was doing wrong, both events are being triggered at the same time, but the MessageBox in the MouseDown event wasn't allowing me to see the Click and MouseUp events also being triggered. When I turned off the MouseDown MessageBox, the other 2 MessageBoxes appeared, thanks!

On leaving a control, how can I give that control focus again?

I've got TextBoxes in a C# form. The user enters data, and then when they leave the control (almost always by hitting Tab), I check the data to make sure it's valid. If it is invalid, I want to highlight their text so they can immediately fix it rather than having to click it.
Right now, on Control.Leave, I validate their entry. This works just fine. However, since they hit Tab, right after they dismiss the error message, it goes on to the next object, even though I've got ((TextBox)sender).Focus();
How can I have the above line fire after the form Tabs to the next control.
You may want to look into Control.CausesValidation property
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.causesvalidation(v=vs.110).aspx
You can validate the control prior to the user leaving focus rather than waiting on Focus moving itself.
And here's MSDN documentation for Control.Validating event, does a good job at laying out the sequence of events when gaining / losing focus of a Control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx
Notice how Control.Validating and Control.Validated are launched prior to Control.LostFocus. You can perform your validation step prior to allowing the user to lose focus of your Textbox.
There's also a pretty good previous answer on stackoverflow.com which outlines how to do this: C# Validating input for textbox on winforms
If you handle the Control.Validating event, setting e.Cancel to true will stop the change of focus from occurring.
Note that this method will also stop buttons from working, so you may need to set Control.CausesValidation to false on certain buttons.
You will also need the following snippet on the main form to allow the close button to work:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
base.OnFormClosing(e);
}
Try using the LostFocus event on the TextBox to Focus it again

Close ComboBox DropDown on mouse leave event

While developing a simple Windows Form UI applications, I am trying to create an effect to show and close dropdown on mouse events.
Like I can open the dropdown on MouseMove event by setting comboBox.DroppedDown = true;
However, this same is not working when I set comboBox.DroppedDown = false; on MouseLeave event to close it.
No idea what exactly is needs to be done here.
The problem is on MouseLeave the dropdown does not lose focus and hence unless you select one item from list, it does not close. It waits for user to select an item from list.
If it can lose focus on MouseLeave, would work.
Any suggestions please.
First of all I must say that I am not an experienced programmer and I just started with WPF.
I know this question is two years old but I had the same issue and I found I can close the drop down list of the ComboBox using the event IsMouseDirectlyOverChanged. What was really annoying for me was that I had a ComboBox and a button, and If the drop down menu was opened without making a selection and I wanted to click the button, nothing happens at the first click because at the first click the drop down menu was closing. After this I could click on the button.
For me it's working fine: the drop down list close if I move the mouse in any direction (up, left, down, right) and a message is append to a textbox control. I don't know if this event is something new or it could be used 2 years ago too.
private void comPortList_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (comPortList.IsDropDownOpen==true)
{
txtMsgBox.AppendText("MouseDirectlyOverChanged\n");
txtMsgBox.ScrollToEnd();
comPortList.IsDropDownOpen = false;
}
}
This event triggers when your mouse pointer is over the opened ComboBox. If you don't open the drop down list, it will not trigger.
Another thing that I've seen is that this event triggers when you enter over the opened ComboBox and also when you leave it. If I append the text before checking if the IsDropDownOpen property is true, the text "MouseDirectlyOverChanged" will appear twice in my textbox when I the mouse pointer leaves the oppened ComboBox.
If i comment the line:
comPortList.IsDropDownOpen = false;
and leave the AppendText and ScrollToEnd before if, the text will append only once.
I hope this helps :)
It sounds to me like you need to be using the MouseEnter event and not MouseMove. The reason it wouldn't work on MouseLeave is because your mouse is moving, and that will just set it to true again.

How to keep the focus back to form when clicking on OK button in windows forms?

I have a windows form with multiple controls and OK and Cancel buttons. In the Click event handler of OK button, it checks to see if there are errors on the form. I need the form to stay open if there are errors. But the code below is not working. If there are errors, it is closing the form and returning to the caller. And I have the below two lines to invoke and show the form.
PtForm paymentForm = new PtForm();
ptForm.ShowDialog();
private void btnOk_Click(object sender, EventArgs e)
{
this.ValidateChildren(ValidationConstraints.Visible);
string ErrorString = GetValidationErrors();
MessageBox.Show(ErrorString, "Errors");
if (!string.IsNullOrEmpty(ErrorString))
{
return;
}
//Processing
}
Thanks for any help.
There's nothing in this code that will close the form. Therefore, the culprit must be outside this code.
Did you set your OK button's DialogResult property to DialogResult.OK? That would explain why the form is closing. AFAIK, if you set DialogResult on the button, that's what happens -- there's no way to veto it in code.
So in that case, you would need to go back to the designer and set the button's DialogResult back to None. Then at the end of your btnOk_Click method, once you've validated all the input and decided that it's safe to close the dialog, add a line that sets your Form's DialogResult property to OK.
Remove DialogResult property of a button-i.e. set it to None.

Categories