|
I tried following code
private void mainTextBox_KeyDown(object sender, KeyEventArgs e)
{
Key key = e.Key;
logTextBox.Text += "Key = " + key.GetHashCode().ToString() + "\r\n";
e.Handled = true;
}
But this can not distinguish between capital letters and small letters. How can I permit only small letters?
|
|
|
|
|
rajeevkuth wrote: But this can not distinguish between capital letters and small letters. How can I permit only small letters?
Have a look at the other members of KeyEventArgs, maybe Shift, KeyCode, or SuppressKeyPress().
And what about edit operations, such as paste (CTRL/V)?
Or use a MaskedTextBox . For more elaborate schemes, there are a number of CP articles on the subject.
Luc Pattyn [My Articles] Nil Volentibus Arduum
modified on Saturday, September 17, 2011 12:31 PM
|
|
|
|
|
I was trying to make a transliteration editor for some Indian language. If user types "Ra" I will replace these characters with 'ര'. If the user types 'ra' I has to replace it with 'റ'.
The problem here is I am not able to distinguish between 'r' and "R".
I do not need to handle copy paste operations.
This is the link for MSDN Link for the class. Is there anything which I can use? Or do I need to derive from TextBox?
|
|
|
|
|
if you need to manipulate printable character input, you're better of in the KeyPress event, where you have a KeyPressEventArgs.KeyChar property. KeyDown and KeyUp events are most useful when you need to deal with non-printable keys such as the arrow keys.
From your reply it seems your initial question did not reflect your actual problem. If you needs happen to change again, I suggest you read up on all Keyboard related events and locate the events and properties that best match your needs.
|
|
|
|
|
I find your question, and the response to Luc, taken together, hard to understand: on the one hand (your original message) you seem to be asking how to restrict keyboard entry to only lowercase letters, but in your response to Luc you say:
"I was trying to make a transliteration editor for some Indian language. If user types "Ra" I will replace these characters with 'ര'. If the user types 'ra' I has to replace it with 'റ'."
Which, to me, implies you do allow the user to type in upper-case, and you do need to take different actions based on case alone.
Please try to clarify what you are trying to do here a little more.
best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
What I really need is, I should be able to know if user enters 'c' or 'C'.
From Luc's answer I found that this is difficult to do by handling KeyUp and KeyDown events. The sad part is, TextBox does not expose such an event from which I can get KeyPressEventArgs.
|
|
|
|
|
rajeevkuth wrote: The sad part is, TextBox does not expose such an event from which I can get KeyPressEventArgs.
Where do you get such nonsense? In WinForms every Control has the same three keyboard events.
|
|
|
|
|
Where do you get such nonsense? In WinForms every Control has the same three keyboard events.
Thanks for this info. I was keeping the textbox in a WPF window. When I kept the TextBox in Form Window, the event is available. I never expected that the same control will expose different sets of events based on its container.
|
|
|
|
|
edit ... now that you've mentioned you are using WPF ... suggest you re-post in the WPF/SilverLight forum ... and repost with a much more carefully written statement of what you are trying to do ... WPF does indeed expose some different events for a TextBox: take a look at TextBox_PreviewTextInput. ... end edit ...
Assuming you are using WinForms: may I suggest you start by design-time setting an Event Handler for the KeyDown Event in a TextBox via the Properties/Events Manager, and then studying carefully the properties exposed by the 'KeyEventArgs e' parameter in that event handler.
Focus on "e.Shift."
And, then, think about what you have to do to preserve the information that you have received one ... or more ... key-presses with the Shift key down, and act upon it if the next key-press is lower-case.
Note the assumption, implicit here, that just handling whether the 'Shift' key is/was down will be enough to get you a solution to what you have described. Always good to question assumptions !
best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
modified on Sunday, September 18, 2011 2:53 AM
|
|
|
|
|
rajeevkuth wrote: How can I permit only small letters?
<a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.charactercasing.aspx">TextBox.CharacterCasing</a>[<a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.charactercasing.aspx" target="_blank" title="New Window">^</a>] . It doesn't solve the problem that you're explaining deeper in the thread, it merely answers the question.
Which emphasizes the need to ask a clear question, explaining what it is that you're trying to achieve. That's a bit different from how to perform a particular task.
Bastard Programmer from Hell
|
|
|
|
|
+5 A great answer to the original question as asked ! Now that the OP has finally revealed he's now using WPF, I've suggested he re-locate his question to that forum: I think the WPF TextBox_PreviewTextInput Event will give him everything he needs (that's only a guess; I don't work with WPF).
best, Bill
"Beauty is in the eye of the beholder, and it may be necessary from time to time to give a stupid or misinformed beholder a black eye." Miss Piggy"
|
|
|
|
|
Thanks
|
|
|
|
|
There are actually two events that you need to hook into, and only one of them relates to previewing text input. The other event handler you need to add in takes care of the clipboard - people often forget that you need to handle the case when the user copies in values. If I were writing this code, I would create an Attached Behavior and handle the events in there, so setting up the actual class would look like this:
public class SpecialTextboxCasing : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewTextInput += AssociatedObject_PreviewTextInput;
DataObject.AddPastingHandler(AssociatedObject, OnClipboardPaste);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PreviewTextInput -= AssociatedObject_PreviewTextInput;
DataObject.RemovePastingHandler(AssociatedObject, OnClipboardPaste);
}
private void OnClipboardPaste(object sender, DataObjectPastingEventArgs dopea)
{
}
void AssociatedObject_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
}
}
|
|
|
|
|
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString() == e.KeyChar.ToString().ToUpper())
{
MessageBox.Show("Only Small Letters are Permitted");
e.Handled = true;
}
}
|
|
|
|
|
And what does your code do if I paste upper case characters into the textbox?
|
|
|
|
|
it not allow to paste
or u can use this code
e.KeyChar = e.KeyChar.ToString().ToLower().ToCharArray()[0];
|
|
|
|
|
Actually, you can't use that code - that's not how you receive text from the clipboard. When you cope with pasting, you have to deal with the possibility that the user is posting in a large amount of text. This is just a friendly hint - try to think of all the ways the user can interact, not just the way that you think is easy.
|
|
|
|
|
Try checking that with char.IsLower function.
|
|
|
|
|
Again, what would you do with pasted text? Please try to be more complete with your answer.
|
|
|
|
|
Ok,
Right to be more specific in textchanged event, check every letter of textbox (using .text property) if its uppercase with char.IsLower and then when you have full string handle code as you want.
|
|
|
|
|
You might want to post this reply to the OP rather than to me. I already know how to do it, and he won't get notified via email that there's a new answer if it's not against his account.
|
|
|
|
|
You may also want to consider handling the TextChanged[^] event and using Regex to fix up the current text. Since the event fires when the control's Text property is changed both as a result of user interaction as well as programatically, you'll need a flag to ignore the event when it fires as a result of a fixup.
Edit: I just realized you're using WPF. In that case, see this[^] event.
/ravi
|
|
|
|
|
In short, I need help on how to add a new section such as:
<section name="Group3" type="System.Configuration.NameValueSectionHandler" />
along with
<Group3>
keys go here
</Group3>
To a config file laid out like:
="1.0"="utf-8"
<configuration>
<configSections>
<sectionGroup name="Groups" >
<section name="Group1" type="System.Configuration.NameValueSectionHandler" />
<section name="Group2" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<Groups>
<Group1>
<add key="System 1" value="192.168.0.3" />
</Group1>
<Group2>
<add key="System 2" value="192.168.0.1" />
<add key="System 3" value="192.168.0.2" />
</Group2>
</Groups>
<userSettings>
<setting name="LastGroup" serializeAs="String">
<value>Group1</value>
</setting>
</userSettings>
</configuration>
I have been trying to do this for nearly a day, trying every method I could find to do so, but I was unsuccessful so I decided its time to ask for help. I even went as far as fully implementing this code[^] into my program and I still could not get it to work properly (it added a section, but it added the key and value to the <group3> block, not to an <add> block and I was unsure as to how to fix it.
Trying my best to understand all I can about the Mysteries of configuration[^]....but it seems to do a lot more than I need it to do (it goes into detail about adding custom sections, which I don't really need, keys and value is all I need in a section).
|
|
|
|
|
Have you tried:
="1.0"="utf-8"
<configuration>
<configSections>
<sectionGroup name="Groups" >
<section name="Group1" type="System.Configuration.NameValueSectionHandler" />
<section name="Group2" type="System.Configuration.NameValueSectionHandler" />
<section name="Group3" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<Groups>
<Group1>
<add key="System 1" value="192.168.0.3" />
</Group1>
<Group2>
<add key="System 2" value="192.168.0.1" />
<add key="System 3" value="192.168.0.2" />
</Group2>
<Group3>
<add key="key1" value="value1" />
<add key="key2" value="value2" />
</Group3>
</Groups>
<userSettings>
<setting name="LastGroup" serializeAs="String">
<value>Group1</value>
</setting>
</userSettings>
</configuration>
where the new entries are in bold?
|
|
|
|
|
Sorry that I wasn't clear enough, I need to be able to add the new group and keys via the program itself.
I basically want to create a piece of software that can modify its own .config file based on use input....I would simply use the user settings to do so, but I want to make it an administrative-like software, that changes settings for all users.
So I need some sort of code that can do basically what you did above automatically.
|
|
|
|