Numeric Up Down Autosize not changing size
I'm a very low-level amateur - sorry if this is really basic (pun intended).
I have a numeric up down box that I wish to auto size the width, but autosize doesn't seem to do anything. Using VS Community. Here is some demonstration code of the problem (not the real code I'm using, but same behavior). The box just stays the same size. Shouldn't the box shrink and grow with the length of the number? Or am I misinterpreting what autosize does?
Code:
' ------- VB.net code -----------------
Public Class Form1
Dim x As ULong
Dim y As ULong
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
nubAutosize.AutoSize = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Application.DoEvents()
nubAutosize.Value = 1
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 10
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 1000
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 10000
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 100000
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 1000000
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 10000000
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 100000000
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 1000000000
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 10000000000
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 100000000000
For y = 1 To 100000
Application.DoEvents()
Next
nubAutosize.Value = 1000000000000
For y = 1 To 100000
Application.DoEvents()
Next
End Sub
End Class
Re: Numeric Up Down Autosize not changing size
It adapts the length of the box depending on the value you put in the .Maximum property but not the effective value. It is not dynamic like a label.
Re: Numeric Up Down Autosize not changing size
Autosize doesn't always do anything for every control. What effect are you hoping to achieve?
Re: Numeric Up Down Autosize not changing size
Quote:
Originally Posted by
Shaggy Hiker
Autosize doesn't always do anything for every control. What effect are you hoping to achieve?
Thank you very much for the answers.
To answer your question, I was hoping to achieve having the box grow according to the number of characters in it, and shrink for less characters. It looks silly to have a huge box with four digits in it. Most of the time the user will use only a four to six digit number (a counter for a loop) but if the user wants the program to run continuously, until the stop button is pressed, the box will display the maximum value the data type is capable of. Occasionally a user may choose a very large number such as ten billion or something (a betting simulator).
An alternative would be to have the box display empty (null) or a string such as "infin" but I haven't figured out how to do either one in a nud box (not at my level of experience). It has no text property in the VB.NET Windows Forms App properties set.
Re: Numeric Up Down Autosize not changing size
A NumericUpDown can only contain numbers, so there is no option for text in it.
A fairly standard way for a user to indicate an infinite number of loop iterations is to use -1 as the value; but that doesn't look too nice. A very good alternative is to use another control (perhaps a checkbox) for "infinite", and disable the NumericUpDown if it is ticked.
Re: Numeric Up Down Autosize not changing size
Also, stay away from DoEvents. If you are thinking of a very long running loop, there is a tendency to reach for DoEvents to keep the display responsive, and it CAN be used that way effectively, but it's better not to. There can be some unexpected side effects of DoEvents, so it would be better to avoid making use of that construct.
Re: Numeric Up Down Autosize not changing size
Quote:
Originally Posted by
si_the_geek
A NumericUpDown can only contain numbers, so there is no option for text in it.
A fairly standard way for a user to indicate an infinite number of loop iterations is to use -1 as the value; but that doesn't look too nice. A very good alternative is to use another control (perhaps a checkbox) for "infinite", and disable the NumericUpDown if it is ticked.
Thank you. That's a good idea. I'm not sure why they didn't think there might be a time we need to put in a text message or blank, but I can work with your idea quite well. Excellent suggestion.
Re: Numeric Up Down Autosize not changing size
Good point. Yes, I'm aware of that and have read articles on both sides of the issue. I'm planning to replace the DoEvent for the counter with other alternatives, but I also have a checkbox for the user to turn off the live counter so the loops will run much faster.
Re: Numeric Up Down Autosize not changing size
Quote:
Originally Posted by
Rzimmerman
Thank you. That's a good idea. I'm not sure why they didn't think there might be a time we need to put in a text message or blank, but I can work with your idea quite well. Excellent suggestion.
Because a NumericUpDown can only hold a number. An empty string is not a number. Technically, you can now have a nullable Decimal, but that has been a recent addition to the language. Before that, it wasn't possible to have a Decimal that was not a number, so blank wasn't an option, since the Value property of the NumericUpDown is a Decimal.