|
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.
|
|
|
|
|
I'd roll my own rather than try to use Microsoft's idea of a config file. But that's just me.
|
|
|
|
|
If I knew a good way to write my own (guess it wouldn't be too hard, but I don't know C# as well as many people on this forum probably do) I would, but until then, I'm stuck with what is offered to me that I can learn some from.
|
|
|
|
|
Oh, well then... here's an article I wrote that might be of some interest:
XmlNode[^]
I tend to write config files that are shared between users and applications -- like a list of the databases that are available on a system.
|
|
|
|
|
Thanks, I haven't read through it yet, just glanced over it and it seems like a lot to learn and take in but I can definitely see the benefits. I should be able to basically rework my config to something along the lines of:
<GroupManager>
<Groups>
<Group GroupName="Group 1">
<System Name="System 1">192.168.0.1</System>
</Group>
<Group GroupName="Group 2">
<System Name="System 2">192.168.0.2</System>
</Group>
</Groups>
</GroupManager>
With what you show in that link, and then more easily be able to manage the XML rather than being restricted to Microsofts config functions, right?
|
|
|
|
|
Sure, it's just XML, you can do pretty much what you want.
|
|
|
|