Inserting a TAB space into a TextBox - c#

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);
}

Related

C# WPF backspace event?

I'm new to WPF and C# so what I'm asking is if there is a backspace event like TextChanged event for TextBoxes?
I made a small Library program with renting books and everything is viewed at a ListView.
What I currently did is that you can filter book names just by typing inside the textbox, so if you have 1000 books and you type the letter 'b' then you might have only 150 books starting with 'b'.
The problem is whenever i press backspace, I want it to previously restore it to what it was.
For example: typing "bob" and then deleted b, I get bo and now i want to present what every starts with "bo".
Now I get the idea. All I need is just another textChanged event. but something need to inform that the text was changed, and I need something better then
if (backspace key is pressed) { Invoke textChanged }
Thx guys!
Well, should i delete the post? maybe some one else will search it someday.
backspace is actually causing a TextChanged event automatically! damn. thx anyway!
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown(v=vs.110).aspx
Here is a reference on the msdn site.
http://csharp.net-informations.com/gui/key-press-cs.htm
On a different site (easier to read) This though looks like it is for Win Forms.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
MessageBox.Show("Enter key pressed");
}
if (e.KeyChar == 13)
{
MessageBox.Show("Enter key pressed");
}
}
Looks like you need to create an event that fires on a key down, then get the value of that key. I think there is a Keys.Backspace but to know for sure let intellisense help you.

Omit action when pressing Enter on button

Is there a way to omit the (whatever) performed action on a button when the key is pressed? I already got some good inputs from SO and tried to catch the event via OnKeyPress or OnKeyDown - but I could not omit the Enter-action. I also liked the idea of disabling the Tabstop property, but as soon as I click the button the problem reoccurs.
OnKeyDown only seems to fire when "casual" keys (like a or z) are pressed, but not when Enter is hit. How can I detect it - when Enter is hit - on the button itself?
When you press ENTER inside a non-multiline field on a form with a default button set, the click event for this button is fired, like a click, not like a keyevent (keypress, keyup, keydown, etc).
Form.AcceptButton
Gets or sets the button on the form that is clicked when the user
presses the ENTER key.
If you don't want this behaviour (or want to prevent it), maybe you should remove it as the default button.
But if you need it to be default only in certain situations, you can bind/unbind it programatically:
form1.AcceptButton = btnSave;
form1.AcceptButton = null;
You can use : if(e.KeyCode == Keys.Enter){}
Check the Button.IsDefault property
Well, the fact that
When you press ENTER [...] the click event for this button is fired
made me think about the use of the OnMouseUp(MouseEventArgs e) method instead of OnClick(EventArgs e). I gave it a shot and it totally fits my requirements - plus: it gives me the opportunity to only exclude the wrapped buttons from said issue, not involving any other controls in my application (like Form.AcceptButton).
Thanks for all your input, which made me think about the issue from another perspective!

Allow button click even if validation on another control fails

I have an interesting problem that I don't know how to solve. I wrote a form which does a password change. The form displays the current password too in a read-only TextBox (not for validation purposes; this isn't important in this case.) Each password TextBox has a button in it that when clicked, masks or unmasks the password (replacing the password characters with bullets and vice versa.) Here's an image of the whole thing:
Notice how the mask/unmask buttons are inside the text boxes, not outside of them. The buttons have been placed inside the text boxes with:
var button = new Button();
button.Width = 20;
button.Cursor = Cursors.Default;
button.FlatStyle = FlatStyle.Flat;
button.Image = SystemIcons.Shield.ToBitmap();
button.Dock = DockStyle.Right;
button.CausesValidation = false;
textBox.Controls.Add(button);
The last TextBox has validation enabled. Now the problem is, that the user is unable to click the passwork unmask button on the other text box, because the validation event fails. Thus, the user is unable to see the current password without entering a new one.
I need a way to have the password mask/unmask button be clickable even if validation is failing in the text box. I can't think of anything. Moving those buttons outside the text boxes is not an option.
The unmask buttons themselves, as well as the parents of the text boxes, all have CausesValidation set to false. Only the text boxes themselves have it set to true.
This is a .NET 2.0 C# project in Visual Studio 2010.
It's not easy to do it with the Validating event of the text box. The click event will not even reach the button when the text box loses focus. I'm thinking that you could either create your own TextBox (by extending TextBox or TextBoxBase) and hack the validation behavior there or override the form's DefWndProc and catch the mouse events + associated info (coordinates) there and still dispatch them (could get ugly) when validation fails.
One easy way out is to not rely on the Validating event anymore. Instead do the validation in the Leave event of the text box and if it fails just mark the text box as such. The user will still see there is a problem.
One more thing you need to take care of is the OK button of the dialog. You need to make sure the user won't be able to close the dialog if there are unvalidated controls on the form. Since you don't have validation support anymore, maybe you can use the Tag property to store a False (for example) when the data is not valid. On OK just iterate over all textboxes and check their tags.
The behavior for the end user will ultimately be the same, you just have to write a bit more code.
Well, I found a way where I can keep using the Validating event and still be able to click the buttons in the other text boxes. It turns out that the Button.MouseUp event is sent even if a button doesn't have focus.
So the solution is to handle both the MouseUp as well as the Click event and perform the password masking/unmasking depending on whether the text box that contains the button currently has keyboard focus or not:
button.MouseUp += (sender, e) =>
{
if (button.Parent.ContainsFocus || e.Button != MouseButtons.Left
|| !button.ClientRectangle.Contains(e.Location))
{
return;
}
textBox.UseSystemPasswordChar = !textBox.UseSystemPasswordChar;
};
button.Click += delegate
{
if (!button.Parent.ContainsFocus) {
return;
}
textBox.UseSystemPasswordChar = !textBox.UseSystemPasswordChar;
};
We do want to handle both events, because if we only handle MouseUp, then clicking the buttons with the keyboard (Tab to switch to the button, and Enter or Space to click it) would not work anymore.

Textbox issue in windows phone 7?

In my app,I just have a page with four text boxes, so when i click a text box soft keyboard appears, now when i want to move to next textbox then i have to tap outside the textbox to make the keyboard disappear and then click on another text box. I don't think it is user friendly, so i have two options,
1)To change the functionality of return button(to make it work as tab).
2)To reduce the frame size and so scrolling will be enabled.
How can I do the foretold two options in windows phone 7??
for the first option
Make return key of the input panel work like tab key.
make key down event of 1st textbox like this
private void txt1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Enter)
{
txt2.Focus();
}
}
similarly make this event for txt2 and txt3 and give focus accordingly and on on txt4 keydown event focus the main grid.
and about the 2nd way. Its a big problem in wp according to my knowledge.
For moving to next textbox #Amu 's answer will work perfect, and to dismiss the keyboard,
if (e.Key == System.Windows.Input.Key.Enter)
{
this.Focus();
}
That will take the focus away from your text box and will bring it to your screen.
And So keyboard will disappear!

How do you detect a square bracket in a KeyDown or KeyUp WPF event?

I'd like to detect if a keystroke would result in a closing square bracket ] in a KeyDown event of a WPF Popup:
private void MyPopup_KeyDown(object sender, KeyEventArgs e)
{
if (theKeyStrokeWouldResultIsAClosingSquareBracket)
{
// ...
// do stuff
// ...
e.Handled = true;
}
}
The solution has to work for any keyboard layout. Notice that key mappings of different layouts usually differ quite a lot. Therefore checking for the right key combination (like Ctrl + Alt + Digit 9 on a German QWERTZ keyboard or simply OemCloseBrackets on a US QUERTY keyboard) is unfortunately no solution.
How can you accomplish such a detection despite these problems?
EDIT - Adding some information before starting the bounty:
I'm currently implementing an IntelliSense-like function in my current project. The IntelliSense stuff is inspired by this: http://www.codeproject.com/Articles/22803/Intellisense-like-Method-Selection-Pop-up-Window
The custom syntax allows expressions like [abc].[def] > [ghi].[jkl], where an user should be able to make the intellisense function autocomplete the text in the brackets. To make the intellisense function a bit more similar to Visual Studio's (and lots of other IDEs') behavior, I'd like to enable the user to type a closing square bracket in the popup's list box to force an autocomplete.
You should use the PreviewTextInput event - this gets a TextCompositionEventArgs argument whose Text property will be the closing bracket.
You could add a transparent TextBox to the popup, and subscribe to the TextChanged event on that instead of an event on the Popup.
If it's a key combination I don't think you'll be able to capture it using a key down event.
if (e.Key == Key.OemCloseBrackets)
{
}
is the correct approach for the simple case with an English keyboard. If you would like to cope with the German keyboard that you have described, you should do a string comparison on the text box that you are working with calling from the key up event.
if (txtKeypad.Text.Substring(txtKeypad.Text.Length - 1, 1) == "]")
I hope this helps.
Ed

Categories

Resources