I have a combobox with the the property DropDownList that runs some code on SelectionChangeCommitted event. It works fine with mouse-click but if I try to select using arrow keys it fires after the first key press. How do I get this to work as a standard drop-down that I can navigate with mouse and keys?
private void dd_jobs_SelectionChangeCommitted(object sender, EventArgs e)
{
Pk_Error p = new Pk_Error(ref_num, j[dd_jobs.SelectedIndex]);
p.Show(); p.BringToFront();
this.Close();
}
I had the same issue to adhere to 508 compliance. I had to implement a combobox that inherits from the windows one and override the functionality.
For you capture the selected text in the OnEnter call and use that during the OnLeave to make the change. Override the OnSelectionChangeCommitted to check if the list is dropped and not make a selection.
if(this.DroppedDown == true)
{
base.OnSelectionChangeCommitted (e);
sCurrentItem = this.Text;
}
Related
I have a readonly DataGrid showing items inside a window that is shown as dialog. This dialog also has two buttons, one to confirm and one to cancel the dialog. The confirm button's IsDefault property is set to true, the cancel button's IsCancel property is set to true. I want to keep up the default dialog button behaviour, i.e. confirming and closing the dialog when pressing the confirm button or ENTER or cancelling and closing it by pressing the cancel button or ESCAPE.
Unfortunately the DataGrid consumes the KeyDown event of the ENTER-key and selects the next row inside the grid. Because the KeyDown event is marked as handled by the DataGrid the dialog will not be confirmed and stays open. My desired behaviour is that the selected row is not changed and the dialog will be confirmed and closed.
I could create my own DataGrid-derived type an overwrite OnKeyDown as follows:
public class MyDataGrid : DataGrid
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key != Key.Enter)
{
base.OnKeyDown(e);
}
}
}
But I was wondering if there is another way without having to create a derived type to achieve my goal.
You can use the curernt DataGrid.
<DataGrid PreviewKeyDown="DataGrid_PreviewKeyDown"/>
private void DataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// This makes this key pressed event end here.
e.Handled = true;
// Do what you need to do here...
}
}
I want to show the list of items in a combo box when the user selects the text. I have a touch screen application and it's very difficult to hit the dropdown arrow so I figure I'd show the menu when the text is selected which is often what gets touched. I'm using VS 2008. and suggestions for a touch friendly numeric up down solution in VS2008?
You could use the ComboBox.Click event handler and the ComboBox.DroppedDown property and do something like this:
private void ComboBox1_Click(System.Object sender, System.EventArgs e)
{
ComboBox1.DroppedDown = true;
}
You could also use the same event handler for a numericUpDown and use the mouseposition as well as the position and height of the NumericUpDown to get whether or not the click was above or below the halfway-line of the control by doing something like this (not sure if my math here is perfect, but it worked when I tested it):
if ((MousePosition.Y - this.PointToScreen(NumericUpDown1.Location).Y < NumericUpDown1.Height / 2))
{
NumericUpDown1.Value += 1;
}
else
{
NumericUpDown1.Value -= 1;
}
HTH
I was working on a similar situation. We wanted to make the text area behave the same as the button on the right. (IE the user clicks and gets the drop down box)
davidsbro is similar to what I ended up doing, but we wanted it to close if they clicked again, so the value became dropDown.DroppedDown = !dropDown.DroppedDown;.
The issue with this is that if the user clicks the right button of the drop down box, the dialog box opens, then calls the onClick event.
I solved this situation by tracking the original state via the onmouseover event. If the value has changed, we have to assume that the button on the select box handled the click already.
private bool cbDropDownState = false;
private void dropDown_MouseEnter(object sender, EventArgs e)
{
cbDropDownState = dropDown.DroppedDown;
}
private void dropDown_Click(object sender, EventArgs e)
{
if (dropDown.DroppedDown == cbDropDownState )
dropDown.DroppedDown = !dropDown.DroppedDown;
}
I have a tree view on the left side. Selecting a node displays relevant information in a form on the right side.
Would I be able to keep the tree and any one control (textbox, combobox, checkbox) on the right in focus at the same time? This will enable a user to select a field, make a change, select another node, and without having to go back and select the same field again, just type and change the value of the same field.
Thanx.
EDIT
I suppose one could implement such behaviour manually:
private Control __cFocus;
private void {anyControl}_Focus(object sender, EventArgs e)
{
__cFocus = (Control)sender;
}
private void treeView1_AfterSelect(object sender, EventArgs e)
{
__cFocus.Focus();
}
I was just wondering if there exists an automatic / more elegant solution
EDIT 2
Ok, so it seems I'll have to implement it manually. Manual implementation it is then. However, now there seem to be another problem; not sure if I should ask this as a separate question.
When selecting a node the textbox gains focus as intended, but only when using the keyboard. It doesn't work when selecting a node with the mouse. First I thought that it might be a mouse event that's interfering, but stepping revealed that the MouseUp event fired first and then the AfterSelect event which sets the focus, so I don't think it's interfering. The textbox's Enter event is also fired, but for some reason it loses focus again to the tree.
Thanx
no, you cannot keep two controls in focus at the same time. But what you can do is set the focus to the target control in the treeview AfterSelect event
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Focus();
textBox1.SelectAll();
}
then in your textbox leave, save the changes, like so:
private void textBox1_Leave(object sender, EventArgs e)
{
//save changes here
}
this way, everytime you select an item in the treeview, check your textbox for change and save as needed, then you will refocus on the textbox for your next edit
There only can be one element having the focus!
But I have an idea for you that might solve your problem. Assuming you have a window with a TreeView and a TextBox. Set the HideSelection property of the TreeView to false and subscribe the AfterSelect event (like edeperson already answered) like this:
private void OnTreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
textBox1.Text = e.Node.Text;
textBox1.Focus();
}
Then subscribe the KeyDown event of the TextBox and do following in the event method:
private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode == Keys.Up) || (e.KeyCode == Keys.Down))
{
treeView1.Focus();
SendKeys.Send(e.KeyCode == Keys.Up ? "{UP}" : "{DOWN}");
}
}
At last subscribe the Leave event of the TextBox and do following in the event method:
private void OnTextBoxLeave(object sender, EventArgs e)
{
if (treeView1.SelectedNode != null)
{
treeView1.SelectedNode.Text = textBox1.Text;
}
}
And, voilá it should work like you expected it...
If you want to focus on it , you can use usercontrol. you can put your textbox on usercontrol and set focus of this textbox on usercontrol using set properties on treeview select.
No you may not, only one control may be in focus at any given time.
See Moonlight's comment for one way to achieve the behavior that you seek.
In c#, I want to select combo box items by keyboard and when i press enter after selecting one that item should be selected. how to do it?
try something like this ..., this will explain how to change the items using mouse and key board ,....
I found this method worked fine in all the conditions. But I m not sure if anything more accurate than this method is available.
bool IsMouse = false;
private void cmbMy_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (IsMouse)
{
//Write the logic if selection is changed by mouse
}
else
{
//Write the logic if selection is changed by keyboard
}
IsMouse = false;
}
private void cmbMy_IsMouseCapturedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
IsMouse = true;
}
In isMouseCapturedChanged event of combo box i made a bool variable true and when selection changed of the combo box i m checking the bool doing the required task and then setting isMouse to false.
Or you need to bulid your own custom combobox ..
You need build a custom ComboBox class and override the Control.ProcessKeyEventArgs Method.
I am working on a silverlight app that you need to enter information into a textbox and then just hit enter. Well there is no onclick event, that I could find, so what I did was use the onkeypressup event and check if it was the enter key that was pressed if so do "blah".
It just feels like there is a better way to do this. So the question is, is there?
I thinks that's the way to catch Key.Enter.
Also, you're code will be more readable if you use the KeyDown event instead of the KeyUp event.
If you only care about catching Key.Enter for a single control then your approach is correct.
You can also catch the Key.Enter for a group of related controls by using the KeyDown event of their container ("Event Bubbling").
Do you really want it in the textbox? I would put a onkeyup handler on the container (e.g. Grid, Canvas) to press the button anywhere on the form.
This will work if you use want to bind the Command property instead of using the Click event. Start by creating an event handler for Click (see below) and then in the KeyUp do:
private void MyTextBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter) SomeButton_Click(this, null);
}
private void SomeButton_Click(object sender, RoutedEventArgs e)
{
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
{
cmd.Execute(null);
}
}
I use the following implementation when using the command pattern:
private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
BindingExpression b = MyTextBox.GetBindingExpression(TextBox.TextProperty);
if (b != null)
b.UpdateSource();
ICommand cmd = SomeButton.Command;
if (cmd.CanExecute(null))
cmd.Execute(null);
}
}
When you press Enter, the data source of the textbox is not updated and the command uses an old value. Therefore you have to call UpdateSource before executing the command.
Of course you can catch the event on a higher level than the textbox.
Well, Im preaty new to Silverlight and I created HitEnter beahaviour for button which have one DependencyProperty Button.
And I manulay wire up Button and Behavior (in code behind) and then when enter is hit I inovke the command on the button.