Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove duplicate code on ipfield events and change key navigation beh… #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions IPUserControls/IpField.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
BorderThickness="0"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
GotKeyboardFocus="FirstByteTextBox_GotKeyboardFocus"
PreviewKeyDown="FirstByteTextBox_OnPreviewKeyDown"/>
TextChanged="TextChanged"
GotKeyboardFocus="OnGotKeyboardFocus"
PreviewKeyDown="OnPreviewKeyDown"/>

<!--Second Byte-->
<TextBox
Expand All @@ -45,8 +46,9 @@
BorderThickness="0"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
GotKeyboardFocus="SecondByteTextBox_GotKeyboardFocus"
PreviewKeyDown="SecondByteTextBox_OnPreviewKeyDown"/>
TextChanged="TextChanged"
GotKeyboardFocus="OnGotKeyboardFocus"
PreviewKeyDown="OnPreviewKeyDown"/>

<!--Third Byte-->
<TextBox
Expand All @@ -57,8 +59,9 @@
BorderThickness="0"
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
GotKeyboardFocus="ThirdByteTextBox_GotKeyboardFocus"
PreviewKeyDown="ThirdByteTextBox_OnPreviewKeyDown"/>
TextChanged="TextChanged"
GotKeyboardFocus="OnGotKeyboardFocus"
PreviewKeyDown="OnPreviewKeyDown"/>

<!--Fourth Byte-->
<TextBox
Expand All @@ -70,9 +73,10 @@
VerticalContentAlignment="Center"
HorizontalContentAlignment="Center"
HorizontalAlignment="Center"
GotKeyboardFocus="FourthByteTextBox_GotKeyboardFocus"
PreviewKeyDown="FourthByteTextBox_OnPreviewKeyDown"/>

TextChanged="TextChanged"
GotKeyboardFocus="OnGotKeyboardFocus"
PreviewKeyDown="OnPreviewKeyDown"/>

<!--Dot Separators-->
<TextBox Grid.Column="1" Text="." VerticalAlignment="Bottom" TextAlignment="Center" BorderThickness="0" Focusable="False" />
<TextBox Grid.Column="3" Text="." VerticalAlignment="Bottom" TextAlignment="Center" Padding="0" BorderThickness="0" Focusable="False" />
Expand Down
38 changes: 27 additions & 11 deletions IPUserControls/IpField.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ private void SetIpByteProperty(ref string backingField, string value, string pro

// Handles valid byte-input
else if (value.IsByte())
{
backingField = value;

}

// Is number but not a byte
else
return;
Expand Down Expand Up @@ -205,16 +207,10 @@ public string IpFourthByte
#region Events

// Select All Text On Keyboard Focus
private void FirstByteTextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) => FirstByteTextBox.SelectAll();
private void SecondByteTextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) => SecondByteTextBox.SelectAll();
private void ThirdByteTextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) => ThirdByteTextBox.SelectAll();
private void FourthByteTextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) => FourthByteTextBox.SelectAll();
private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) => (sender as TextBox).SelectAll();

// Shift text box focus on key down events
private void FirstByteTextBox_OnPreviewKeyDown(object sender, KeyEventArgs e) => ShiftTextBoxFocus((TextBox)sender, e);
private void SecondByteTextBox_OnPreviewKeyDown(object sender, KeyEventArgs e) => ShiftTextBoxFocus((TextBox)sender, e);
private void ThirdByteTextBox_OnPreviewKeyDown(object sender, KeyEventArgs e) => ShiftTextBoxFocus((TextBox)sender, e);
private void FourthByteTextBox_OnPreviewKeyDown(object sender, KeyEventArgs e) => ShiftTextBoxFocus((TextBox)sender, e);
private void OnPreviewKeyDown(object sender, KeyEventArgs e) => ShiftTextBoxFocus((TextBox)sender, e);

private static void ShiftTextBoxFocus (TextBox textBox, KeyEventArgs e)
{
Expand All @@ -233,14 +229,34 @@ private static void ShiftTextBoxFocus (TextBox textBox, KeyEventArgs e)
break;
}
case Key.Back:
if (textBox.SelectedText.Length != textBox.Text.Length)
{
if (textBox.CaretIndex == 0)
textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
}
else
{
textBox.Text = string.Empty;
}
break;
case Key.Left:
{
if (textBox.CaretIndex == 0)
textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
if (textBox.CaretIndex == 0)
textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Left));
break;
}
}
}
private void TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox.Text.Length == 3)
{
textBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
if (textBox.Text == "0")
textBox.Text = "";
}
}
#endregion Events

#region Property Notifications
Expand Down
44 changes: 18 additions & 26 deletions Prototyping Prism/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 9 additions & 13 deletions Prototyping Prism/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Prototyping Prism/Prototyping Prism.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Prototyping_Prism</RootNamespace>
<AssemblyName>Prototyping Prism</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -88,6 +89,7 @@
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="app.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
3 changes: 3 additions & 0 deletions Prototyping Prism/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>
6 changes: 3 additions & 3 deletions Prototyping/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
</configuration>
</configuration>
6 changes: 4 additions & 2 deletions Prototyping/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
xmlns:vm ="clr-namespace:Prototyping"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
mc:Ignorable="d"
Title="Test Window" Height="133.733" Width="395.733">
Title="Test Window" Height="188.37" Width="395.733">

<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="IP Address" />
<ip:IpField Name="IpField"/>
<ip:IpField Name="IpField" TabIndex="0"/>
<ip:IpPort Name="IpPort" VerticalAlignment="Center"/>
<ip:IpStatus Name="IpStatus" />
<Button Name="ConnectionButton" Content="Connect" Click="Button_Click" />
Expand All @@ -28,6 +28,8 @@
<Label Content="{Binding ElementName=IpPort, Path=PortNumber}" HorizontalAlignment="Center"/>
</StackPanel>
</StackPanel>
<Label Content="Bind To Property : "/>
<ip:IpField IpAddress="{Binding IpAddress , Mode=TwoWay}" />
</StackPanel>
</Grid>
</Window>
4 changes: 3 additions & 1 deletion Prototyping/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ public partial class MainWindow : INotifyPropertyChanged
{
public MainWindow()
{
IpAddress = "127.0.0.1";
InitializeComponent();
}

public string IpAddress { get; set; }
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(IpAddress);
if (IpStatus.ConnectionStatus != ConnectionStatus.Connected)
Connect();
else
Expand Down
43 changes: 18 additions & 25 deletions Prototyping/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading