很多朋友是从VB转向C#的,包括我,很想拥有像InputBox那样的简单用户交互方法,现提供一个解决办法,源码如下:using System;namespace MisGoldPrinterTest
{
/// <summary>
/// 仿VB的InputBox函数
/// </summary>
public class InputBox
{
/// <summary>
/// 显示一个输入对话框,单击取消时返回空串。
/// </summary>
/// <param name="prompt">提示字符串</param>
/// <param name="title">窗口标题</param>
/// <param name="defaultResponse">默认输入值</param>
/// <returns></returns>
public static string Show(string prompt,string title,string defaultResponse)
{
string strReturn = defaultResponse;
if (title == "")
{
title = "MIS金质打印通";
} frmInputBox frm = new frmInputBox(prompt,defaultResponse);
frm.Text = title; System.Windows.Forms.DialogResult dlgResult = frm.ShowDialog(); if (dlgResult == System.Windows.Forms.DialogResult.OK)
{
strReturn = frm.InputText;
}
else
{
strReturn = "";
} return strReturn; } }//End class
}//End Namespace

解决方案 »

  1.   

    以上InputBox类中的窗口frmInputBox代码如下:using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace MisGoldPrinterTest
    {
    /// <summary>
    /// 仿VB的InputBox函数UI
    /// </summary>
    public class frmInputBox : System.Windows.Forms.Form
    { private string _InputText = "";
    private string m_DefaultText = "";
    private string m_Prompt = ""; private System.Windows.Forms.TextBox txtInput;
    private System.Windows.Forms.Button btnOk;
    private System.Windows.Forms.Label lblPrompt;
    private System.Windows.Forms.Button btnCancel; public string InputText
    {
    get
    {
    return _InputText;
    }
    } #region Windows 窗体设计器生成的代码
                      //....不列了,本人讲求实用,相信大家只要在窗口放一个文本框和按钮,双击两个按钮就会得到这些东东 public frmInputBox(string prompt,string defaultText):this()
    {
    m_Prompt = prompt;
    m_DefaultText = defaultText;
    }

    private void frmInputBox_Load(object sender, System.EventArgs e)
    {
    this.txtInput.Text = m_DefaultText;
    this.lblPrompt.Text = m_Prompt;
    }

    private void btnOk_Click(object sender, System.EventArgs e)
    {
    _InputText = txtInput.Text; this.DialogResult = DialogResult.OK;
    } private void frmInputBox_Closed(object sender, System.EventArgs e)
    {
    _InputText = txtInput.Text; this.DialogResult = DialogResult.OK;

    } private void btnCancel_Click(object sender, System.EventArgs e)
    {
    _InputText = txtInput.Text; this.DialogResult = DialogResult.Cancel;
    } }//End Class
    }//End NameSpace
      

  2.   

    把InputBox从frmInputBox 分离出来的道理很简单,举个简单的例子,我们用
    对话框如System.Drawing.Printing.PageSettings的时候有没有Form一些我们根本不用的属性啊,没有吧!//重命名工作表
    private void btnRenameSheet_Click(object sender, System.EventArgs e)
    {
    #region 实现... string name = InputBox.Show("请输入要重命名的工作表名:","MIS金质打印通(Excel专版)","sheet1");
    string newName = InputBox.Show("请输入新的名称:","MIS金质打印通(Excel专版)","sheet1"); //表示取消了
    if (name == "" || newName == "")
    {
    return;
    } Excel.Worksheet sheet = excel.WorkSheets.Rename(name,newName);
    if (sheet == null)
    {
    MessageBox.Show(name + "不存在,请重试!","MIS金质打印通(Excel专版)");
    } #endregion 实现...
    }