C#实现VB6.0的输入窗体InputBox代码

备注:新建一个类,粘贴上所有的一下代码就可以调用了

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;
        }
    }


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值