Click here to Skip to main content
15,912,578 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am building a Winform Application in which the application has to show I label whatever the user types from Keyboard.

The problem is:
When a key is pressed along with SHIFT both small and capial form of the letter is shown in label

eample :

A + SHIFT --> aA

but I need to show only "A"

here is my code:

C#
if ((e.Modifiers == Keys.Shift) && e.KeyCode >= Keys.A && e.KeyCode <= Keys.Z)
           {


              String s = e.KeyCode.ToString();
            List c = s.ToList();
              c.RemoveAt(c.Count);
              lbl_by_user.Text += s;
           }


I also tried this (I found it in StackOverflow) but it didn't work

C#
string data= "/temp string";

//If we want to remove the first character / we can do by alot of ways such as :

data.Remove(0,1);
data.TrimStart('/');
data.Substring(1);

Such kind of problem have been solved many times. So I googled it but couldn't
find the solution.

Any kind of help would be appreciated
Posted
Comments
Sergey Alexandrovich Kryukov 13-Jan-15 13:19pm    
Why not simply using TextBox?
—SA
BillWoodruff 13-Jan-15 22:47pm    
Are you willing to use a pop-up TextBox to enter Text, and when it is closed, a Label is created and appears in its place ? I can help you with that.
BibhutiAlmighty 14-Jan-15 8:19am    
Yes. I would appreciate that
BillWoodruff 14-Jan-15 17:49pm    
And what determines where this Label will appear when it is created ? Where the mouse went down on a Form, or ... ? More details, please.

All your mistakes step from the fact that you don't understand that strings are immutable. Say, System.String.Remove doesn't not remove anything, it does not modify the string at all but returns a brand-new string each time. Therefore, don't use System.String at all for calculating your data, use only the Label.Text property to assign the whole string on each change. (You even can use System.String for those calculations, but it will be awkward and inefficient.)

How to calculate data on keyboards events? Either use System.Collections.Generic.List<code><char></char> or System.Text.StringBuilder (consider it as the mutable version of string type). Read the documentation on these types; everything is clear and simple, well explained.

—SA
 
Share this answer
 
See http://msdn.microsoft.com/en-us/library/ms171534(v=vs.110).aspx[^], for the best ways of handling keyboard input.

As to your second question, you need to remember that strings are immutable; i.e. cannot be changed. You need to store the result of the method that is changing it, like:
C#
data = data.TrimStart('/');
data = data.Substring(1);
// etc

A look at the documentation would have made this clear.
 
Share this answer
 
Try doing this :
C#
string s = e.KeyCode.ToString();
lbl_by_user.Text = s; 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900