1
- using System . Linq ;
1
+ using System . Collections ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
2
4
using System . Windows ;
3
5
using System . Windows . Controls ;
4
6
using System . Windows . Input ;
@@ -17,8 +19,6 @@ public class IPEditBox : Control
17
19
private const string TextBox3TemplateName = "PART_TextBox3" ;
18
20
private const string TextBox4TemplateName = "PART_TextBox4" ;
19
21
20
-
21
-
22
22
public string Text
23
23
{
24
24
get { return ( string ) GetValue ( TextProperty ) ; }
@@ -31,13 +31,13 @@ public string Text
31
31
private static void OnTextChanged ( DependencyObject d , DependencyPropertyChangedEventArgs e )
32
32
{
33
33
var ctrl = d as IPEditBox ;
34
- if ( e . NewValue is string text && ! ctrl . _isChangingText )
34
+ if ( e . NewValue is string text && ! ctrl . _isChangingText )
35
35
ctrl . PasteTextIPTextBox ( text ) ;
36
36
}
37
37
38
38
private TextBox _textBox1 , _textBox2 , _textBox3 , _textBox4 ;
39
-
40
39
private bool _isChangingText = false ;
40
+ private short _index = 0 ;
41
41
42
42
public override void OnApplyTemplate ( )
43
43
{
@@ -118,6 +118,7 @@ void TextBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
118
118
{
119
119
var ip = $ "{ _textBox1 . Text } .{ _textBox2 . Text } .{ _textBox3 . Text } .{ _textBox4 . Text } ";
120
120
Clipboard . SetText ( ip ) ;
121
+ e . Handled = true ;
121
122
}
122
123
}
123
124
@@ -133,51 +134,71 @@ void ClipboardHandle()
133
134
134
135
void TextBox_PreviewKeyDown ( object sender , KeyEventArgs e )
135
136
{
137
+ var textbox = sender as TextBox ;
138
+ switch ( textbox . Name )
139
+ {
140
+ case TextBox1TemplateName :
141
+ _index = 0 ;
142
+ break ;
143
+ case TextBox2TemplateName :
144
+ _index = 1 ;
145
+ break ;
146
+ case TextBox3TemplateName :
147
+ _index = 2 ;
148
+ break ;
149
+ case TextBox4TemplateName :
150
+ _index = 3 ;
151
+ break ;
152
+ }
136
153
if ( e . KeyboardDevice . Modifiers . HasFlag ( ModifierKeys . Control ) && e . Key == Key . V )
137
154
{
138
155
ClipboardHandle ( ) ;
156
+ _isChangingText = false ;
139
157
e . Handled = true ;
140
158
}
159
+ else if ( e . Key == Key . Delete || e . Key == Key . Back )
160
+ {
161
+ _isChangingText = true ;
162
+ }
163
+ else
164
+ _isChangingText = false ;
141
165
}
142
166
143
167
void PasteTextIPTextBox ( string text )
144
168
{
169
+ _textBox1 . TextChanged -= TextBox1_TextChanged ;
170
+ _textBox2 . TextChanged -= TextBox2_TextChanged ;
171
+ _textBox3 . TextChanged -= TextBox3_TextChanged ;
172
+ _textBox4 . TextChanged -= TextBox4_TextChanged ;
145
173
if ( string . IsNullOrWhiteSpace ( text ) )
146
174
{
147
175
_textBox1 . Text = string . Empty ;
148
176
_textBox2 . Text = string . Empty ;
149
177
_textBox3 . Text = string . Empty ;
150
178
_textBox4 . Text = string . Empty ;
151
- return ;
152
179
}
153
- var strs = text . Split ( '.' ) ;
154
- var _textboxBoxes = new TextBox [ ] { _textBox1 , _textBox2 , _textBox3 , _textBox4 } ;
155
- for ( short i = 0 ; i < strs . Length && i < _textboxBoxes . Length ; i ++ )
156
- _textboxBoxes [ i ] . Text = strs [ i ] ;
157
- }
158
-
159
- private void TextBox4_PreviewKeyUp ( object sender , KeyEventArgs e )
160
- {
161
- UpdateText ( ) ;
162
- }
163
- private void TextBox3_PreviewKeyUp ( object sender , KeyEventArgs e )
164
- {
165
- if ( _textBox3 . Text . ToString ( ) . Length >= 3 ) _textBox4 . Focus ( ) ;
166
- UpdateText ( ) ;
167
- }
168
-
169
- private void TextBox2_PreviewKeyUp ( object sender , KeyEventArgs e )
170
- {
171
- if ( _textBox2 . Text . ToString ( ) . Length >= 3 ) _textBox3 . Focus ( ) ;
172
- UpdateText ( ) ;
173
- }
180
+ else
181
+ {
182
+ var strs = text . Split ( '.' ) ;
183
+ var _textboxBoxes = new TextBox [ ] { _textBox1 , _textBox2 , _textBox3 , _textBox4 } ;
184
+
185
+ if ( _index == 0 )
186
+ {
187
+ for ( short i = _index ; i < strs . Length && i < _textboxBoxes . Length ; i ++ )
188
+ _textboxBoxes [ i ] . Text = strs [ i ] ;
189
+ }
190
+ else
191
+ {
192
+ for ( short i = _index ; i < strs . Length && i < _textboxBoxes . Length ; i ++ )
193
+ _textboxBoxes [ i ] . Text = strs [ i - 1 ] ;
194
+ }
174
195
175
- private void TextBox1_PreviewKeyUp ( object sender , KeyEventArgs e )
176
- {
177
- if ( _textBox1 . Text . ToString ( ) . Length >= 3 ) _textBox2 . Focus ( ) ;
178
- UpdateText ( ) ;
196
+ }
197
+ _textBox1 . TextChanged += TextBox1_TextChanged ;
198
+ _textBox2 . TextChanged += TextBox2_TextChanged ;
199
+ _textBox3 . TextChanged += TextBox3_TextChanged ;
200
+ _textBox4 . TextChanged += TextBox4_TextChanged ;
179
201
}
180
-
181
202
void UpdateText ( )
182
203
{
183
204
var segments = new string [ 4 ]
@@ -187,10 +208,15 @@ void UpdateText()
187
208
_textBox3 . Text . Trim ( ) ,
188
209
_textBox4 . Text . Trim ( )
189
210
} ;
190
- _isChangingText = true ;
191
- var ips = string . Join ( "." , segments . Where ( s => ! string . IsNullOrEmpty ( s ) ) ) ;
192
- if ( ips != Text )
193
- SetValue ( TextProperty , ips ) ;
211
+ var allEmpty = segments . All ( string . IsNullOrEmpty ) ;
212
+ if ( allEmpty )
213
+ {
214
+ SetValue ( TextProperty , string . Empty ) ;
215
+ return ;
216
+ }
217
+ var ip = string . Join ( "." , segments . Where ( s => ! string . IsNullOrWhiteSpace ( s ) ) ) ;
218
+ if ( ip != Text )
219
+ SetValue ( TextProperty , ip ) ;
194
220
}
195
221
}
196
222
}
0 commit comments