473,395 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

NumericUpDown Hides its value from the user . . .

CodeNoobster
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.
Oct 17 '14 #1

✓ answered by Frinavale

Your line #3:
Expand|Select|Wrap|Line Numbers
  1. 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

8 1823
Frinavale
9,735 Expert Mod 8TB
Is this a WPF application wherein you have a trigger that is not bound properly that hides the control?

-Frinny
Oct 17 '14 #2
Hi Frinny, no its a simple windows form application.

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

Expand|Select|Wrap|Line Numbers
  1.         private void ClearTextBoxes(Control.ControlCollection c)
  2.         {
  3.             foreach (Control ctrl in c)
  4.             {
  5.                 TextBox tb = ctrl as TextBox;
  6.                 if (tb != null)
  7.                     tb.Text = "";
  8.                 else
  9.                     ClearTextBoxes(ctrl.Controls);
  10.             }
  11.         }
  12.  
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
Oct 18 '14 #3
Frinavale
9,735 Expert Mod 8TB
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
Oct 20 '14 #4
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?

Expand|Select|Wrap|Line Numbers
  1. foreach (Control ctrl in c)
  2. {
  3.     TextBox tb = ctrl as TextBox;
  4.        if (tb != null)
  5.          {
  6.            if (tb is TextBox)
  7.               {
  8.                  tb.Text = "";
  9.               }
  10.            else
  11.               {
  12.                  ClearTextBoxes(ctrl.Controls);
  13.               }
  14.          }
  15. }
  16.  
Oct 21 '14 #5
Frinavale
9,735 Expert Mod 8TB
Your line #3:
Expand|Select|Wrap|Line Numbers
  1. 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
Oct 21 '14 #6
Hmmmmm ok man, thanks for the help, appreciate it.
Oct 21 '14 #7
Frinavale
9,735 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. For Each ctrl in c
  2.   If TypeOf tb Is TextBox Then
  3.     DirectCast(ctrl,TextBox).Text = "";
  4.   End If
  5. 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:
Expand|Select|Wrap|Line Numbers
  1. foreach (Control ctrl in c)
  2. {
  3.   if (ctrl is TextBox)
  4.   {
  5.     (TextBox)tb.Text = "";
  6.   }
  7. }
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)
Expand|Select|Wrap|Line Numbers
  1. foreach (Control ctrl in c)
  2. {
  3.   if (ctrl.GetType() == typeof(TextBox))
  4.   {
  5.     (TextBox)tb.Text = "";
  6.   }
  7. }
Oct 21 '14 #8
Thanks Frinny , that works well.

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

Expand|Select|Wrap|Line Numbers
  1. Year_Selector_Cus.Value = Year_Selector_Cus.Value + 1;
  2. Year_Selector_Cus.Value = Year_Selector_Cus.Value - 1;
  3.  
I basically incremented the value then decremented it. And the value was displayed again.
Oct 23 '14 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Nemo | last post by:
I have problem to get my dates from my user control. The problems is that the datetimes sets after when I get the values from my aspx page. Here is how my user control lock likes. private...
5
by: Len Weltman | last post by:
I am trying to pass a NumericUpDown object into a class method using Visual Studio 2005, but the control type is not found in Intellisense and the type declaration is flagged as an error. Here...
1
by: GAZ | last post by:
Hello all, We have a bit of a problem with application settings and user control. In short, we have developed a user control that should, as it happenes, get a value stored in application...
2
by: Stephan Zaubzer | last post by:
Hi all, I encountered a problem with NumericUpDown yesterday and managed to reproduce the error with a very easy examle. I have a Windows Application with only one Form, which contains only one...
6
by: KDCinfo | last post by:
Although I'm making an ajax call, this is really a javascript question (although it could be even more of an HTML or DOM question... not exactly sure) I'm doing an ajax call to a remote php...
2
by: underground | last post by:
Hi, everyone I've been trying to figure out a way for a user to update there information. I'm using sections to identify the specific user..Here is the form <? include("include/session.php");...
1
by: mattmao | last post by:
Hello everyone. I am new to J2EE and I am asked to do this:"Display two bean properties in a JSP file, the value of the username should be retrieved from the request.getRemoteUser();" I got: ...
3
by: prashantdwivedi | last post by:
i want to write a program that display data type of enterd value that enter by user.E.G. if user enter 2 then our program will display it is an integer value,if user enter 10.5 then our program will...
0
jsos20
by: jsos20 | last post by:
start_form("get"); #will start the form print "What's the User name? ",textfield(-name=>'Username',-value=>'user'), #will get the user name p, #this gives a new paragraph ...
5
by: madhanrajesh210002 | last post by:
I'm trying to calculate dynamic formula value for dynamic inputs, that is user inputs and formulla all will be given by user, i tried in php, but failed , codes as follows, $a=10; //$a -...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.