I have three TextBoxes, one Button and a DataGrid in my WPF Window
I want the user to be able to circle between textBoxes and the Button with Tab
The problem is is seems to stop on the dataGrid as well
I've tried setting the IsTabStop property to False, KeyboardNavigation.TabNavigation="None" and even setting the Focusable property to False, but it still focuses the datagird
To be more accurate, I tried to find out which item it is focusing with tab after textbox, and wrote this:
private void MyWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Tab)
{
System.Windows.Forms.MessageBox.Show(FocusManager.GetFocusedElement(this).ToString());
}
}
And after going over textBoxes, it showed: System.Windows.Controls.DataGrid Items.Count = 13
How can I disable tabstop for my dataGrid?
Related
I have two group boxes, in the first group box I have 3 textboxes and in the second group box I have 1 textbox. I added this code:
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyData == Keys.Enter))
{
SelectNextControl(ActiveControl, true, true, true, true);
}
}
But enter only works in the first group box and jumps over the button and skips the 2nd group box.
What should i do?
As the MSDN mentioned:
The SelectNextControl method activates the next control in the tab order if the control's Selectable style bit is set to true in ControlStyles, it is contained in another control, and all its parent controls are both visible and enabled.
You can find your Controls (Textbox's) tab order number on the Property TabIndex in the Designer.
I have a TableLayoutPanel with a number of TextBoxes and GroupBoxes. I have set this TableLayoutPanel.Enabled = false.This Disables all the TextBoxes and Groupboxes and the Scrollbar. Is their anyway i can enable the scrollbar even if the TableLayouPanel.Enabled = false?
To achieve this automatically, you can subscribe to the EnabledChanged event of your TableLayoutPanel. You can subscribe to the event using the designer, or with the following line of code:
tableLayoutPanel.EnabledChanged += tableLayoutPanel_EnabledChanged;
Then, from the event handler, we can simply set the enabled property of the scroll bar to match the enabled property value of the TableLayoutPanel:
private void tableLayoutPanel_EnabledChanged(object sender, EventArgs e)
{
scrollbar.Enabled = tableLayoutPanel.Enabled;
}
Now, whenever the enabled state of the TableLayoutPanel changes, the scroll bar enabled state will be updated to match.
A am writing a WinRT application which utilises a Gridview to display some data. The Gridview has a SelectionMode of Extended so that as the user navigates the grid with the cursor keys the selected item moves with them (plus I have multi-select functionality)
The problem I'm experiencing is that if you navigate the grid using the cursor keys and have Ctrl pressed down, the selected item remains where is was and only the focus changes. My DataTemplate doesn't show the focused item so it's quite confusing to the user.
Is there anyway I can change this behaviour so that navigating the grid with Ctrl held down works in the same way as if it wasn't being held down?
The solution was quite simple in the end. Just create a GotFocus handler like this one:
private void SdxGridView_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is GridViewItem && !((GridViewItem)e.OriginalSource).IsSelected)
{
SelectedItems.Clear();
((GridViewItem)e.OriginalSource).IsSelected = true;
}
}
I have a small window with 2 textboxes in a grid databinded to some properties, it is called from context menu of another window. I made one of textboxes focused after appearing by
<Grid FocusManager.FocusedElement="{Binding ElementName=priceBox}">
I would like to have behavior that Text in TextBox would be selected (dark blue background) so if I start type new symbols old ones being immediately deleted. I don't want to delete old symbols first. Same behavior I would like to have after I press Tab to switch to next textbox.
Is there any textbox settings to achieve this?
I have very old winforms applications and It looks like it was behaving this way by default.
You will have to set Keyboard focus on the TextBox before selecting the text
e.g:
private void SelectAllText(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
{
Keyboard.Focus(textBox);
textBox.SelectAll();
}
}
How can I totally disable tabbing on DataGridView so it won't go through cells at all?
I use DataGridView as music playlist in my application and I don't need that annoying windows default selection frame around cells. I want be able to select rows normally. I managed to hide selection border on buttons with SetStyle(ControlStyles.Selectable, false) but this does not disable tabbing on DataGridView.
Handle the KeyDown event of the DataGridView and call the parent (or grandparent) control's SelectNextControl method.
private void dataGridView1_KeyDown( object sender, KeyEventArgs e )
{
if ( e.KeyCode == Keys.Tab )
{
SelectNextControl( dataGridView1, true, true, true, true );
// or Parent.SelectNextControl() if the grid is an only child, etc.
e.Handled = true;
}
}
This will cause the whole grid to behave like tabbing among text boxes and buttons - you tab into the grid, and another press of the tab key will tab out and onto the next control. This retains navigation within the grid by the cursor keys. Refer to the linked MSDN documentation for options on the direction of tabbing, etc., which are what all those terrible Boolean parameters configure. The first parameter sets which control the "next" tab search begins from, so you can set that to a parent or sibling or grandparent.
if you want to DataGrid don't focus, you can set it's Enable property to false, this control on the form doesn't get focus, but in this way you must add or delete rows in DataGridView with specific button (it means a button for add and another for delete)
but if you want their cells don't focus, you should following this: in KeyDown event of your form, type this code
if (e.KeyCode == Keys.Tab)
{
dgvMain.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
[other component of your form like a textbox or a button].Focus();
}
with this, your DataGridView only highlight the whole selected row
OK. I've managed to do that. This ARTICLE helped me a lot. I used form's OnActivated and OnDeactivated events to disable and enable TAB key. Here you have sample code:
protected override void OnActivated(EventArgs e) {
ProcessModule objCurrentModule = Process.GetCurrentProcess().MainModule;
objKeyboardProcess = new LowLevelKeyboardProc(captureKey);
ptrHook = SetWindowsHookEx(13, objKeyboardProcess, GetModuleHandle(objCurrentModule.ModuleName), 0);
base.OnActivated(e);
}
protected override void OnDeactivate(EventArgs e) {
UnhookWindowsHookEx(ptrHook);
objKeyboardProcess = null;
ptrHook = IntPtr.Zero;
base.OnDeactivate(e);
}
There were a couple of problems that came up while i was trying to make it work but that's different story. Happy coding! :)
You can also set:
dataGridView.TabStop=false;
This will skip the grid when the tab button is hit.