C# winform 弹出输入框 |
您所在的位置:网站首页 › 点击文本框弹出框怎么做 › C# winform 弹出输入框 |
以前用Qt和C++Builder的时候,里面都有一个InputDialog输入框。但是到C# winform里面没有这个。那么要实现这个效果怎么办,当然也是比较容易的。 先看下效果: 现在就上核心代码。 1、首先添加新建项:C#窗体 2、添加控件,并修改布局如下效果 修改button1控件name为btnOK,Text为确定, 修改取消button2控件name为btnCancel,Text为取消。 然后在按下F7,在代FrmInputDialog类里面添加代码: public delegate void TextEventHandler(string strText); public TextEventHandler TextHandler;3、双击按钮分别添加按钮事件,代码如下: private void btnOk_Click(object sender, EventArgs e) { if (null != TextHandler) { TextHandler.Invoke(txtString.Text); DialogResult = DialogResult.OK; } } private void btnCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; }4、添加TextBox的Key_Press事件,代码如下: private void txtString_KeyPress(object sender, KeyPressEventArgs e) { if (Keys.Enter == (Keys)e.KeyChar) { if (null != TextHandler) { TextHandler.Invoke(txtString.Text); DialogResult = DialogResult.OK; } } }5、然后在项目中添加新建项:C#类 命名为:InputDialog 然后修改InputDialog.cs中代码: public static class InputDialog { public static DialogResult Show(out string strText) { string strTemp = string.Empty; FrmInputDialog inputDialog = new FrmInputDialog(); inputDialog.TextHandler = (str) => { strTemp = str; }; DialogResult result = inputDialog.ShowDialog(); strText = strTemp; return result; } }以上就是自定义输入框的核心代码。 用法示例: 在按钮事件下添加代码: string strText = string.Empty; InputDialog.Show(out strText);strText就是从输入框拿到的内容。就这么简单。 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |