如何通过C#控件限定处理

如何通过C#控件限定处理


作者: 张国军_Suger
开发工具与关键技术:Visual Studio 2015、C#、.NET、winfrom

      之前做项目所需就做了一下笔记,下面有文本框只限制输入数字、给DGV加序号+去空格、Ese按键退出窗体操作、回车键登录操作,这样既方便了自己,和可以帮助到朋友们。
实现截图:
文本框只限数字:
文本框只限数字
实现代码:

#region 只限数字
private void txtChuRangMianJi_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((Keys)e.KeyChar >= Keys.D0 && (Keys)e.KeyChar <= Keys.D9 ||
        (Keys)e.KeyChar == Keys.Back || (Keys)e.KeyChar == Keys.Enter
        || (Keys)e.KeyChar == (Keys)46)
    {
    }
    else
    {
        e.Handled = true;
    }
}
#endregion 

给DGV加序号+去空格:
给DGV加序号+去空格
实现代码:

#region DGV加序号+去所有空格
private void dgvDiKuaiChengJiaoJiLu_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    //给DGV添加序号
    DataGridViewRow CurrentRow = this.dgvDiKuaiChengJiaoJiLu.Rows[e.RowIndex];
    CurrentRow.HeaderCell.Value = Convert.ToString(e.RowIndex + 1);
    //去除DGV中所有的空格
    if (e.Value is string)
    {
        e.Value = e.Value.ToString().Trim();
    }
}
#endregion

Esc按钮退出窗体:
Esc按钮退出窗体
实现代码:

#region Esc按钮退出窗体
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
    int WM_KEYDOWN = 256;
    int WM_SYSKEYDOWN = 260;
    if (msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN)
    {
        switch (keyData)
        {
            case Keys.Escape:
                this.Close();//esc关闭窗体 
                break;
        }
    }
    return false;
}
#endregion

回车键登录:
回车键登录
实现代码:

#region 回车键登录
private void txtShuZiZhengShuMiMa_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        btnLogin_Click(null, null);
    }
}
#endregion
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#中,可以使用指针来批量处理控件。具体的实现方式如下: 1. 首先,需要获取控件的句柄,可以使用Control类的Handle属性来获取。 2. 然后,使用Marshal类的PtrToStructure方法将句柄转换为控件的结构体。 3. 对结构体进行批量处理,可以使用指针的方式访问结构体中的成员变量。 4. 处理完成后,使用Marshal类的StructureToPtr方法将结构体转换为句柄,然后将句柄设置回控件的Handle属性即可。 下面是一个示例代码,演示如何使用指针批量处理TextBox控件的Text属性: ```csharp using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace PointerDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 获取所有TextBox控件 foreach (Control control in Controls) { if (control is TextBox) { // 获取控件句柄 IntPtr handle = control.Handle; // 将句柄转换为控件结构体 TextBoxStruct textBoxStruct = (TextBoxStruct)Marshal.PtrToStructure(handle, typeof(TextBoxStruct)); // 批量处理Text属性 textBoxStruct.Text = "Hello world!"; // 将结构体转换为句柄,并设置回控件 Marshal.StructureToPtr(textBoxStruct, handle, true); } } } } // 定义TextBox控件结构体 [StructLayout(LayoutKind.Sequential)] public struct TextBoxStruct { public IntPtr Hwnd; public int ID; public IntPtr Text; public int MaxLength; public int Style; public int ExStyle; public int X; public int Y; public int Width; public int Height; public IntPtr Parent; public IntPtr Instance; public IntPtr Param; public int LeftMargin; public int RightMargin; public int TopMargin; public int BottomMargin; public int LimitText; public int PasswordChar; public int Visible; public int ReadOnly; public int NoHideSel; public int Lowercase; public int UpperCase; public int NoIntegralHeight; public int Multiline; public int WantReturn; public int AutoVerticalScroll; public int AutoHorizontalScroll; public int ScrollBars; public int Accelerator; public int WordWrap; public int ImeMode; public int HideSelection; public int NoNCPaint; public int OwnDC; public int NoParentNotify; public int User; } } ``` 需要注意的是,使用指针批量处理控件可能会导致不稳定的行为,因为这种方式绕过了C#的安全检查。在实际开发中,应该慎重使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张国军_Suger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值