• winform 自定义数值(数字)输入框


    最近写winform程序,发现自带的TextBox没发设置输入类型,这就有点痛苦了,查找了网上,没看到比较符合需求的;于是花了点时间,自己自定义了一个简单的数值输入框,实现的方式很简单,继承系统 的TextBox,重写OnTextChanged方法限制输入即可,代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Widgets
    {
        /// 
        /// 数值输入框
        /// 
        public class NumberTextBox : TextBox
        {
            /// 
            /// 输入数值类型
            /// 
            private NumberType numberType;
    
            /// 
            /// 输入小数点,默认2位
            /// 
            private int decimalPointNumber;
    
            /// 
            /// 允许输入的最小值,空值即代表当前类型的最小值
            /// 
            private string minValue;
    
            /// 
            /// 允许输入的最大值,空值即代表当前类型的最大值
            /// 
            private string maxValue;
    
            [Browsable(true)]
            [Category("NumberTextBox"), Description("指定控件数值输入的最小值"), DisplayName("最小值")]
            public string MinValue
            {
                get
                {
                    return minValue;
    
                }
                set
                {
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        minValue = "";
                    }
                    else if (numberType == NumberType.Integer)
                    {
                        if (int.TryParse(value, out int result))
                        {
                            minValue = result.ToString();
                        }
                        else
                        {
                            minValue = "";
                        }
    
                    }
                    else if (numberType == NumberType.Long)
                    {
                        if (long.TryParse(value, out long result))
                        {
                            minValue = result.ToString();
                        }
                        else
                        {
                            minValue = "";
                        }
    
                    }
                    else if (numberType == NumberType.Float)
                    {
                        if (float.TryParse(value, out float result))
                        {
                            minValue = result.ToString("F" + decimalPointNumber);
                        }
                        else
                        {
                            minValue = "";
                        }
    
                    }
                    else if (numberType == NumberType.Double)
                    {
                        if (double.TryParse(value, out double result))
                        {
                            minValue = result.ToString("F" + decimalPointNumber);
                        }
                        else
                        {
                            minValue = "";
                        }
                    }
                }
            }
    
            [Browsable(true)]
            [Category("NumberTextBox"), Description("指定控件数值输入的最大值"), DisplayName("最大值")]
            public string MaxValue
            {
                get
                {
                    return maxValue;
                }
                set
                {
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        maxValue = "";
                    }
                    else if (numberType == NumberType.Integer)
                    {
                        if (int.TryParse(value, out int result))
                        {
                            maxValue = result.ToString();
                        }
                        else
                        {
                            maxValue = "";
                        }
    
                    }
                    else if (numberType == NumberType.Long)
                    {
                        if (long.TryParse(value, out long result))
                        {
                            maxValue = result.ToString();
                        }
                        else
                        {
                            maxValue = "";
                        }
    
                    }
                    else if (numberType == NumberType.Float)
                    {
                        if (float.TryParse(value, out float result))
                        {
                            maxValue = result.ToString("F" + decimalPointNumber);
                        }
                        else
                        {
                            maxValue = "";
                        }
    
                    }
                    else if (numberType == NumberType.Double)
                    {
                        if (double.TryParse(value, out double result))
                        {
                            maxValue = result.ToString("F" + decimalPointNumber);
                        }
                        else
                        {
                            maxValue = "";
                        }
                    }
                }
            }
    
            [Browsable(true)]
            [Category("NumberTextBox"), Description("指定控件数值输入类型"), DisplayName("数值类型")]
            public NumberType NumberType { get => numberType; set => numberType = value; }
    
            [Browsable(true)]
            [Category("NumberTextBox"), Description("当指定输入数值类型为小数时保留小数点位数"), DisplayName("小数点位数")]
            [DefaultValue(2)]
            public int DecimalPointNumber { get => decimalPointNumber; set => decimalPointNumber = value; }
    
            protected override void OnTextChanged(EventArgs e)
            {
                base.OnTextChanged(e);
                if (string.IsNullOrWhiteSpace(Text) || Text == "-")//输入负值
                {
                    return;
                }
                switch (numberType)
                {
                    case NumberType.Integer:
                        if (!int.TryParse(Text, out var i))
                        {
                            Text = Text.Substring(0, Text.Length - 1);
                            SelectionStart = Text.Length;
                        }
                        break;
                    case NumberType.Long:
                        if (!long.TryParse(Text, out var l))
                        {
                            Text = Text.Substring(0, Text.Length - 1);
                            SelectionStart = Text.Length;
                        }
                        break;
                    case NumberType.Float:
                        if (!float.TryParse(Text, out var f))
                        {
                            Text = Text.Substring(0, Text.Length - 1);
                            SelectionStart = Text.Length;
                        }
                        break;
                    case NumberType.Double:
                        if (!double.TryParse(Text, out var d))
                        {
                            Text = Text.Substring(0, Text.Length - 1);
                            SelectionStart = Text.Length;
                        }
                        break;
                }
            }
    
            protected override void OnKeyDown(KeyEventArgs e)
            {
                base.OnKeyDown(e);
                if (e.KeyCode == Keys.Enter)
                {
                    VerifyThenFormatValue();
                }
            }
    
            protected override void OnLostFocus(EventArgs e)
            {
                base.OnLostFocus(e);
                VerifyThenFormatValue();
    
            }
    
            private void VerifyThenFormatValue()
            {
                if (string.IsNullOrWhiteSpace(Text))
                {
                    return;
                }
                if (numberType == NumberType.Float)
                {
                    Text = float.Parse(Text).ToString("F" + decimalPointNumber).ToString();
                }
                else if (numberType == NumberType.Double)
                {
                    Text = double.Parse(Text).ToString("F" + decimalPointNumber).ToString();
                }
                if (!string.IsNullOrWhiteSpace(minValue))
                {
                    if (numberType == NumberType.Integer)
                    {
                        if (int.Parse(Text) < int.Parse(minValue))
                        {
                            Text = minValue;
                        }
                    }
                    else if (numberType == NumberType.Long)
                    {
                        if (long.Parse(Text) < long.Parse(minValue))
                        {
                            Text = MinValue;
                        }
                    }
                    else if (numberType == NumberType.Float)
                    {
                        if (float.Parse(Text) < float.Parse(minValue))
                        {
                            Text = minValue;
                        }
                    }
                    else if (numberType == NumberType.Double)
                    {
                        if (double.Parse(Text) < double.Parse(minValue))
                        {
                            Text = minValue;
                        }
                    }
                }
                if (!string.IsNullOrWhiteSpace(maxValue))
                {
                    if (numberType == NumberType.Integer)
                    {
                        if (int.Parse(Text) > int.Parse(maxValue))
                        {
                            Text = maxValue;
                        }
                    }
                    else if (numberType == NumberType.Long)
                    {
                        if (long.Parse(Text) > long.Parse(maxValue))
                        {
                            Text = maxValue;
                        }
                    }
                    else if (numberType == NumberType.Float)
                    {
                        if (float.Parse(Text) > float.Parse(maxValue))
                        {
                            Text = maxValue;
                        }
                    }
                    else if (numberType == NumberType.Double)
                    {
                        if (double.Parse(Text) > double.Parse(maxValue))
                        {
                            Text = maxValue;
                        }
                    }
                }
                SelectionStart = Text.Length;
            }
    
            /// 
            /// 复写Text,确保值是符合我们预设的类型
            /// 
            public override string Text 
            { 
                get => base.Text; 
                set 
                {
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        base.Text = value;
                    }
                    else
                    {
                        if (numberType == NumberType.Integer)
                        {
                            if (int.TryParse(value,out var result))
                            {
                                base.Text = result.ToString();
                            }
                            else
                            {
                                base.Text = "";
                            }
                        }
                        if (numberType == NumberType.Long)
                        {
                            if (long.TryParse(value, out var result))
                            {
                                base.Text = result.ToString();
                            }
                            else
                            {
                                base.Text = "";
                            }
                        }
                        else if (numberType == NumberType.Float)
                        {
                            if (float.TryParse(value, out var result))
                            {
                                base.Text = result.ToString("F"+decimalPointNumber);
                            }
                            else
                            {
                                base.Text = "";
                            }
                        }
                        else if (numberType == NumberType.Double)
                        {
                            if (double.TryParse(value, out var result))
                            {
                                base.Text = result.ToString("F" + decimalPointNumber);
                            }
                            else
                            {
                                base.Text = "";
                            }
                        }
                    }               
                } 
            }
    
            [Browsable(false)]
            [DefaultValue(0)]
            public int IntValue
            {
                get
                {
                    if (string.IsNullOrWhiteSpace(Text) || numberType != NumberType.Integer)
                    {
                        return 0;
                    }
                    VerifyThenFormatValue();
                    return int.Parse(Text);
                }
                set
                {
                    Text = value.ToString();
                    VerifyThenFormatValue();
                }
            }
    
            [Browsable(false)]
            [DefaultValue(0)]
            public long LongValue 
            {
                get
                {
                    if (string.IsNullOrWhiteSpace(Text) || numberType != NumberType.Long)
                    {
                        return 0;
                    }
                    VerifyThenFormatValue();
                    return long.Parse(Text);
                }
                set
                {
                    Text = value.ToString();
                    VerifyThenFormatValue();
                }
            }
    
            [Browsable(false)]
            [DefaultValue(0)]
            public float FloatValue
            {
                get
                {
                    if (string.IsNullOrWhiteSpace(Text) || numberType != NumberType.Float)
                    {
                        return 0;
                    }
                    VerifyThenFormatValue();
                    return float.Parse(Text);
                }
                set
                {
                    Text = value.ToString();
                    VerifyThenFormatValue();
                }
            }
            [Browsable(false)]
            [DefaultValue(0)]
            public double DoubleValue
            {
                get
                {
                    if (string.IsNullOrWhiteSpace(Text) || numberType != NumberType.Double)
                    {
                        return 0;
                    }
                    VerifyThenFormatValue();
                    return double.Parse(Text);
                }
                set
                {
                    Text = value.ToString();
                    VerifyThenFormatValue();
                }
            }
    
        }
    
        public enum NumberType
        {
            Integer, Long, Float, Double
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457

    该输入框可实现不同类型数值输入,包括整数,小数(可设置保留位数),还可以限制输入的最大最小值

    使用方式:

    1. 拷贝代码到工程中,编译项目
    2. 打开工具箱,找到NumberTextBox拖到界面中即可
    3. 点击控件,可在属性栏中设置输入类型,最大最小值等相关属性
    4. 根据自己定义的输入类型调用IntValue等方法获取值即可
  • 相关阅读:
    Nginx系列教程(一)| 手把手教你在Linux环境下搭建Nginx服务
    python 多进程并行处理
    windows中怎样使用nginx?
    数据分析方法总结
    scau Java综合性实验之Java源程序分析程序
    客户端单元测试实践 — C++篇
    Redis面试问题汇总
    ChatGPT App迎来重大更新;人工智能应用于应对气候变化
    【计算机毕设小程序案例】基于微信小程序的图书馆座位预定系统
    sql:group by和聚合函数的使用
  • 原文地址:https://blog.csdn.net/ngl272/article/details/126430750