C#数字键盘0-9逻辑 [英] c# number pad 0-9 logic

查看:144
本文介绍了C#数字键盘0-9逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以纠正逻辑,以便按钮0-9可以运行一段代码,然后按一下按钮,该数字就会出现在文本框中?

解决方案

<在与mer merss进行讨论之后,我创建了一个示例性示例. (只显示要点,没有错误处理!)

 使用系统;
使用使用System.Windows.Forms;

命名空间 OnHandlerForAll
{
    静态  class 程序
    {
        [STAThread]
        静态 无效 Main()
        {
            Application.Run( FormWithButtons());
        }

        公共  FormWithButtons:表单
        {
            ListBox listbox =  ListBox(){Dock = DockStyle.Fill};

            公共 FormWithButtons()
            {
                Controls.Add(列表框);
                 for ( int  i =  0 ; i <   10 ; i ++)
                {
                    Button button =  Button(){文本= i.ToString(),Tag = i,Dock = DockStyle.Top};
                    // 按钮.单击+ = new EventHandler(button_Click); 
                    点击+ =  EventHandler(button_ClickWithSwitch);
                    Controls.Add(button);
                }
            }

            无效 button_Click(对象发​​件人,EventArgs e)
            {
                Button buttonSender =发件人作为按钮;
                // 如果我们有一个新的Button,我们不必知道"button"携带的是哪个数字.不必扩展它.
                listbox.Items.Add(buttonSender.Tag); // 由于某些原因,我们需要ListBox中的数字
            }

            无效 button_ClickWithSwitch(对象发​​件人,EventArgs e)
            {
                Button buttonSender =发件人作为按钮;

                // 我们之前必须知道"这些情况,如果有一个新的Button,我们必须对其进行扩展
                开关(buttonSender.Text)
                {
                    案例 " :列表框. .Add( 0 );  break ; // 由于某些原因,我们需要ListBox中的数字
                    案例 " :列表框. .Add( 1 );  break ;
                    案例 " :列表框. .Add( 2 );  break ;
                    案例 " :列表框. .Add( 3 );  break ;
                    案例 " :列表框. .Add( 4 );  break ;
                    案例 " :列表框. .Add( 5 );  break ;
                    案例 " :列表框. .Add( 6 );  break ;
                    案例 " :列表框. .Add( 7 );  break ;
                    案例 " :列表框. .Add( 8 );  break ;
                    案例 " :列表框. .Add( 9 );  break ;
                }
            }
        }
    }
} 


我在现实世界中看到的一个可行的技巧是将Buttons"1","2"上的字符串转换为Click-Handler中的数字.我不会那样做,而是使用Button的Tag-Property:

 Button button =  Button();
button.Text = " ;
button.Tag =  1 ;
按钮.点击+ = 委托(对象 obj,EventArgs ea)
{
    Button buttonClicked = obj  as 按钮;
     int  iNumberButtonIsRepresenting =( int )buttonClicked.Tag;
    //  ... 
};


使用按钮控件的CommandArgument和CommandName.为每个按钮指定相同的CommandName,并为每个按钮指定CommandArgument的编号.非常粗糙的样子:

 受保护的  void  btnSubmit_Command(对象发​​件人,CommandEventArgs e)
{
开关(例如CommandArgument)
{
案例  0 :
// 做某事... 
 break ;
案例 ...
}
} 


is there a way to right a logic so that buttons 0 - 9 can run off one piece of code and whe npress button the number appears in the textbox?

解决方案

Following the discussion with mark merrens I created a placative example. (just showing the point, no error-handling!)

using System;
using System.Windows.Forms;

namespace OnHandlerForAll
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.Run(new FormWithButtons());
        }

        public class FormWithButtons : Form
        {
            ListBox listbox = new ListBox() { Dock = DockStyle.Fill };

            public FormWithButtons()
            {
                Controls.Add(listbox);
                for (int i = 0; i < 10; i++)
                {
                    Button button = new Button() { Text = i.ToString(), Tag = i, Dock = DockStyle.Top };
                    // button.Click += new EventHandler(button_Click);
                    button.Click += new EventHandler(button_ClickWithSwitch);
                    Controls.Add(button);
                }
            }

            void button_Click(object sender, EventArgs e)
            {
                Button buttonSender = sender as Button;
                // we don't have to know which number the "button" carries, if there is a new Button we don't have to extend it.
                listbox.Items.Add(buttonSender.Tag); // we need numbers in the ListBox for some reason
            }

            void button_ClickWithSwitch(object sender, EventArgs e)
            {
                Button buttonSender = sender as Button;

                // We have to "know" the cases before, If there is a new Button we have to extend it
                switch (buttonSender.Text)
                {
                    case "0": listbox.Items.Add(0); break; // we need numbers in the ListBox for some reason
                    case "1": listbox.Items.Add(1); break;
                    case "2": listbox.Items.Add(2); break;
                    case "3": listbox.Items.Add(3); break;
                    case "4": listbox.Items.Add(4); break;
                    case "5": listbox.Items.Add(5); break;
                    case "6": listbox.Items.Add(6); break;
                    case "7": listbox.Items.Add(7); break;
                    case "8": listbox.Items.Add(8); break;
                    case "9": listbox.Items.Add(9); break; 
                }
            }
        }
    }
}


A working hack I have seen in real world implementation was to convert the string on the Buttons "1", "2" to a number in the Click-Handler. I wouldn''t do it like that, but use the Tag-Property of the Button like this:

Button button = new Button();
button.Text = "1";
button.Tag = 1;
button.Click += delegate(object obj, EventArgs ea)
{
    Button buttonClicked = obj as Button;
    int iNumberButtonIsRepresenting = (int)  buttonClicked.Tag;
    // ...
};


Use the CommandArgument and CommandName of the button controls. Give each of the buttons the same CommandName and give each of the buttons a number for the CommandArgument. Very crudely might look like:

protected void btnSubmit_Command(object sender, CommandEventArgs e)
{
	switch (e.CommandArgument)
	{
		case 0:
			// Do something...
			break;
		case ...
	}
}


这篇关于C#数字键盘0-9逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆