Skip to content

Commit 1e54e75

Browse files
committed
Update IPEditBox.cs
1 parent ee6b1ae commit 1e54e75

File tree

1 file changed

+62
-36
lines changed

1 file changed

+62
-36
lines changed

src/WPFDevelopers.Shared/Controls/IPEditBox/IPEditBox.cs

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Linq;
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Windows;
35
using System.Windows.Controls;
46
using System.Windows.Input;
@@ -17,8 +19,6 @@ public class IPEditBox : Control
1719
private const string TextBox3TemplateName = "PART_TextBox3";
1820
private const string TextBox4TemplateName = "PART_TextBox4";
1921

20-
21-
2222
public string Text
2323
{
2424
get { return (string)GetValue(TextProperty); }
@@ -31,13 +31,13 @@ public string Text
3131
private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
3232
{
3333
var ctrl = d as IPEditBox;
34-
if (e.NewValue is string text && !ctrl._isChangingText)
34+
if (e.NewValue is string text && !ctrl._isChangingText)
3535
ctrl.PasteTextIPTextBox(text);
3636
}
3737

3838
private TextBox _textBox1, _textBox2, _textBox3, _textBox4;
39-
4039
private bool _isChangingText = false;
40+
private short _index = 0;
4141

4242
public override void OnApplyTemplate()
4343
{
@@ -118,6 +118,7 @@ void TextBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e)
118118
{
119119
var ip = $"{_textBox1.Text}.{_textBox2.Text}.{_textBox3.Text}.{_textBox4.Text}";
120120
Clipboard.SetText(ip);
121+
e.Handled = true;
121122
}
122123
}
123124

@@ -133,51 +134,71 @@ void ClipboardHandle()
133134

134135
void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
135136
{
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+
}
136153
if (e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control) && e.Key == Key.V)
137154
{
138155
ClipboardHandle();
156+
_isChangingText = false;
139157
e.Handled = true;
140158
}
159+
else if (e.Key == Key.Delete || e.Key == Key.Back)
160+
{
161+
_isChangingText = true;
162+
}
163+
else
164+
_isChangingText = false;
141165
}
142166

143167
void PasteTextIPTextBox(string text)
144168
{
169+
_textBox1.TextChanged -= TextBox1_TextChanged;
170+
_textBox2.TextChanged -= TextBox2_TextChanged;
171+
_textBox3.TextChanged -= TextBox3_TextChanged;
172+
_textBox4.TextChanged -= TextBox4_TextChanged;
145173
if (string.IsNullOrWhiteSpace(text))
146174
{
147175
_textBox1.Text = string.Empty;
148176
_textBox2.Text = string.Empty;
149177
_textBox3.Text = string.Empty;
150178
_textBox4.Text = string.Empty;
151-
return;
152179
}
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+
}
174195

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;
179201
}
180-
181202
void UpdateText()
182203
{
183204
var segments = new string[4]
@@ -187,10 +208,15 @@ void UpdateText()
187208
_textBox3.Text.Trim(),
188209
_textBox4.Text.Trim()
189210
};
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);
194220
}
195221
}
196222
}

0 commit comments

Comments
 (0)