首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# winform窗体,form1为软键盘,form2上有textbox,那么怎么使用软键盘在form2的textbox内输入数据

2013-08-09 
C# winform窗体,form1为软键盘,form2上有textbox,那么如何使用软键盘在form2的textbox内输入数据form1软键

C# winform窗体,form1为软键盘,form2上有textbox,那么如何使用软键盘在form2的textbox内输入数据
form1软键盘是可以使用的,在网页上,QQ对话框等都是可以输入数据的,好像是焦点的问题,但是我点击使用软键盘form1的时候,焦点就在form1上了。怎样让焦点在textbox上,使用软键盘在textbox上输入数据



namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f = new Form2();
            f.Press += MiniKeyboardHandler;
            f.ShowDialog();
        }

        private void MiniKeyboardHandler(object sender, MiniKeyboardArgs e)
        {
            textBox1.Text += e.KeyCode;
        }
    }
}



namespace Test
{
    //模拟小键盘
    public partial class Form2 : Form
    {
        public delegate void MiniKeyboardHandler(object sender, MiniKeyboardArgs e);
        public event MiniKeyboardHandler Press;

        public Form2()
        {


            InitializeComponent();

            BindEvent();
        }

        private void BindEvent()
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is Button)
                    ctl.Click += MinikeyboardPress;
            }
        }

        private void MinikeyboardPress(object sender, EventArgs e)
        {
            OnMiniKeyboardHandle(new MiniKeyboardArgs(((Button)sender).Text));
        }

        private void OnMiniKeyboardHandle(MiniKeyboardArgs e)
        {
            MiniKeyboardHandler temp = Press;
            if (temp != null)
                temp(this, e);
        }
    }

    public class MiniKeyboardArgs : EventArgs
    {
        public string KeyCode { get; private set; }

        public MiniKeyboardArgs(string code)
        {
            KeyCode = code;
        }
    }
}


[解决办法]
引用:
Quote: 引用:

Quote: 引用:

。。。。
一开始我也觉得是焦点的问题,但是我的两个form在一个应用程序里面,不可能出现两个焦点的,除非软键盘独立出来成为一个单独的exe文件。所以问题就变成了两个form之间的数据传递了



定义一个事件传递吧,软键盘的窗体公开一个事件,点击每个字符的时候就触发,让接受输入的窗体注册事件。

版主,有详细点的列子不


代码比较乱,不好贴,我精简合并下,凑合看吧。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.Run(new MainForm());
        }
    }
    public  class MainForm : Form
    {
        private Form _keyForm = new KeysForm();
        private TextBox _txtBox = new TextBox();
        public MainForm()
        {
            this._txtBox.Size=new System.Drawing.Size(100, 30);
            this.ClientSize = new System.Drawing.Size(120, 60);
            this.Controls.Add(this._txtBox);
            //不想用这个事件你可以在KeysForm里面重新定义一个适合你自己的
            this._keyForm.KeyDown += _keyForm_KeyDown;
            this._keyForm.Show();
        }

        void _keyForm_KeyDown(object sender, KeyEventArgs e)
        {
            //接受KeysForm传递过来的按键值
            //根据keyCode你自己转换吧
            this._txtBox.AppendText(e.KeyCode.ToString());
        }
    }
    public  class KeysForm : Form
    {
        public KeysForm()
        {


            String keys = "ABCDEF";
            Int32 i = 1;
            //随便加几个按钮
            foreach (var item in keys)
            {
                Button keyButton = new Button();
                keyButton.Click += keyDown_Click;
                keyButton.Text = item.ToString();
                keyButton.Tag = ((Int32)item).ToString();
                keyButton.Size = new System.Drawing.Size(50, 30);
                keyButton.Location = new System.Drawing.Point((i - 1) * 60, 0);
                i++;
                this.Controls.Add(keyButton);
            }
            this.ClientSize = new System.Drawing.Size(340, 60);
        }

        private void keyDown_Click(object sender, EventArgs e)
        {
            Control keyControl = sender as Control;
            Int32 charCode = -1;
            if (Int32.TryParse(keyControl.Tag as String, out charCode) == false)
            {
                return;
            }
            Keys key = (Keys)charCode;
            KeyEventArgs ke = new KeyEventArgs(key);



            //主要就是这点,触发KeyDown事件,通知MainForm接受我按了那一个按键
            this.OnKeyDown(ke);
        }
    }
}



主要思路就是通过事件传递你想要的传递的值。

热点排行