locked
How to check which textBox is empty ,checkBox is not checked

    Question

  • Say, I have a questionnaire kind of form in which there are many textboxes, checkboxes. How to check which textbox has not been filled up (or Empty ) and which Checkbox has not been checked. Would appreciate if you can give me code or sample.

    Thanks

    Tuesday, December 2, 2014 1:57 PM

Answers

  • The problem is that you are trying to cast elements to a CheckBox/RadioButton, it should be element (without an 's'):

    private async void validate()
            {
                IEnumerable<UIElement> elements = FindVisualChildren<UIElement>(LTA_GV);
    
                foreach (UIElement element in elements)
                {
    
                    TextBox tBox = element as TextBox;
    
                    if (tBox != null)
                    {
                        if (string.IsNullOrEmpty(tBox.Text))
                        {
                            MessageDialog msg1 = new MessageDialog("Please enter something: ", "TxtBox");
                            await msg1.ShowAsync();
                        }
                        else
                        {
                            MessageDialog msg1 = new MessageDialog("content : " + tBox.Text, "TxtBox content");
                            await msg1.ShowAsync();
                        }
                    }
    
                    CheckBox cBox = element as CheckBox;
    
                    if (cBox != null)
                    {
                        if (cBox.IsChecked.Value == false)
                        {
                            MessageDialog msg = new MessageDialog("Please tick the chkBox", "CheckBox Not checked");
                            await msg.ShowAsync();
                        }
                    }
    
    
                    RadioButton rBtn = element as RadioButton;
    
                    if (rBtn != null)
                    {
                        if (rBtn.IsChecked.Value == false)
                        {
                            MessageDialog msg = new MessageDialog("Please tick the radio button", "Radio Button not checked");
                            await msg.ShowAsync();
                        }
                    }
    
    
                }
    
            }

    >>1. ( cBox != null or rBtn != null) means that the checkbox/Radio Button  is created?

    cBox != null means that the cast to a CheckBox succeeed for the current element and that it is in fact a CheckBox.

    >>2. Does it matters if the Grid contains Rows and column for the text/checkboxes/Radio button, scrollViewer and other when using your solution?

    No.

    Once again, please mark all helpful posts as answer and helpful.


    • Edited by Magnus (MM8)MVP Sunday, December 7, 2014 6:37 PM
    • Marked as answer by FireDance Monday, December 8, 2014 4:19 AM
    Sunday, December 7, 2014 6:37 PM

All replies

  • Give each input control a unique x:Name in the XAML, e.g:

    <TextBox x:Name="t1"/>
    <CheckBox x:name="c1" />

    You could then reference each control by its name:

    if(t1.Text == "some text...")
    {
    //do this
    }
    else
    {
    //do that...
    }
    
    if(c1.IsChecked == true)
    {
    //checkbox is checked...do this
    }
    else
    {
    //do that...
    }
    
    if(string.IsNullOrEmpty(t1.Text))
    {
    //t1 is empty...
    }

    Please remember to mark helpful posts as answer and/or helpful.
    Tuesday, December 2, 2014 2:33 PM
  •  I have the checkboxes and TextBoxes inside a UserControl.

    say , I don't know the checkBoxes and TextBoxes' names contain inside the Usercontrol. This userControl will be added into Grid.

    How to I iterate the Controls one by one inside the usercontrol.

    Thanks

    Wednesday, December 3, 2014 3:59 AM
  • You could use a helper method that traverses the visual tree:

    <UserControl x:Name="yourUserControl" />

          IEnumerable<UIElement> elements = FindVisualChildren<UIElement>(yourUserControl);
          foreach(UIElement element in elements)
          {
            TextBox tBox = element as TextBox;
            if (tBox != null) {
              //do something
            }
    
            CheckBox cBox = elements as CheckBox;
            if (cBox != null) {
              //do something
            }
          }
    
    
        private static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject {
          if (depObj != null) {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) {
              DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
              if (child != null && child is T) {
                yield return (T)child;
              }
    
              foreach (T childOfChild in FindVisualChildren<T>(child)) {
                yield return childOfChild;
              }
            }
          }
        }
    

    Please remember to close your threads by marking helpful posts as answer and start a new thread if you have a new question.

    Wednesday, December 3, 2014 2:26 PM
  • Hi Magnus,

    I tried your solution.  In the Grid, I have textboxes, checkboxes, radio buttons. It seems the code can detect the textbox and not Checkbox and Radio Button. Why ?

    The code :

    I have these questions:

    1. ( cBox != null or rBtn != null) means that the checkbox/Radio Button  is created?

    2. Does it matters if the Grid contains Rows and column for the text/checkboxes/Radio button, scrollViewer and other when using your solution?

    Thanks

    private async void validate()
     {
           IEnumerable<UIElement> elements = FindVisualChildren<UIElement>(LTA_GV);

             foreach (UIElement element in elements)
             {

                   TextBox tBox = element as TextBox;

                    if (tBox != null)
                    {
                        if (string.IsNullOrEmpty(tBox.Text))
                        {
                            MessageDialog msg1 = new MessageDialog("Please enter something: ", "TxtBox");
                            await msg1.ShowAsync();
                        }
                        else
                        {
                            MessageDialog msg1 = new MessageDialog("content : " + tBox.Text, "TxtBox content");
                            await msg1.ShowAsync();
                        }
                    }

                    CheckBox cBox = elements as CheckBox;

                    if (cBox != null)
                    {
                        if (cBox.IsChecked.Value == false)
                        {
                            MessageDialog msg = new MessageDialog("Please tick the chkBox", "CheckBox Not checked");
                            await msg.ShowAsync();
                        }
                    }


                    RadioButton rBtn = elements as RadioButton;

                    if (rBtn != null)
                    {
                        if (rBtn.IsChecked.Value == false)
                        {
                            MessageDialog msg = new MessageDialog("Please tick the radio button", "Radio Button not checked");
                            await msg.ShowAsync();
                        }
                    }


             }
           
         }



    • Edited by FireDance Friday, December 5, 2014 6:04 AM additional info
    Friday, December 5, 2014 4:37 AM
  • The problem is that you are trying to cast elements to a CheckBox/RadioButton, it should be element (without an 's'):

    private async void validate()
            {
                IEnumerable<UIElement> elements = FindVisualChildren<UIElement>(LTA_GV);
    
                foreach (UIElement element in elements)
                {
    
                    TextBox tBox = element as TextBox;
    
                    if (tBox != null)
                    {
                        if (string.IsNullOrEmpty(tBox.Text))
                        {
                            MessageDialog msg1 = new MessageDialog("Please enter something: ", "TxtBox");
                            await msg1.ShowAsync();
                        }
                        else
                        {
                            MessageDialog msg1 = new MessageDialog("content : " + tBox.Text, "TxtBox content");
                            await msg1.ShowAsync();
                        }
                    }
    
                    CheckBox cBox = element as CheckBox;
    
                    if (cBox != null)
                    {
                        if (cBox.IsChecked.Value == false)
                        {
                            MessageDialog msg = new MessageDialog("Please tick the chkBox", "CheckBox Not checked");
                            await msg.ShowAsync();
                        }
                    }
    
    
                    RadioButton rBtn = element as RadioButton;
    
                    if (rBtn != null)
                    {
                        if (rBtn.IsChecked.Value == false)
                        {
                            MessageDialog msg = new MessageDialog("Please tick the radio button", "Radio Button not checked");
                            await msg.ShowAsync();
                        }
                    }
    
    
                }
    
            }

    >>1. ( cBox != null or rBtn != null) means that the checkbox/Radio Button  is created?

    cBox != null means that the cast to a CheckBox succeeed for the current element and that it is in fact a CheckBox.

    >>2. Does it matters if the Grid contains Rows and column for the text/checkboxes/Radio button, scrollViewer and other when using your solution?

    No.

    Once again, please mark all helpful posts as answer and helpful.


    • Edited by Magnus (MM8)MVP Sunday, December 7, 2014 6:37 PM
    • Marked as answer by FireDance Monday, December 8, 2014 4:19 AM
    Sunday, December 7, 2014 6:37 PM
  • Thanks. take away the extra (s) in elements works.
    Monday, December 8, 2014 4:20 AM