I have a combobox on the winforms. On Enter even I open it:
cbo.DroppedDown = true;
But if I do that the combo opens and closes immediately. What am I doing wrong?
The reason why I use this event is I need to open combo on Tab, when user click tab on the previous control, this combo opens properly. But if user clicks the combo it opens and closes. How to do that properly?
Thanks
I tried it just like this:
private void comboBox1_enter(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
no changes to mouseup or timers. it behaved just as expected. Whether I selected the comboBox with a mouse click or tabbed into it the drop down list appeared and stayed open until I selected something.
I would look to see if there is something else pulling focus off the box.
The reason you are having this problem is because the mouseup event occurs after the enter event and the default window procedure is closing the combobox.
In the enter you could check the mouse button status and if the button is down, do not open the combo. Then have another event handler for the mouseup event to open the combo.
Another option is to set a timer for a few milliseconds, and open the combo when it goes off.
Knowing this is a bit old, but I found that this works well. You can TAB into the combo box and it opens and if you click the arrow it doesn't close back up.
private void ComboBox_Enter(object sender, EventArgs e)
{
if (MouseButtons == MouseButtons.None)
((System.Windows.Forms.ComboBox)sender).DroppedDown = true;
}
Set DroppedDown = true in GotFocus event of the combobox. Otherwise, the dropdown list will show at wrong location.
void cbo_GotFocus(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
cbo.DroppedDown = true;
}
I think you just need to focus it first before open the comboBox.
cbo.Focus();
cbo.DroppedDown = True
Hope it works for you.
Related
I have a ComboBox whose DropDownStyle is DropDown, allowing the user to type into it and its AutoCompleteMode is Suggest. The problem is that if the ComboBox is currently open and the user starts typing into it the auxiliary drop-down list appears but clicking on an item from it actually selects the item from the ComboBox's original drop-down list residing under the mouse at the time of click.
I would prefer if while the ComboBox's drop-down list is open the user could not type into it and would like to know if there is a more elegant solution than:
Setting the AutoCompleteMode to None when the ComboBox is open
Possibly changing the DropDownStyle to DropDownList on the OnClick event (haven't tried, but the theory is sound)
Manipulating (or restricting) the entered text while the list is open
Similar
As an option you can handle KeyPress event of the ComboBox and close the dropdown. It keeps the autocomplete menu open, but closes dropdown:
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
this.comboBox1.DroppedDown = false;
}
As another option, you can handle DropDown and DropDownClosed events of the ComboBox and disable autocomplete in DropDown and enable it again in DropDownClosed event:
private void comboBox1_DropDown(object sender, EventArgs e)
{
this.comboBox1.AutoCompleteMode = AutoCompleteMode.None;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
}
You can create a new class deriving from ComboBox and override corresponding OnXXXX methods and put the logic there. This way you encapsulate the fix in the control class instead of handling events in your form and will have a reusable bug-free control and a more clean code.
Is there a way to disable a user from clicking within a text box? I've tried .ReadOnly but this disables the user from typing in the text box. I want to be able to let the user type their name maybe "Peter" but disallow clicking back so they can't type over.
Use the MouseUp event:
void textBox1_MouseUp(object sender, MouseEventArgs e) {
textBox1.SelectionStart = textBox1.Text.Length;
}
If WPF you can bind Focusable and just return false once a value has been set. Or just explicitly call tbDisable.Focusable = False;
Another possibility is listening to the "Preview..." mouse events of the TextBox and setting e.Handled = true. In contrast to the accepted answer this will fully prevent mouse interactions - and not just hide the selection on MouseUp.
WPF's TextBox has a property named IsInactiveSelectionHighlightEnabled. I set this property to true in order to make a TextBox always show selection. However, it doesn't work in this case:
private void button_Click(object sender, RoutedEventArgs e) {
textBox.Select(0, 10);
}
I just want to see the selection after clicking the button. But selection will not appear until I right click the TextBox. Why? Am I miss something?
You should have the Keyboard focus on your textbox to select the text in it.
Add this code to your button click event before the selection.
Keyboard.Focus(textBox);
Hope it helps.
I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)
i have small problem with autocomplete option in combobox. Everything is working correct, except that i want to work it diffrent :)
When I start typing in combobox, autusuggest working the way i like :
But when i first open combobox, and then start typing i get something like that:
What's more i can't pick item from autosuggest combobox, only from this list under.
AutocompleteMode is SuggestAppend
I'd like to have autosuggest like on the first picture, and in situations like picture 2, this first combobox list should be closed somehow..
I had the same problem and solved it this way:
private void comboBox_DropDown(object sender, EventArgs e)
{
ComboBox cbo = (ComboBox)sender;
cbo.PreviewKeyDown += new PreviewKeyDownEventHandler(comboBox_PreviewKeyDown);
}
private void comboBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
ComboBox cbo = (ComboBox)sender;
cbo.PreviewKeyDown -= comboBox_PreviewKeyDown;
if (cbo.DroppedDown) cbo.Focus();
}
Once the user clicks on the DropDown button PreviewKeyDown event is attached to that ComboBox. When user starts typing, freshly added event is triggered. In that event we check if ComboBox is DroppedDown, if it is, focus that ComboBox. On ComboBox focus DropDown disappeares and that's it.
What about using the DropDown and DropDownClosed events to disable or change the auto-complete mode?
I was having exactly the same problem.
I tried the DropDown and DropDownClosed events to set the AutoCompleteMode property to none and suggest.
In this situation the SelectedIndexChanged event does not get fired after selecting an item with the mouse.
I was using the SelectedValue property in the SelectedIndexChanged event and this property is already changed at the moment the DropDownClosed event is triggered.
In my case I simply called the SelectedIndexChanged method from the DropDownClosed event to solve the problem.
Implement event on ComboBox KeyDown. It should look like this.
void cmbExample_KeyDown(object sender, KeyEventArgs e)
{
if ((sender as ComboBox).DroppedDown)
(sender as ComboBox).DroppedDown = false;
}
Have you tried the other possible values for AutoCompleteMode, which are Append, None, and Suggest? I think that what you are looking for is Suggest instead of AppendSuggest.
Here is some downloadable sample code illustrating the different modes, if you need it.
I also found the default UI implementation distracting as the two dropdowns fight for mouse control.
You want to hide the dropdown list when autocomplete suggestions are shown. There is a windows message that the combobox makes before showing the autocomplete suggestions. I chose to collapse the droplist in response to this message. It requires a small override of the combobox to achieve this:
Public Class Combobox2
Inherits ComboBox
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = 135 AndAlso DroppedDown Then 'WM_GETDLGCODE
DroppedDown = False
End If
MyBase.WndProc(m)
End Sub
End Class
void cmbExample_KeyDown(object sender, KeyEventArgs e)
{
cmbExample.DroppedDown = false;
}