NumericUpDown Hides its value from the user . . .

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeNoobster
    New Member
    • Sep 2013
    • 52

    NumericUpDown Hides its value from the user . . .

    Hi everyone

    I have a button which selects a particular tab and tab page on a form. This tab page has an embedded tab control with several more pages.

    When I click on this button, the desired tab page is selected, but the NumericUpDown value (which is on the tab page) is not visible. The value is the current year.

    Any idea what could possibly be the issue? I would post code but its a hell of a lot to post.

    Could it be that somehow, the click event for the button is hiding the value?

    All idea's and suggestions are welcome because Im baffled.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Is this a WPF application wherein you have a trigger that is not bound properly that hides the control?

    -Frinny

    Comment

    • CodeNoobster
      New Member
      • Sep 2013
      • 52

      #3
      Hi Frinny, no its a simple windows form application.

      I found the source of the problem, the following code :

      Code:
              private void ClearTextBoxes(Control.ControlCollection c)
              {
                  foreach (Control ctrl in c)
                  {
                      TextBox tb = ctrl as TextBox;
                      if (tb != null)
                          tb.Text = "";
                      else
                          ClearTextBoxes(ctrl.Controls);
                  }
              }
      is used to clear all text boxes, which i have a lot of. Its an easy way to do it rather than manually clearing all of them.

      But after experimenting, I found that it also clears the numeric up down. Is a numeric up down a special kind of text box? And is there any way to exclude it? I'm trying to avoid additional lines of code.

      Thanks

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I'm glad that you solved your problem!

        I checked out the documentation for the NumericUpDown Class and I see that it inherits from System.Windows. Forms.Control not from a TextBox.

        Because you are looping through all of the Controls on the screen, the NumericUpDown control is included in this.

        Your checking for null after attempting to cast the control into a TextBox works. But you could also check that the Type of control is a TextBox before you clear it and you should be able to get around issues like that :)



        -Frinny

        Comment

        • CodeNoobster
          New Member
          • Sep 2013
          • 52

          #5
          Thank you for your solution frinny. Lol how ever, now that I check for the type of control, only a few textboxes are cleared. Interesting

          is my code incorrect?

          Code:
          foreach (Control ctrl in c)
          {
              TextBox tb = ctrl as TextBox;
                 if (tb != null)
                   {
                     if (tb is TextBox)
                        {
                           tb.Text = "";
                        }
                     else
                        {
                           ClearTextBoxes(ctrl.Controls);
                        }
                   }
          }

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Your line #3:
            Code:
            TextBox tb = ctrl as TextBox;
            The as keyword will perform certain types of conversions between compatible reference types. Which means that some of the controls on your screen could be converted into TextBoxes and so they were being cleared but they aren't actually a TextBox.

            -Frinny

            Comment

            • CodeNoobster
              New Member
              • Sep 2013
              • 52

              #7
              Hmmmmm ok man, thanks for the help, appreciate it.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                One other thing...

                In VB.NET there is a TypeOf Operator that you use to check if the type of something is what you are expecting.

                For example, I would use this code to check if the control is a TextBox:
                Code:
                For Each ctrl in c
                  If TypeOf tb Is TextBox Then
                    DirectCast(ctrl,TextBox).Text = "";
                  End If
                Next
                It looks like there is a C# version of the typeof Operator as well.

                And upon further investigation I have learned that, in C#, The following if statement will be true if the control is a TextBox or derives from TextBox:
                Code:
                foreach (Control ctrl in c)
                {
                  if (ctrl is TextBox)
                  {
                    (TextBox)tb.Text = "";
                  }
                }
                And the the following if statement will only be true if the control is a TextBox (and it will be false if the control derives from TextBox)
                Code:
                foreach (Control ctrl in c)
                {
                  if (ctrl.GetType() == typeof(TextBox))
                  {
                    (TextBox)tb.Text = "";
                  }
                }

                Comment

                • CodeNoobster
                  New Member
                  • Sep 2013
                  • 52

                  #9
                  Thanks Frinny , that works well.

                  My original solution to the problem was a bit cheap but it works aswell

                  Code:
                  Year_Selector_Cus.Value = Year_Selector_Cus.Value + 1;
                  Year_Selector_Cus.Value = Year_Selector_Cus.Value - 1;
                  I basically incremented the value then decremented it. And the value was displayed again.

                  Comment

                  Working...