国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - C#自定義IP輸入框控件

C#自定義IP輸入框控件

2022-02-23 13:41程序猿老黃 C#

這篇文章主要為大家詳細介紹了C#自定義IP輸入框控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下

場景: 做一些網絡設備應用時,需要有ip地址的輸入,這就需要ip地址輸入框控件

思路:

       1 重寫TextBox 為IpInputBox。

        2 重寫TextBox為SubIpInputBox

        3 一個IpInputBox 添加4個SubIpInputBox 和3個Label

控件圖:

C#自定義IP輸入框控件

具體代碼:

主窗口文件   Dialog.cs

?
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace IP
{
  public delegate void FallBackEvent(IpInputBox box, int flag);
 
  public partial class Dialog : Form
  {
    public Dialog()
    {
      InitializeComponent();
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
      this.Controls.Add(ipBox);
      ipBox.Location = new Point(10,10);
    }
 
    private IpInputBox ipBox = new IpInputBox(false);
  }
}

IpInputBox.cs 文件

?
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
 
namespace IP
{
  public class IpInputBox :TextBox
  {
    private bool _isNetmask = false;
    public bool IsNetmask
    {
      get { return _isNetmask; }
      set { _isNetmask = value; }
    }
    public IpInputBox(bool isNetmask)
    {
      _isNetmask = isNetmask;
 
      this.Size = new System.Drawing.Size(150, 25);
 
      _dotLabel1.Text = ".";
      _dotLabel2.Text = ".";
      _dotLabel3.Text = ".";
 
      _dotLabel1.Size = new System.Drawing.Size(10, 25);
      _dotLabel2.Size = new System.Drawing.Size(10, 25);
      _dotLabel3.Size = new System.Drawing.Size(10, 25);
 
      _box1.IsNetmask = _isNetmask;
      _box2.IsNetmask = _isNetmask;
      _box3.IsNetmask = _isNetmask;
      _box4.IsNetmask = _isNetmask;
 
      _box1.Flag = 1;
      _box2.Flag = 2;
      _box3.Flag = 3;
      _box4.Flag = 4;
 
 
      this.Controls.Add(_box1);
      this.Controls.Add(_dotLabel1);
 
 
      this.Controls.Add(_box2);
      this.Controls.Add(_dotLabel2);
 
 
      this.Controls.Add(_box3);
      this.Controls.Add(_dotLabel3);
 
      this.Controls.Add(_box4);
 
      this.Font = new System.Drawing.Font(this.Font.Name, 11);
      _box1.Location = new System.Drawing.Point(-1, 2);
      _dotLabel1.Location = new System.Drawing.Point(29, 2);
      _box2.Location = new System.Drawing.Point(39, 2);
      _dotLabel2.Location = new System.Drawing.Point(69, 2);
      _box3.Location = new System.Drawing.Point(79, 2);
      _dotLabel3.Location = new System.Drawing.Point(109, 2);
      _box4.Location = new System.Drawing.Point(119, 2);
 
      _box1.Box = this;
      _box2.Box = this;
      _box3.Box = this;
      _box4.Box = this;
    }
    public void FallBackEventFun(IpInputBox box, int flag)
    {
      switch (flag)
      {
        case 1:
          _box1.Focus();
          break;
        case 2:
          _box1.Focus();
          break;
        case 3:
          _box2.Focus(); ;
          break;
        case 4:
          _box3.Focus(); ;
          break;
      }
    }
 
    private string _ipAddress = string.Empty;
    public void UpDateIpaddress()
    {
      try
      {
        string[] sArray = ipAddress.Split(new char[] { '.' });
        _box1.Text = sArray[0];
        _box2.Text = sArray[1];
        _box3.Text = sArray[2];
        _box4.Text = sArray[3];
      }
      catch (Exception exp)
      {
        MessageBox.Show("更新字符串失敗:" + exp.Message);
      }
    }
 
    /// <summary>
    /// 獲取ip地址
    /// </summary>
    public string IpAddressString
    {
      get
      {
        string _ipStr1 = _box1.Text;
        string _ipStr2 = _box2.Text;
        string _ipStr3 = _box3.Text;
        string _ipStr4 = _box4.Text;
        string _ipDotStr = ".";
        _ipAddress = _ipStr1 + _ipDotStr + _ipStr2 + _ipDotStr + _ipStr3 + _ipDotStr + _ipStr4;
        return _ipAddress;
      }
      set
      {
        _ipAddress = value;
      }
    }
    private string ipAddress = string.Empty;
 
    public string IpAddress
    {
      get { return ipAddress; }
      set { ipAddress = value; }
    }
 
    private SubIpInputBox _box1 = new SubIpInputBox("");
    private SubIpInputBox _box2 = new SubIpInputBox("");
    private SubIpInputBox _box3 = new SubIpInputBox("");
    private SubIpInputBox _box4 = new SubIpInputBox("");
 
    private Label _dotLabel1 = new Label();
    private Label _dotLabel2 = new Label();
    private Label _dotLabel3 = new Label();
 
  }
}

SubIpInputBox.cs 文件

 

?
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
 
namespace IP
{
  public class SubIpInputBox:TextBox
  {
      /// <summary>
      /// 判斷是否是子網掩碼,false表示ip地址,true表示子網掩碼
      /// </summary>
      private bool _isNetmask = false;
      private bool _isSendKey = false;
      private System.ComponentModel.IContainer components;
      private int _flag = 0;
 
      public int Flag
      {
        get { return _flag; }
        set { _flag = value; }
      }
 
      /// <summary>
      /// 
      /// </summary>
      public event FallBackEvent TextFallBackEvent;
 
 
      //定義事件函數
      public void FallBackEventFun(int flag)
      {
        if (TextFallBackEvent != null)
        {
          TextFallBackEvent(box, this.Flag);
        }
      }
 
      public bool IsNetmask
      {
        get { return _isNetmask; }
        set { _isNetmask = value; }
      }
 
      /// <summary>
      /// 構造函數
      /// </summary>
      public SubIpInputBox(bool isNetmask)
      {
        _isNetmask = isNetmask;
        box = new IpInputBox(_isNetmask);
        this.Font = new System.Drawing.Font(this.Font.Name, 11);
        this.BorderStyle = System.Windows.Forms.BorderStyle.None;//去掉邊框
        this.TextAlign = HorizontalAlignment.Center;//字體居中
        this.Size = new System.Drawing.Size(30, 25);
        this.MaxLength = 3;
      }
 
      public SubIpInputBox(string str)
      {
        this.Text = str;
        this.Size = new System.Drawing.Size(30, 25);
        this.MaxLength = 3;
        this.BorderStyle = System.Windows.Forms.BorderStyle.None;//去掉邊框
        this.TextAlign = HorizontalAlignment.Center;//字體居中
      }
 
      private IpInputBox box;
      public IpInputBox Box
      {
        get { return box; }
        set { box = value; }
      }
 
      protected override void OnKeyUp(KeyEventArgs e)
      {
        base.OnKeyUp(e);
 
 
        if (this.Text == "")
        {
          if (e.KeyCode.ToString() == "Back")
          {
            this.TextFallBackEvent += new FallBackEvent(box.FallBackEventFun);
            this.FallBackEventFun(this.Flag);
          }
        }
      }
      protected override void OnKeyPress(KeyPressEventArgs e)
      {
        base.OnKeyPress(e);
 
        //阻止從鍵盤輸入鍵
        e.Handled = true;
 
 
        if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar == (char)8))
        {
 
          if ((e.KeyChar == (char)8))
          {
            e.Handled = false; return;
          }
          else
          {
            int len = this.Text.Length;
            if (len < 4)
            {
              if (len == 0 && e.KeyChar != '0')
              {
                e.Handled = false; return;
              }
              else if (len == 0)
              {
                if (this.Flag == 1 && this.IsNetmask == false)
                {
                  return;
                }
              }
              e.Handled = false; return;
            }
            else
            {
              // MessageBox.Show("編號最多只能輸入3位數字!");
            }
          }
        }
        else if (e.KeyChar == '.')
        {
          //MessageBox.Show("編號只能輸入數字!");
          if (this.Text.Length != 0)
          {
            // 如果輸入 . 就執行 TAB 鍵 
            SendKeys.SendWait("{TAB}");
          }
        }
        else if (this._isSendKey)
        {
          this.SelectAll();
        }
 
      }
 
 
      protected override void OnTextChanged(EventArgs e)
      {
 
        try
        {
          string currentStr = this.Text;
          int currentNumber = Convert.ToInt32(currentStr);
          this.Text = currentNumber.ToString();
          this.SelectionStart = currentStr.Length;//設置光標在末尾
          if (_isNetmask == false)//表示ip地址
          {
 
            if (currentNumber > 223 || (currentNumber == 0 && this.Flag == 1))
            {
              MessageBox.Show("你輸入的" + currentStr + "不是有效數值,請指定一個介于1到223之間的數值!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
 
              if (this.Flag == 1 && currentNumber == 0)
              {
                this.Text = "1";
              }
              else
              {
                this.Text = "223";
              }
              _isSendKey = true;
              this.Focus();
              this.SelectionStart = currentStr.Length;//設置光標在末尾
              this.SelectAll();
            }
            else
            {
              if (currentStr.Length == 3 && _isSendKey == false)
              {// 當輸入的字符個數為3時,跳入另外一個輸入框
                //MessageBox.Show("輸入完畢");
                if (currentNumber == 0)
                {
                  this.Text = "";
                  MessageBox.Show("000");
                }
                SendKeys.SendWait("{TAB}");
              }
            }
 
          }
          else//子網掩碼
          {
            if (currentNumber > 255)
            {
              MessageBox.Show("你輸入的" + currentStr + "不是有效數值,請指定一個介于0到255之間的數值!", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Warning);
              this.Text = "255";
              this.Focus();
              this.SelectionStart = currentStr.Length;//設置光標在末尾
              this.SelectAll();
            }
            else
            {
              if (currentStr.Length == 3 && _isSendKey == false)
              {// 當輸入的字符個數為3時,跳入另外一個輸入框
                //MessageBox.Show("輸入完畢");
                SendKeys.SendWait("{TAB}");
 
              }
            }
          }
        }
        catch
        {
          // 異常處理
        }
      }
 
      private void InitializeComponent()
      {
        this.SuspendLayout();
        // 
        // SubIpInputBox
        // 
        this.TabIndexChanged += new System.EventHandler(this.SubTextBox_TabIndexChanged);
        this.ResumeLayout(false);
      }
      private void SubTextBox_TabIndexChanged(object sender, EventArgs e)
      {
 
      }
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/taoerit/article/details/42535349

延伸 · 閱讀

精彩推薦
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
主站蜘蛛池模板: 久久伊人久久 | 五月婷婷综合网 | 久久综合久久受 | 自拍偷拍中文字幕 | 青娱乐国产视频 | 91麻豆精品国产91久久久久久久久 | 久色网| 成人免费视频视频 | 国产区视频在线观看 | 狠狠se| 国产午夜精品久久 | 亚洲综合自拍 | 在线成人亚洲 | 亚洲成av| 欧美视频一二三区 | av片在线看 | 亚洲综合中文字幕在线观看 | 中文二区| 久久大香 | 亚洲一区 | 亚洲高清免费视频 | 日本精品在线观看视频 | 久久久久国产精品一区二区 | 欧美黑人一级爽快片淫片高清 | 刺激网 | av网址在线播放 | 亚洲字幕 | 一级a毛片| 91精品国产91久久久 | 亚洲免费观看 | 久久综合九色综合欧美狠狠 | aaa级大片 | 欧美视频在线播放 | 中文字幕国产日韩 | 337p亚洲欧洲 | 国产一区二区久久久 | 免费av片网站 | 国产一区二区精品久久 | 色综合网址 | 欧美中文字幕在线 | 国产在线高清视频 |