Restrict numeric input vb property grid - vb.net

I have a variable that can only be set to certain numbers (integers and decimals) and that gets assigned by the user using a property grid. What I want is something that behaves like an Enum, where the users can select from a a drop down list of acceptable values. However enums don't see to support non integer numeric input. Any ideas?

Ended up doing get/set and just changing the value if it's outside the acceptable range.
<DataMember()>
<DisplayName("Fisheye Angle")>
<Description("This value indicates the angle used in the fisheye view, if it is enabled. The acceptable range is 45 to 360 degrees with 180 degrees being the default. ")>
<DefaultValue(180)>
Public Property FisheyeAngle As Double
Get
Return _FisheyeAngle
End Get
Set(value As Double)
If value < 45 Then
_FisheyeAngle = 45
ElseIf value > 360 Then
_FisheyeAngle = 360
Else
_FisheyeAngle = value
End If
End Set
End Property

Related

ColumnWidths property in VBA does not seem to work as documented

Note - the document I reference in the title is here:ColumnWidths Property
I have a columnwidths property that I set in code based upon the items in a query for a listbox. This works fine:
colString = "0 in; 2 in; 2 in; 1 in; 0 in;"
Me.lstListbox.ColumnWidths = colString
I have a query which has quite a few columns, 44 to be exact. Two things about what happens when this ColumnWidths property is set for this query:
When I set the ColumnWidths for those 44 columns, so that my colString is "0 in; 2 in; 2 in; 1 in; 0 in;" plus about 39 other column sizes (by inch), I get an error 2176, "The setting for this property is too long." I get it that the string being applied to the property (about 267 chars) is too long, I'm guessing the limit is 255, just no place in the documentation that confirms this.
So, my string is too long to apply to the ColumnWidths property. Looking at the referenced documentation, the default is measured in points, with 72 points being an inch. Or I can specify "0; 144; 144; 72; 0;...etc", which should cut down on the length of the string I use for this property That should be a good substitute. Substituting the following, however, does not work:
colString = "0; 144; 144; 72; 0;"
Me.lstListbox.ColumnWidths = colString
The behavior of the property seems to be incorrect in the VBA documentation. That's one thing.
The other thing - any suggestions on how I can cut the length of the string applied to the property and achieve the same goal - which is set widths in the listbox for the data?

Is there a way to generate a random number using a Minimum and Maximum value using Single in VB.Net?

I'm looking for a random number generator that I can use to alter the size of a picture box on my form. Currently, I have this solution found here on StackOverflow:
Public Function GetRandomNumber(ByVal Min As Integer, ByVal Max As Integer) As Integer
Static lcGenerator As System.Random = New System.Random()
Return lcGenerator.Next(Min, Max)
End Function
As it stands, the function will generate a random integer between the range specified. This is great, but I am noticing that often the picture box doesn't change in size with a decent amount of variation. The following code shows how I am calculating variance in size:
Dim lcModifier As Decimal = (GetRandomNumber(-20, 11) * 0.01)
pbForeground.Width = CInt((0.4 - lcModifier) * pbBackground.Width)
pbForeground.Height = CInt((0.4 - lcModifier) * pbBackground.Height)
The idea is that I generate a random number between -20 and 11. This would yield a 20% increase or 10% decrease, as the minimum is inclusive but the maximum is exclusive, and I am subtracting the modifier.
I had the idea to use a Single value in place of the integer value. The Single type gave me more "room" away from 0 than a Double would, but the Next method only uses Integer values, and we're back to my initial problem.
The NextDouble method works differently to the Next method, and I am unable to specify Min and Max values, nor can I generate a negative value.
Is there any way I can generate a value using Minimum and Maximum bounds that is not close to zero?
Thanks,
sunnyCr
The NextDouble return a percentage. You just need to multiply by the range of value and then translate to the desired starting position.
Function GetRandomNumber(ByVal low As Double, high As Double) As Double
Static rng As New Random
Return (rng.NextDouble() * (high - low)) + low
End Function

vb.net working with currency values in text boxes

I've got a simple form in my app used for adding orders.
It contains two textbox controls, 'neworder_costprice' and 'neworder_saleprice'.
I also have a slider control, markup_percent', which can be a value between 0 and 100 at increments of 10.
I'm trying to make it so if the user types in "1.20" for example in the costprice textbox, the saleprice textbox will automatically populate with the value of costprice + markup_percent.
I've tried a few different ways of getting this to work, but nothing seems to want to do it for me! Can anyone point out the error of my ways?
The following is my current code from a 'workoutsaleprice()' function which is called upon costprice.valuechanged....
tech_neworder_costprice.Text = String.Format("{0:n2}", neworder_costprice.Text)
Dim costprice As Double = neworder_costprice.Text
Dim markup As Integer = percent_slider.Value
Dim saleprice As Double = ((costprice / 100) * markup) + costprice
neworder_saleprice.Text = saleprice.ToString
Use NumericUpDown for numeric input instead of a TextBox. Validation is handled automatically so it is guaranteed to never have a non numeric value.
You would instead use the NumericUpDown.Value property of type Decimal to perform numeric operations.

Convert Textbox value to Integer

I have the following Integer variables
Dim sMaxAmount As Integer
Dim sMinAmount As Integer
I am trying to compare them with a TextBox field.
If (Convert.ToInt32(txtTransactionAmount) < sMinAmount And Convert.ToInt32(txtTransactionAmount) > sMaxAmount) Then
Although I am converting it to Integer I get exception
Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to
type 'System.IConvertible'.
What am I doing wrong?
Typo: use txtTransactionAmount.Text instead of txtTransactionAmount
If (Convert.ToInt32(txtTransactionAmount.Text) < sMinAmount AndAlso Convert.ToInt32(txtTransactionAmount.Text) > sMaxAmount)
Val(TextBox.Text) will convert the value of a TextBox to an Integer.
txtTotal.text= Val(txtPrice.text) * Val(txtQuantity.text)```
In addition to not using the Text property to get the string contained in the TextBox, there are two other problems with your code.
You are not validating the contents of the TextBox. If the user enters something that can't be converted to an integer, an exception will be thrown.
The test you are doing doesn't make sense given the names of the variables. The value in the TextBox can't be both less than the minimum and more than the maximum.
The following code uses Integer.TryParse to validate the contents of the TextBox and convert it to an Integer. It also checks that the value in greater than or equal to sMinAmount and less than or equal to sMaxAmount.
Dim amount As Integer
If Integer.TryParse(txtTransactionAmount.Text, amount) _
AndAlso amount >= sMinAmount AndAlso aamount <= sMaxAmount Then
'The Integer called "amount" now contains a value between sMinAmount and sMinAmount
End If

Why is my public variable not behaving as expected?

Public Class Form1
Public diameter As Integer
Public radius As Decimal = diameter / 2
Private Sub TxtRadius10_TextChanged(sender As Object, e As EventArgs) Handles TxtRadius10.TextChanged, TxtDiameter10.TextChanged
diameter = TxtDiameter10.Text
TxtRadius10.Text = radius
End Sub
End Class
The value in TxtRadius10 keeps coming back 0 when it should be 5 (The text in TxtDiameter10 box is 10 and locked). I know diameter is initializing because if I set TxtRadius10.Text = diameter/2, it will come back 5.
This isn't doing what you think it's doing:
Public radius As Decimal = diameter / 2
The radius variable doesn't hold a reference to the diameter variable for future calculations. This assignment statement calculates it once with the current values and places the result in radius. The current value of diameter is:
Public diameter As Integer
Well, it's unassigned. So it's defaulting to 0. And 0 / 2 is 0. So radius is being initialized to 0, and it's never assigned again in the code. So it will always be 0.
If you want to dynamically calculate the value every time it's accessed, make it a property:
Public Property radius As Decimal
Get
Return diameter / 2
End Get
End Property
Now any reference to radius will run the Get logic whenever it's accessed and re-calculate the value on the fly.
You've got yourself an unintended loop.
When you change the value of TxtRadius10.Text in the TxtRadius10_TextChanged event, it fires off the event again. Since diameter starts off as 0 (the default value of an integer), the radius value starts off as 0 (0 / 2).
You should also know that Radius is not going to update when you update diameter. You've just set an initial value for it. You've got to recalculate the radius whenever diameter changes.
You're declaring radius = diameter/ 2
However diameter isn't defined when it comes across this statement.
Define diameter first before defining radius