简体   繁体   English

允许用户编辑标签和更改文本-C#WinForm

[英]Allow user to edit label and change text - c# winform

I have a winform with a custom label inherited from a Windows Form Label. 我有一个带有从Windows窗体标签继承的自定义标签的winform。 I need to allow user to edit this label and change the value. 我需要允许用户编辑此标签并更改值。

I know we can use textbox to allow user to edit by changing the readonly property to false. 我知道我们可以使用文本框通过将readonly属性更改为false来允许用户进行编辑。

But the reason why i use label is that i might need to change the orientation of the text and hence the custom label. 但是我使用标签的原因是我可能需要更改文本的方向,因此也需要更改自定义标签。

So my question is, is it possible to allow user to edit a normal Winform Label? 所以我的问题是,是否可以允许用户编辑普通的Winform标签?

The post: Editable Label Controls is not a working solution. 帖子: 可编辑标签控件不是可行的解决方案。 That also doesn't have an accepted answer. 那也没有可接受的答案。

This isn't the best option by far, however, it's still an option. 到目前为止,这并不是最佳选择,但是仍然是一种选择。 Set your forms KeyPreview to true, add a KeyDown key event handler to your form and a Click event handler to your label. 将您的窗体KeyPreview设置为true,将KeyDown键事件处理程序添加到窗体,并将Click事件处理程序添加到标签。 In the click event handler, have it set a bool to true. 在click事件处理程序中,将布尔值设置为true。 Inside the keydown handler, have an if statement that adds the keys pressed to the labels Text property if the bool is true, that way, if the user clicks the label then anything they type will go into the label. 在keydown处理程序内部,有一个if语句,如果bool为true,则将按下的键添加到label的Text属性中,这样,如果用户单击标签,则他们键入的任何内容都会进入标签。

Example code for my horrible description: 我的恐怖描述的示例代码:

public static Form f1 = new Form();
public static Label l1 = new Label();
public static bool isLabelClicked = false;

Then put these in whatever method sets the properties for your form and it's objects. 然后将它们放在设置表单及其对象属性的任何方法中。

f1.KeyPreview = true;
l1.Click += new EventHandler (l1_Clicked);
f1.KeyDown += new KeyEventHandler (f1_keydown);

Then have these two methods in your project 然后在您的项目中使用这两种方法

public static void l1_Clicked(object sender, EventArgs e)
{
    isLabelClicked = true;
}

public static void f1_keydown(object sender, KeyEventArgs e)
{
    if (isLabelClicked == true)
    {
        if (e.KeyCode == Keys.A)
        {
            l1.Text += "a";
        }
        else if (e.KeyCode == Keys.B)
        {
            //Etc. For each key
        }
    }
}

It's not the most efficient way to do it and you'd need to set a way to make the bool false afterwards so it didn't keep changing the Labels value every time you typed, but it would work. 这不是最有效的方法,您需要设置一种方法来使布尔值成为假,这样它就不会每次输入时都不断更改Labels值,但是它可以工作。

I'm sure there's also an easier way to handle each different key without writing out a giant if, else if block, just be weary of converting KeyCode since "," can be "Oemcomma". 我敢肯定,还有一种更简单的方法来处理每个不同的密钥,而不用写出一个巨集(如果不是的话),因为对“,”可以是“ Oemcomma”感到厌倦了转换KeyCode。 Good luck with your coding :) 祝您编码顺利:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM