-
Notifications
You must be signed in to change notification settings - Fork 61
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
Bug fix/40328 Timeout default value #3759
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1651,7 +1651,7 @@ private void txtTimeout_TextChanged(object sender, TextChangedEventArgs e) | |||||
{ | ||||||
if (xTimeoutTextBox.Text == String.Empty || xTimeoutTextBox.Text == null) | ||||||
{ | ||||||
xTimeoutTextBox.Text = "0"; | ||||||
xTimeoutTextBox.Text = null; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider handling empty string input more robustly. The current implementation sets the text to - xTimeoutTextBox.Text = null;
+ xTimeoutTextBox.Text = ""; Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Set it to empty instead of null |
||||||
xTimeoutTextBox.CaretIndex = 1; | ||||||
} | ||||||
} | ||||||
|
@@ -2242,5 +2242,34 @@ protected override void IsVisibleChangedHandler(object sender, DependencyPropert | |||||
base.IsVisibleChangedHandler(sender, e); | ||||||
} | ||||||
} | ||||||
|
||||||
private void xTimeoutTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) | ||||||
{ | ||||||
|
||||||
bool isEmptyString = string.IsNullOrEmpty(e.Text); | ||||||
if (isEmptyString) | ||||||
{ | ||||||
//allow | ||||||
e.Handled = false; | ||||||
return; | ||||||
} | ||||||
|
||||||
string currentText = xTimeoutTextBox.Text != null ? xTimeoutTextBox.Text : string.Empty; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. allow setting timeout to empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it's allowing- |
||||||
bool isValidInteger = int.TryParse(currentText + e.Text, out _); | ||||||
if (isValidInteger) | ||||||
{ | ||||||
//allow | ||||||
e.Handled = false; | ||||||
return; | ||||||
} | ||||||
else | ||||||
{ | ||||||
//don't allow | ||||||
e.Handled = true; | ||||||
} | ||||||
|
||||||
} | ||||||
Comment on lines
+2246
to
+2271
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure input validation for timeout text box is robust. The input validation logic for the timeout text box currently allows only integer values, which is good. However, the implementation can be simplified and made more robust by using regular expressions or leveraging existing WPF features for input restrictions. Here's a simplified version using a behavior: // Add this class in a suitable place
public class NumericInputBehavior : Behavior<TextBox>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.PreviewTextInput += OnPreviewTextInput;
DataObject.AddPastingHandler(AssociatedObject, OnPaste);
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.PreviewTextInput -= OnPreviewTextInput;
DataObject.RemovePastingHandler(AssociatedObject, OnPaste);
}
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text);
}
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(string)))
{
string text = (string)e.DataObject.GetData(typeof(string));
if (!IsTextAllowed(text))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
private bool IsTextAllowed(string text)
{
return Regex.IsMatch(text, "[^0-9]+");
}
}
// Usage in XAML:
<TextBox Text="{Binding Timeout, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Behaviors>
<local:NumericInputBehavior />
</i:Interaction.Behaviors>
</TextBox> This approach not only simplifies the code but also handles copy-paste scenarios where non-numeric values might be pasted into the text box.
Comment on lines
+2245
to
+2271
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify input validation logic in The method private void xTimeoutTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
string combinedText = xTimeoutTextBox.Text + e.Text;
e.Handled = !int.TryParse(combinedText, out _); // Directly set handled based on the parse result
} |
||||||
|
||||||
|
||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review the use of
TextChanged
event for setting default values.The
TextChanged
event handler clears thexTimeoutTextBox
text unconditionally. Consider implementing a more robust method for handling default values or empty inputs to avoid potential issues with user interactions.Committable suggestion