【问题标题】:How to make a menu to bind keys如何制作菜单以绑定键
【发布时间】:2019-01-16 13:30:13
【问题描述】:

I want to create a field (textbox, button, label ...) that when selected and pressed a key, take the name of the key.

我正在尝试这个没有成功:

private void label24_Click(object sender, System.Windows.Forms.KeyEventArgs e)
{
    label24.Text += e.KeyData.ToString();
}

编辑1:

My menu

目前我需要在文本框中手动输入keycode,我想点击该字段并按一个键,textbox.text变为key code

【问题讨论】:

  • 您好,能否提供更多与您的问题相关的代码?现在还不清楚你想做什么
  • 仍然很困惑,但让我们尝试一下。为什么不能将 KeyDownEvent 添加到 TextBox,那么在这种情况下,您应该使用 e.Key 更改 label24.Text
  • 我还是个初学者,只是通过KeyDown改变Click事件?
  • 问题是你没有提供足够的关于代码运行环境的信息。如果它是 WPF,我建议您使用视图模型和文本绑定,如果它是 WinForm,那么您应该将 KeyDown 事件添加到您的 TextBox 中,您可以在其中键入应该在标签中显示的信息。在此 KeyDown 事件中,您应该更新标签文本

标签: c# menu


【解决方案1】:

如果您询问用户在触摸屏显示器上按下时如何绑定键,以下是示例。

private void label24_Click(object sender, EventArgs e)
{   
    myButton btn = (myButton)sender;
    MyTextBox.Text += btn.Tag.ToString();
}

但是如果你想迭代字段中的所有按钮。请参阅下面的示例

public partial class myNumpad : UserControl
{
    public myNumpad()
    {
        InitializeComponent();
        BindControls(tableLayoutPanel2);
    }

    public void BindControls(TableLayoutPanel table)
    {
        for (int i = 0; i <= table.ColumnCount; i++)
        {
            for (int j = 0; j <= table.RowCount; j++)
            {
                Control c = table.GetControlFromPosition(i, j);
                if (c is myButton)
                    c.Click += new EventHandler(Numpad_Click);
            }
        }
    }

    [BindableAttribute(true), Category("Control"), Description("SetControl")]
    public Control SetControl { get; set; }

    private void Numpad_Click(object sender, EventArgs e)
    {
        if (SetControl != null)
        {
            myButton btn = (myButton)sender;
            SetControl.Text += btn.Tag.ToString();
        }
    }
}

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 1970-01-01
    • 2015-06-25
    相关资源
    最近更新 更多