473,327 Members | 2,055 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,327 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 1822
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.