C#实现VB6.0的输入窗体InputBox代码
2012-05-13 19:46
381 查看
备注:新建一个类,粘贴上所有的一下代码就可以调用了
using System;
using System.Windows.Forms;
using System.Drawing;
class InputBox : Form
{
private Label labelText = new Label();
private TextBox textboxValue = new TextBox();
private Button buttonOK = new Button();
private bool onlyNumeric;
public InputBox()
{
Init();
}
private void Init()
{
this.Width = 400;
this.Height = 150;
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MinimizeBox = false;
this.MaximizeBox = false;
labelText.AutoSize = true;
labelText.Location = new Point(10, 20);
textboxValue.Location = new Point(10, (this.ClientSize.Height - textboxValue.Height) / 2);
textboxValue.Width = this.ClientSize.Width - 20;
buttonOK.Text = "确定(&O)";
buttonOK.Location = new Point((this.ClientSize.Width - buttonOK.Width) / 2, this.ClientSize.Height - buttonOK.Height - 10);
this.Controls.Add(labelText);
this.Controls.Add(textboxValue);
this.Controls.Add(buttonOK);
this.AcceptButton = buttonOK;
buttonOK.Click += new EventHandler(buttonOK_Click);
textboxValue.KeyPress += new KeyPressEventHandler(textboxValue_KeyPress);
}
void textboxValue_KeyPress(object sender, KeyPressEventArgs e)
{
if (onlyNumeric)
if ((e.KeyChar < (char)Keys.D0 || e.KeyChar > (char)Keys.D9) && e.KeyChar != (char)Keys.Back)
{
e.Handled = true;
}
}
/// <summary>
/// InputBox的静态函数,返回输入的字符串
/// </summary>
/// <param name="Title">窗口标题</param>
/// <param name="Text">提示文本</param>
/// <param name="DefaultValue">默认值</param>
/// <returns>返回字符串</returns>
public static string Input(string Title, string Text, string DefaultValue)
{
InputBox inputBox = new InputBox();
inputBox.Text = Title;
inputBox.labelText.Text = Text;
inputBox.textboxValue.Text = DefaultValue;
inputBox.textboxValue.SelectAll();
DialogResult result = inputBox.ShowDialog();
if (result == DialogResult.OK)
return inputBox.textboxValue.Text;
else
return DefaultValue;
}
/// <summary>
/// InputBox的静态函数,返回输入的字符串
/// </summary>
/// <param name="Title">窗口标题</param>
/// <param name="Text">提示文本</param>
/// <param name="DefaultValue">默认值</param>
/// <param name="OnlyNumeric">是否只允许输入数字</param>
/// <returns>返回字符串</returns>
public static string Input(string Title, string Text, string DefaultValue, bool OnlyNumeric)
{
InputBox inputBox = new InputBox();
inputBox.Text = Title;
inputBox.labelText.Text = Text;
inputBox.onlyNumeric = OnlyNumeric;
DialogResult result = inputBox.ShowDialog();
if (result == DialogResult.OK)
return inputBox.textboxValue.Text;
else
return DefaultValue;
}
private void buttonOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
using System;
using System.Windows.Forms;
using System.Drawing;
class InputBox : Form
{
private Label labelText = new Label();
private TextBox textboxValue = new TextBox();
private Button buttonOK = new Button();
private bool onlyNumeric;
public InputBox()
{
Init();
}
private void Init()
{
this.Width = 400;
this.Height = 150;
this.StartPosition = FormStartPosition.CenterParent;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.MinimizeBox = false;
this.MaximizeBox = false;
labelText.AutoSize = true;
labelText.Location = new Point(10, 20);
textboxValue.Location = new Point(10, (this.ClientSize.Height - textboxValue.Height) / 2);
textboxValue.Width = this.ClientSize.Width - 20;
buttonOK.Text = "确定(&O)";
buttonOK.Location = new Point((this.ClientSize.Width - buttonOK.Width) / 2, this.ClientSize.Height - buttonOK.Height - 10);
this.Controls.Add(labelText);
this.Controls.Add(textboxValue);
this.Controls.Add(buttonOK);
this.AcceptButton = buttonOK;
buttonOK.Click += new EventHandler(buttonOK_Click);
textboxValue.KeyPress += new KeyPressEventHandler(textboxValue_KeyPress);
}
void textboxValue_KeyPress(object sender, KeyPressEventArgs e)
{
if (onlyNumeric)
if ((e.KeyChar < (char)Keys.D0 || e.KeyChar > (char)Keys.D9) && e.KeyChar != (char)Keys.Back)
{
e.Handled = true;
}
}
/// <summary>
/// InputBox的静态函数,返回输入的字符串
/// </summary>
/// <param name="Title">窗口标题</param>
/// <param name="Text">提示文本</param>
/// <param name="DefaultValue">默认值</param>
/// <returns>返回字符串</returns>
public static string Input(string Title, string Text, string DefaultValue)
{
InputBox inputBox = new InputBox();
inputBox.Text = Title;
inputBox.labelText.Text = Text;
inputBox.textboxValue.Text = DefaultValue;
inputBox.textboxValue.SelectAll();
DialogResult result = inputBox.ShowDialog();
if (result == DialogResult.OK)
return inputBox.textboxValue.Text;
else
return DefaultValue;
}
/// <summary>
/// InputBox的静态函数,返回输入的字符串
/// </summary>
/// <param name="Title">窗口标题</param>
/// <param name="Text">提示文本</param>
/// <param name="DefaultValue">默认值</param>
/// <param name="OnlyNumeric">是否只允许输入数字</param>
/// <returns>返回字符串</returns>
public static string Input(string Title, string Text, string DefaultValue, bool OnlyNumeric)
{
InputBox inputBox = new InputBox();
inputBox.Text = Title;
inputBox.labelText.Text = Text;
inputBox.onlyNumeric = OnlyNumeric;
DialogResult result = inputBox.ShowDialog();
if (result == DialogResult.OK)
return inputBox.textboxValue.Text;
else
return DefaultValue;
}
private void buttonOK_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
相关文章推荐
- 艾伟_转载:C#下实现空白窗体上中文输入,可以实现类PS的文字工具
- C#下实现空白窗体中文输入
- C# 无边框窗体之窗体移动实现代码
- 几行代码帮您实现Win7窗体的打开效果(C#)
- C#实现窗体截图(代码+效果)
- 用C#代码实现类似QQ窗体的“上、左、右”停靠功能【转】
- 用C#用C#实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码实现窗体在规定时间弹出,例如:10:00.弹出后关闭。并在5分钟后再次弹出。5次后停止。最好有具体代码
- C#窗体的最大化/最小化/还原/最小化到托盘/NotifyIcon的代码实现
- 【转】C#中代码实现控件随窗体的自由变换
- C#下实现空白窗体中文输入
- C#窗体的最大化/最小化/还原/最小化到托盘/NotifyIcon的代码实现
- C# 在winform中隐藏或者去除c#的标题栏并实现窗体移动附代码
- c#窗体支持多个文件拖放的代码实现
- C#简单代码实现窗体最小化到托盘运行
- C#中ListView控件实现窗体代码
- C#下实现空白窗体中文输入,实现类PS的文字工具
- 在winfrom下利用c#代码,实现kindEditor的JavaScript方法:editor.html(),实现上报窗体的自动提交。
- C#无边框窗体实现以及拖动代码
- C#窗体的最大化/最小化/还原/最小化到托盘/NotifyIcon的代码实现
- C#下实现空白窗体上中文输入,可以实现类PS的文字工具