RichTextBox and tab key - c#

I created a richTextBox and i noticed that when I press Tab key it is not doing anything.
It is suppose to do some space but it do not.
How can i access it?

By default pressing tab moves focus to the next control. Setting AcceptsTab property of the RichTextBox to true allows the RichTextBox to accept tab characters.
See this MSDN article on the AcceptsTab property.

First you need to set following properties of Richtextbox
richTextBox.Multiline = true;
richTextBox.AcceptsTab = true;
And in the keypress event of richtextbox you need to do following
if (e.KeyChar == 9)
{
e.Handled = false;
}

Related

Disable Text Box Selection or Clicking

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.

How can i totally disable tabbing on DataGridView but keep ability to select rows?

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.

Inserting a TAB space into a TextBox

I have seen few tutorials that claim to solve this issue online but they do not work. I would like to insert a TAB space when the TAB key is pressed, into my multiline TextBox.
A dudes response from Microsoft was that, by design, Metro apps will bring focus to the next control if you press TAB inside a TextBox. Now, this would make sense, if you were pressing TAB on a Single-line TextBox. But in a multiline TextBox? Don't you think it's more likely that the user will want to insert a TAB?
And yes, I know, you can insert a TAB space in a Metro TextBox by pressing Ctrl+TAB. But that is error prone, since most of us are used to just pressing TAB, and old habbits die hard sometimes.
Here is my issue. I have a text editor feature of my app where the user may need to enter large amounts of data. And you know what people are like, they like to separate things to make their text documents more readable and it's very uncomfortable and more tedious to use Ctrl+TAB. So I would like to know if anybody can help with a workaround for this (it can't involve a RichTextBox, though)?
Also, if I manage to find a workaround, will this increase the chances of my app release being rejected by the Store?
Subscribe to the KeyPress event of your TextBox, capture the Tab key by inspecting the KeyCode of the pressed key, and then set the Handled property of the KeyEventArgs to true so the key isn't passed onto any other controls.
Use SendKeys to send a "Tab" character to the TextBox to mimic the behavior of pressing "Ctrl+Tab", like you said:
TextBox_KeyPress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
e.Handled = true;
SendKeys(^{TAB});
}
}
The carrot (^) represents the CTRL key.
richTextBox1.AcceptsTab = true;
in your KeyPress event of your textbox control. Make sure that you have the property set true for multiline on the textbox control
This would work if you are using a RichText Control which is what I would suggest
if (e.Key == Windows.System.VirtualKey.Tab)
{
e.Handled = true;
string SelectionText = "";
TextBox.Document.Selection.GetText(Windows.UI.Text.TextGetOptions.None, SelectionText);
TextBox.Document.Selection.TypeText(char(9) + SelectionText);
}

How to Add shortcut key to checkbox without text

I want to add shortcut key to checkbox. Checkbox do not have text. I have label and then Checkbox. Label have shortcut key for ex. &Visible. So, Label have V as shortcut key. If someone press Alt+V then chechbox should change from selected to not selected state and same in opposite manner.
You can check it like this refer the following code part.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// look for the expected key
if (keyData == Keys.Alt && keyData == Keys.V)
{
checkBox1.Checked = true;
return true;
}
else
{
checkBox1.Checked = false;
return false;
}
}
Label controls are special with respect to keyboard mnemonics. Since labels can't ever get the focus, whenever you attempt to set the focus to a label using its keyboard mnemonic, the label sets the focus to the very next control in the tab order.
This is intended for use with textboxes and comboboxes, which don't have any built-in facility for displaying a label (in contrast to the check box and option button controls). To set up a mnemonic for these controls, you position a label next to them, set a mnemonic for it, and ensure that it comes right before the textbox in the tab order. That way, when the user activates the keyboard mnemonic for the label, it automatically sets focus to the textbox control. You've seen this all over the place in Windows:
Well, you can do exactly the same thing with a checkbox control if you must (though I'm really not sure why you'd want to). Set the mnemonic for the label (&Visible), and then position the label next to the checkbox that you want it to work with. Use the TabIndex configuration options in the Visual Studio IDE to ensure that if the label has tab index n, the checkbox control has tab index n+1.
There's no need to override ProcessCmdKey or anything else difficult.

RichTextBox equivalent of TextBox.AcceptsReturn

I am switching several TextBoxes out for RichTextBoxes to gain some of the cool features.
I had my TextBoxes configured to AcceptReturn so that the enter key will create a new line, rather than leave the control. The RichTextBox does not seem to have this feature.
Is there a simple way to do this, or do I have to capture all keypresses and handle them individually?
Note: This issue occurs only when you set the "AcceptButton" property of the form.
Set the RichTextBox.AcceptsTab to true. For some reason this works for both tabs and the enter key. If you only want the enter keys then you will have to write custom code.
Since Carter pointed out that this only applies if AcceptButton is set, and the other solution suggests deriving the RichTextBox class, I found another simple solution. Just unset AcceptButton for the time that the/a RichTextBox has the focus. Here's a sample code:
private void RichText_Enter(object sender, EventArgs e)
{
AcceptButton = null;
}
private void RichText_Leave(object sender, EventArgs e)
{
AcceptButton = OKActionButton;
}
This assumes that you only have a single AcceptButton and that is unlikely to change. Otherwise you would have to copy some AcceptButton finding logic here or just backup the previous AcceptButton value before setting it to null.
This solution also has the side effect of removing the default border from the actual accept button, indicating to the user that pressing the Enter key now will not activate that button.
The solution is to override IsInputKey:
protected override bool IsInputKey(Keys keyData)
{
if (
(keyData & ~Keys.Modifiers) == Keys.Tab &&
(keyData & (Keys.Control | Keys.Alt)) == 0
)
return false;
return base.IsInputKey(keyData);
}
After setting AcceptsTab to true, you ensure that the RichTextBox processes both the tab and return key. With the IsInputKey implementation above, we ensure that the Tab and Shift+Tab key never reach the RichTextBox so they are used for navigation instead.
The above override must be pasted in a class derived from RichTextBox.
Just change Accept option in Richtextbox Property turn to "true" it will work like a magic

Categories

Resources