This is my xaml structure
<StackPanel>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="A"
LostFocus="text_LostFocus"/>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="B"
LostFocus="text_LostFocus"/>
</StackPanel>
=> This structure can loop more. Such as:
<StackPanel>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="A"
LostFocus="text_LostFocus"/>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="B"
LostFocus="text_LostFocus"/>
</StackPanel>
<StackPanel>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="A"
LostFocus="text_LostFocus"/>
<m:TextBoxWithEllipsis IsEllipsisEnabled="True"
Name="B"
LostFocus="text_LostFocus"/>
</StackPanel>
In .cs file, I define event lost focus as below
private void text_LostFocus(object sender, RoutedEventArgs e)
{
TextBox textbox = ((TextBox)sender);
if (textbox.Text.Trim().Length == 0)
{
System.Windows.Forms.DialogResult result1 = System.Windows.Forms.MessageBox.Show("Empty string!", "Warning",
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
textbox.Dispatcher.BeginInvoke((Action)(() => { textbox.Focus(); }));
return;
}
textbox.ScrollToHome();
}
The problem: If there are >= 2 textbox having value is empty ("").
- I click first empty textbox => I don't enter any characters.
- Then I click second empty textbox.
==> Program always show the message box => If I click OK button, It show another. It occur forever. I can't close the program.
Question: If I have >= 2 empty textbox and I do same as problem above. How can I change the function text_LostFocus to solve the problem???
DEFAULT:
Value of these textbox is always empty (DEFAULT)
Must use BeginInvoke => Because I want when user click to texbox, user must enter least a character.
BeginInvoke. You are already on the UI thread. - Moti AzuSystem.Windows.Forms.DialogResultcan be easily replaced withSystem.Windows.MessageBox! - XAMlMAXBeginInvokedoesn't do what you say, it just runs code on a specific dispatcher.textbox.Dispatcher.BeginInvoke((Action)(() => { textbox.Focus(); }));should be replaced withtextbox.Focus();- Moti Azu