C#中一个类似inputBox的输入框(转)
2006-05-30 14:21
393 查看
小玩意,很实用(自己太懒)
The InputBox class is a public class with a private constructor. You use it by calling
the static Show method. This method instantiates a new instance of the class, sets
it's properties and returns the result. The result is not a string but a InputBoxResult
object. This object has two properties: OK and Text. OK is a boolean indicating
that the user clicked the OK button and not the Cancel button. The Text contains
the string the user entered.
public static InputBoxResult
Show(string
prompt, string
title,
string defaultResponse,
InputBoxValidatingHandler validator,
int xpos,
int ypos)
{
using
(InputBox form =
new InputBox())
{
form.labelPrompt.Text
= prompt;
form.Text =
title;
form.textBoxText.Text
= defaultResponse;
if
(xpos >= 0
&& ypos
>= 0)
{
form.StartPosition =
FormStartPosition.Manual;
form.Left =
xpos;
form.Top =
ypos;
}
form.Validator =
validator;
DialogResult
result = form.ShowDialog();
InputBoxResult
retval = new
InputBoxResult();
if
(result
== DialogResult.OK) {
retval.Text =
form.textBoxText.Text;
retval.OK =
true;
}
return
retval;
}
}
public static InputBoxResult
Show(string
prompt, string
title,
string defaultText,
InputBoxValidatingHandler validator)
{
return Show(prompt,
title, defaultText, validator,
-1,
-1);
}
You activate the InputBox by calling the static Show() method. It has 4 required
and 2 optional arguments (using overloading).
private void
buttonTest_Click(object
sender, System.EventArgs e)
{
InputBoxResult result = InputBox.Show("Test prompt:",
"Some title", "Default text",
null);
if (result.OK)
{
textBox1.Text =
result.Text;
}
}
You can add validation logic using the validator argument. The validator is a InputBoxValidatingHandler
delegate which you can use to validate the Text. The following sample checks whether
the Text is not empty. If so it sets Cancel to true and the Message to 'Required'.
private void
buttonTest_Click(object
sender, System.EventArgs e)
{
InputBoxResult result = InputBox.Show("Test prompt:",
"Some title", "Default text",
new InputBoxValidatingHandler(inputBox_Validating));
if (result.OK)
{
textBox1.Text =
result.Text;
}
}
private void
inputBox_Validating(object
sender, InputBoxValidatingArgs
e) {
if (e.Text.Trim().Length
== 0)
{
e.Cancel
= true;
e.Message
= "Required";
}
}
下载地址:http://www.reflectionit.nl/download.aspx?URL=InputBoxSample.zip
InputBox class
The InputBox class is a public class with a private constructor. You use it by callingthe static Show method. This method instantiates a new instance of the class, sets
it's properties and returns the result. The result is not a string but a InputBoxResult
object. This object has two properties: OK and Text. OK is a boolean indicating
that the user clicked the OK button and not the Cancel button. The Text contains
the string the user entered.
public static InputBoxResult
Show(string
prompt, string
title,
string defaultResponse,
InputBoxValidatingHandler validator,
int xpos,
int ypos)
{
using
(InputBox form =
new InputBox())
{
form.labelPrompt.Text
= prompt;
form.Text =
title;
form.textBoxText.Text
= defaultResponse;
if
(xpos >= 0
&& ypos
>= 0)
{
form.StartPosition =
FormStartPosition.Manual;
form.Left =
xpos;
form.Top =
ypos;
}
form.Validator =
validator;
DialogResult
result = form.ShowDialog();
InputBoxResult
retval = new
InputBoxResult();
if
(result
== DialogResult.OK) {
retval.Text =
form.textBoxText.Text;
retval.OK =
true;
}
return
retval;
}
}
public static InputBoxResult
Show(string
prompt, string
title,
string defaultText,
InputBoxValidatingHandler validator)
{
return Show(prompt,
title, defaultText, validator,
-1,
-1);
}
Usage
You activate the InputBox by calling the static Show() method. It has 4 requiredand 2 optional arguments (using overloading).
private void
buttonTest_Click(object
sender, System.EventArgs e)
{
InputBoxResult result = InputBox.Show("Test prompt:",
"Some title", "Default text",
null);
if (result.OK)
{
textBox1.Text =
result.Text;
}
}
Validation
You can add validation logic using the validator argument. The validator is a InputBoxValidatingHandlerdelegate which you can use to validate the Text. The following sample checks whether
the Text is not empty. If so it sets Cancel to true and the Message to 'Required'.
private void
buttonTest_Click(object
sender, System.EventArgs e)
{
InputBoxResult result = InputBox.Show("Test prompt:",
"Some title", "Default text",
new InputBoxValidatingHandler(inputBox_Validating));
if (result.OK)
{
textBox1.Text =
result.Text;
}
}
private void
inputBox_Validating(object
sender, InputBoxValidatingArgs
e) {
if (e.Text.Trim().Length
== 0)
{
e.Cancel
= true;
e.Message
= "Required";
}
}
下载地址:http://www.reflectionit.nl/download.aspx?URL=InputBoxSample.zip
相关文章推荐
- 在C#中使用类似VB中的InputBox
- 一个c#的输入框函数
- 如何用c#来制作一个activeX控件或类似的东西
- .net C# inputBox文字输入对话框,类似delphi的inputBox
- SharpDevelop 类似visual studio的一个C#制作的开源,.net开发工具
- 在Unity3D中用C#封装一个类似AS3的ByteArray类
- 用c#写的一个局域网聊天客户端 类似小飞鸽
- 如何用c#来制作一个activeX控件或类似的东西
- C#中的委托(与Ruby中的类似,方法看作一个类型)
- 如何用c#来制作一个activeX控件或类似的东西
- C#体贴之处点滴 - 如果打算写一个类似System.Linq.Enumerable.Where的extention method
- 一个c#的输入框函数
- (原创)一个和c#中Lazy<T>类似的c++ Lazy<T>类的实现
- 如何用c#来制作一个activeX控件或类似的东西
- 一个类似FlashGet的c#开源下载工具
- C#怎么做一个永远没有焦点的窗口 类似输入法控制栏
- 用C#写的一个类似网吧游戏菜单的软件
- .net C# inputBox文字输入对话框,类似delphi的inputBox
- 用c#开发一个类似AutoCAD功能的程序
- ClassInputBox类 在c#中类似VB的InputBox