preventing a numericupdown from going passed a certain number - vb.net

So i want to be able to prevent people increasing the number in a numericupdown after an assigned point is met.
e.g. in rugby the amount of conversions cant be larger then the amount of tries. so say i assigned one numericupdown as tries and had it at 4 then another as conversions. how do i limit so people cant put in anymore then 4 tries.
At the moment i have have NumericUpDowncon.Maximum = try
but it still allows the user to input more conversions then tries but then when the calculate button is pressed it puts the converions back down to the max it could be in reguards to the tries.

Assign a handler to the ValueChanged event of both NumericUpDowns. In the handler you'll have to check if conversions > tries.
Private Sub NumericUpDowncon_ValueChanged(sender as Object, e as EventArgs) _
Handles NumericUpDowncon.ValueChanged, NumericUpDowntry.ValueChanged
If NumericUpDowncon.Value > NumericUpDowntry.Value then NumericUpDowncon.Value = NumericUpDowntry.Value
End Sub

Related

Search data inside Datagridview without database - VB.net

I've searched around and couldn't find the one I've been looking for.
I have a DataGridView in VB.NET with thousands of records from MySQL Database. Now, I want to SEARCH item_description that matches or something LIKE the inputted text. I am using TEXTCHANGED
I didn't use search to database since its taking time to load.
Sample is
Search: orange
DataGridView must filter and display only with the words like "orange"
ITEM_ID
ITEM_DESCRIPTION
120
Orange Juice
832
Orange Fruit
I am thinking of getting the array list names of the column "item_description" and filter it but I don't know where to start and what codes to use. Thank you
Assuming that you have taken the advice in my comment and populated a DataTable and then bound that to the DataGridView via a BindingSource, filtering the data is a simple matter of setting the Filter property of that BindingSource. In your case, it should be something like this:
myBindingSource.Filter = "ITEM_DESCRIPTION LIKE 'orange*'"
One issue with filtering on the TextChanged event of a TextBox is that you will filter multiple times when the user types multiple characters when you only need the last one. To alleviate that, I suggest using a Timer that you start/restart each time a character is typed and then filter when it Ticks. That will enable the user to type multiple characters without filtering but also not have a significant wait after they stop typing for the filtering to be done. I recommend an interval of around 250 milliseconds but you can experiment to see what you think is best. E.g.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'Start/restart the Timer.
Timer1.Stop()
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
'Perform the filtering.
BindingSource1.Filter = $"ColumnName LIKE '{TextBox1.Text}*'"
End Sub

NumericUpDown Invalid value

I have an application in WinForms where I have many controls. These controls represents settings of application and user can save this settings by clicking on a button.
I am trying to resolve a problem with NumericUpDown (I will call it Num) control: Lets say that Num has these properties:
Minimum: 10
Maximum: 60
Step: 1
If a user want to change value, there are two ways how to do that: clicking on arrows (right side of the Num) or manually typing value directly to Num. First way is OK but most most of users are using second way and there is a little problem.
If a user type some value out of the interval for example 1, it is OK because he could continue in typing with 5 so final value is 51 and this is inside the interval. But if he stop typing after value 1, it means he type value out of the interval (1). If he click somewhere out of the Num, value (which is out of the interval) is automatically changed to the closest allowed value (in case of 1, value will be changed to 10).
But he probably will not notice this automatic change so I want to handle it somehow and inform him that he put there invalid value. But this situation is not handled by any event of Num (there is no way how to find out this invalid value he put there - if I try to read value inside ValueChanged event, it reads automatically changed value, not the invalid one).
I know I can add TextChanged event, but there is a problem that if he type some invalid value (5), it can be changed to valid value (by adding 1 so it makes 15).
Do you have any ideas how to resolve this issue? I know this is stupid but this doesn't depend on me, I have to do this and I don't know how.
use Lost Focus and instead of setting the minimum at design stage check it with an If statement.
Private Sub Num_LostFocus(sender As Object, e As System.EventArgs) Handles Num.LostFocus
If Num.Value < 10 Then MsgBox("Number is incorrect")
End Sub
alternatively you can probably create a custom control where you can override the event that changes the value to the minimum but i have never tried.
So this is my solution:
I handle TextChanged event and inside this method I assign text to its tag:
Private Sub NUDTextChanged()
Integer.TryParse(NUD.Text, NUD.Tag)
End Sub
And then:
Private Sub NUD_LostFocus() Handles NUD.LostFocus
If NUD.Tag < NUD.Minimum Or NUD.Tag > NUD.Maximum Then
' show message
End If
End Sub
Handling Validating event is useless for me because automatic change to allowed value is before validating and this automatic change fires TextChange event so after validating I have new value instead invalid one.
LostFocus is before automatic change so I can easily control if the value is valid or not.

NumericUpDown to set decimal points?

I'm trying to make a NumericUpDown box set the decimal points of a textbox and update everytime it is clicked up or down. I have tried it 2 different ways:
Me.txtCalc.Text = FormatNumber(Me.txtCalc.Text, Me.NumericUpDown1.Value)
This way works but it isn't keeping the decimal values when i increase the value. So if the textbox said 2 and is really supposed to be 2.987899. I change the NumericUpDown box from 0 to 6 and all it does is display: 2.000000.
I've also tried it this way (Which i think I'm doing wrong..)
Me.txtCalc.Text = Math.Round(CDec(Me.txtCalc.Text), CInt(Me.NumericUpDown1.Value))
This was the only way I could get it to do anything. And when I try this, everytime I click the vlaue to go up, it does absolutely nothing. But if I set it to 7 first and then add some values like: 2.987899; when i click from 7 down to 1, it removes one point at a time like it should... it just doesnt go back up. at all.
I assume I have to use Math.Round because that's the only way it will keep the decimal values...
Hopefully I explained this so you guys understand.
Any help would be appreciated.
Me.txtCalc.Text = FormatNumber(Me.txtCalc.Text, Me.NumericUpDown1.Value)
this not only formats the value, it resets the starting value for any next iteration. Assuming a start of 2.987899 going down in decimal places changes the VALUE:
2.98789
2.9878
2.987
..
2.
When "2" gets assigned to the TB, the starting value for going up is 2. you lost the rest of the decimals. You need to store in a decimal variable and format THAT to go up and down as you want:
Dim decVal As Decimal = 2.987899
Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
' now, I am not changing the underlying VALUE, just the format
TB1.Text = FormatNumber(decVal, NumericUpDown1.Value)
End Sub
I'm not sure how you get different results with Math.Round because the code shown will still assign a new value to the TB with fewer decimals and therefore lost.

Best Practice For Error Checking Controls On A Form

So I have a form with a variety of different controls (combobox, textboxes, listboxes, etc).
My first thought is to create a If, Else, End If statement. Well while that would work, it could also get pretty long, depending on the amount of controls and combinations.
Validation could include if a listbox is filled, checkbox is checked, etc pertaining to WinForms.
Is there a better solution to check all possiblities than an If statement?
It might be worthwhile to do the error checking as the user fills out the form. This could be implemented with the LostFocus event. Ex:
Private Sub btnTest_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles yourbutton.LostFocus
Dim txt = yourbutton.Text
If txt = "yourtest" Then
'do stuff
EndIf
End Sub
As above. It depends on the Validation you are trying to do. Are you validating user input, datatype lenght range, etc. Are you validating business rules. Should such and such a value equal something else. There's all kinds of possibilities.

VB.Net - How do you "dynamically" select an object?

I'm not sure if the question properly asks what I want, because I'm a bit new, but what I am trying to do is write a sub or function that I can run when a label is clicked, and I want to to be reusable on multiple labels. There will be 12 in total, and I don't wish to write the same thing over and over, with slightly different characters. A programmer never wants to write the same thing twice, right? Also, something else is making it a necessity to do it dynamically.
So, how I was trying to accomplish that was by using a string, and adding the name of the label to the string on click.
click1 = "Label1"
As it turns out, you can't just say click1.Text and return the text of Label1. The reason it's important to do it implicitly is because I need to somehow remember the one I clicked before, to compare the first click and second click, and if they match, do A, and if they don't match, do B.
The first parameter (it is called sender) to the event handler your wrote to respond to the click event is the object which sent the event.
If you assign the same routine to respond to on click of all your labels, it will be called for every one of them, but the sender parameter will point to the actual label which was clicked
HTH
mfeingold is correct, if you're unsure of the syntax:
Private Sub LabelClicked (ByVal sender As Object, ByVal e As EventArgs) Handles Label1.Click, Label2.Click, Label3.Click
sender.Text = "I've been clicked."
End Sub