且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

需要帮助代码C#WinForm

更新时间:2023-12-06 17:19:34

你所犯的错误都是因为你不明白字符串是不可变的。比如说, System.String.Remove 不会删除任何内容,它根本不会修改字符串,而是每次返回一个全新的字符串。因此,不要使用 System.String 来计算数据,只使用 Label.Text 属性在每次更改时分配整个字符串。 (您甚至可以使用 System.String 进行这些计算,但这样会很尴尬和低效。)



如何计算键盘事件的数据?使用 System.Collections.Generic.List< code>< char>< / char> System.Text.StringBuilder (将其视为字符串类型的可变版本)。阅读有关这些类型的文档;一切都清晰简单,很好解释。



-SA


参见http://msdn.microsoft.com/en-us/library/ms171534(v=vs .110).aspx [ ^ ],用于处理键盘输入的***方式。



关于第二个问题,你需要记住 string s是不可变的;即无法改变。您需要存储正在更改它的方法的结果,例如:

 data = data.TrimStart('  /'); 
data = data.Substring( 1 );
// etc



看一下文档就可以了解这一点。


尝试这样做:

  string  s = e.KeyCode.ToString(); 
lbl_by_user.Text = s;


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:

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 ***) but it didn't work

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

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


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:
data = data.TrimStart('/');
data = data.Substring(1);
// etc


A look at the documentation would have made this clear.


Try doing this :
string s = e.KeyCode.ToString();
lbl_by_user.Text = s;