winform 文本框 限制输入类型

/// <summary>
        /// 文本框只能输入float
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TextEdit_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextEdit textEdit = sender as TextEdit;

            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.') && (e.KeyChar != '-'))
            {
                e.Handled = true; // 阻止非法字符被添加到文本框中
            }
            // 允许只输入一个小数点
            if (e.KeyChar == '.' && textEdit.Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
            // 允许只输入一个减号
            if (e.KeyChar == '-' && textEdit.Text.IndexOf('-') > -1)
            {
                e.Handled = true;
            }
        }
        /// <summary>
        /// 文本框只能输入int
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void TextEditIn_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextEdit textEdit = sender as TextEdit;
            string s = textEdit.Text;
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true; // 阻止非法字符被添加到文本框中
            }
        }


 this.txtworkcount.KeyPress += TextEditIn_KeyPress;
            this.txttorsionMax.KeyPress += TextEdit_KeyPress;
            this.txttorsionMin.KeyPress += TextEdit_KeyPress;
            this.txtangleMax.KeyPress += TextEdit_KeyPress;
            this.txtangleMin.KeyPress += TextEdit_KeyPress;

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Winform文本框是Windows窗体应用程序中常用的控件之一,用于用户输入和显示文本信息。它可以接收用户的键盘输入,并显示已经预设的文本。通过文本框,用户可以输入和编辑文本信息,例如用户名、密码、搜索关键字等等。文本框还可以设置限制条件,如限制输入的字符长度、限制只能输入数字等。 在Winform中,我们可以使用TextBox控件来创建文本框。通过TextBox控件提供的属性和方法,我们可以实现按回车键触发Button事件,清空文本框内容以及限制文本框输入。 下面是实现按回车键触发Button事件的方法:可以在TextBox的KeyDown事件中判断按下的键是否是回车键,如果是,则执行相应的操作。 下面是清空文本框内容的方法:可以使用foreach循环遍历整个窗体中的文本框控件,然后设置它们的Text属性为空。这样就可以同时清空多个文本框的内容,避免漏掉某一个文本框。 下面是限制文本框输入的方法:可以在TextBox的KeyPress事件中判断输入的字符是否符合要求,如果不符合,则取消输入。可以使用正则表达式或者判断字符的ASCII码来实现限制输入的功能。 综上所述,Winform文本框是一种常用的控件,可以用于用户输入和显示文本信息。我们可以通过设置属性和使用相应的方法来实现文本框的各种功能,如按回车键触发Button事件、清空文本框内容和限制文本框输入。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【Winform学习笔记(二)】TextBox文本框实现按回车键触发Button事件](https://blog.csdn.net/sallyyellow/article/details/130365212)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Winform关于文本框(textbox)清空和限制输入【灰色显示】](https://blog.csdn.net/weixin_43267344/article/details/103157133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值