VB.NET limit multiple NUD - vb.net

I have a groupbox with 5 NUD controls. I am trying to figure out if there is a way to limit the TOTAL value of all of the NUD combined, and to set a restriction to where it has a minimal amount required of them all combined. I checked my book for school and cannot seem to find it in there nor is google providing me with the answer I seek. This is not a requirement of my project, I just thought the extra touch to it would be nice. Any help would be appreciated. Thanks.
EDIT:
I tried using an array but wasn't getting the results I was looking for, still searching around to find a way for people to not able to increase the value of said NUD's when limit is reached. This is what I currently am doing and just don't like the way its handled, I would prefer them not to press a button to find out if the total is more than the requirement.
If numForest.Value + numFountain.Value + numConstruct.Value + numIntercept.Value + numWaterFall.Value > 30 Then
MessageBox.Show("Total photo prints needs to be below 30")
Else : lstPhotoOrder.Items.Add(txtOrderName.Text)

As Plutonix hinted, you can use the ValueChanged event to examine the current value of the NumericUpDown controls. The problem is, it doesn't tell you what the previous value was to fall back to, so try storing those values in a dictionary:
Private oldNums As New Dictionary(Of NumericUpDown, Decimal)
Private revertingValue As Boolean = False
Private Sub num_ValueChanged(sender As Object, e As EventArgs) Handles _
numForest.ValueChanged, _
numFountain.ValueChanged, _
numConstruct.ValueChanged, _
numIntercept.ValueChanged, _
numWaterFall.ValueChanged
If Not revertingValue Then
Dim numControl As NumericUpDown = sender
If Not oldNums.ContainsKey(numControl) Then
oldNums.Add(numControl, numControl.Minimum)
End If
If numForest.Value + numFountain.Value + numConstruct.Value + _
numIntercept.Value + numWaterFall.Value > 30 Then
MessageBox.Show("Total photo prints needs to be below 30")
revertingValue = True
Try
numControl.Value = oldNums(numControl)
Catch ex As Exception
Finally
revertingValue = False
End Try
numControl.Select()
Else
oldNums(numControl) = numControl.Value
End If
End If
End Sub

Related

Conditional increasing ID in ms access formular

i have a ms access formular where there are given several Information. For "Status" Combobox there are several options like "1","2","3","4". If "4" is selected in "cbx_Status" then I want to add in Textbox "txt_ID_Order" an automatically increased ID and give an Order Time in textbox "txt_OrderTime". That's why I wrote this and works well:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.cbx_Status.Value = "4" Then
Me.txt_OrderTime = Now()
Me.txt_ID_Order = DMax("[ID_Order]", "tblX") + 1
Else:
Me.txt_ID_Order=""
Me.txt_OrderTime = ""
End If
End Sub
However, if Status "4" is for some reason changed and again selected , I want to keep that old ID. But right know wenn i do that, it's still increasing ID.
How can I fix it?
Thanks
Check for a value:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me!cbx_Status.Value = "4" Then
If IsNull(Me!txt_OrderTime.Value) Then
Me!txt_OrderTime.Value = Now()
Me!txt_ID_Order.Value = DMax("[ID_Order]", "tblX") + 1
End If
Else
Me!txt_ID_Order.Value = Null
Me!txt_OrderTime.Value = Null
End If
End Sub
Not sure about the logic though; if you select anything else than 4, the textboxes will be cleared.

How to display my data from a text-file determined by a combo box?

I am trying to retrieve a surfer from a list on a textfile, the user selects the surfer from the combo box which then displays that surfers data in a bunch of labels. My code requires each surfer to have an ID for it to be able to know which record to retrieve from the database. However when I try to put my ID from my loop together with each surfers name, I receive an error saying "Conversion from string to type 'Long' is not valid." I have tried different methods of getting around this, none of which have worked. Here is my code:
Private Sub Lookup_Load(sender As Object, e As EventArgs) Handles MyBase.Load
madelabel = False
For i = 1 To maxrecJudge
FileGet(2, ajudge, i)
recnoJudge = i
judgename = recnoJudge And " " And ajudge.name
cmbJudge.Items.Add(judgename)
Next i
For i = 1 To maxrecSurfer
FileGet(1, asurfer, i)
recnoSurfer = i
surfername = recnoSurfer And " " And asurfer.name
cmbSurfer.Items.Add(surfername)
Next i
End Sub
Private Sub cmbSurfer_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbSurfer.SelectedIndexChanged
recnoSurfer = CInt(Val(New Text.StringBuilder((From ch In cmbSurfer.Text.ToCharArray Where IsNumeric(ch)).ToArray).ToString))
FileGet(1, asurfer, recnoSurfer)
If madelabel = False Then
lblName = New Label
lblName.Top = 160
lblName.Left = 253
lblName.Autosize = True
lblName.Text = asurfer.name
Me.Controls.Add(lblName)
End Sub
For simplicity I have only included one label creation above, but there are several labels spawned each with data of the surfer.
Am I on the right track with this? In the second sub my code extracts the integer (ID) from the combo box which then is used as a record number to find the rest of the data. The error relates to these lines:
surfername = recnoSurfer And " " And asurfer.name
cmbSurfer.Items.Add(surfername)
It won't let me concatenate the ID and the surfers name. Any help?
If you are doing a lot of concatenation, use a StringBuilder. It is far quicker. Every time you concatenation strings, a new string is created. Over a large amount of iterations, StringBuilder is the better choice.

datagridview cell is nothing after data is deleted

I have a function that automatically tallies some cells in a data grid and display the result every time a user enters a value in then. It works, however when data is deleted, the cell where the data has been deleted becomes nothing and causes error.
I could of course test for this, and may be I should, but I have a feeling that perhaps my adding code is in the wrong event?
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
If DataGridView1.IsCurrentCellDirty Then
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
For Each Grow As DataGridViewRow In Me.DataGridView1.Rows
Grow.Cells("ADJ").Value = Val(Grow.Cells("THAW1").Value.ToString) + Val(Grow.Cells("THAW2").Value.ToString) + Val(Grow.Cells("THAW3").Value.ToString) + Val(Grow.Cells("THAW4").Value.ToString) + Val(Grow.Cells("THAW5").Value.ToString) + Val(Grow.Cells("THAW6").Value.ToString) + Val(Grow.Cells("THAW7").Value.ToString) + Val(Grow.Cells("THAW8").Value.ToString) + Val(Grow.Cells("THAW9").Value.ToString) + Val(Grow.Cells("THAW10").Value.ToString) + Val(Grow.Cells("THAW11").Value.ToString) + Val(Grow.Cells("THAW12").Value.ToString) + Val(Grow.Cells("THAW13").Value.ToString) + Val(Grow.Cells("THAW14").Value.ToString) + Val(Grow.Cells("THAW15").Value.ToString)
Next
End Sub
Could someone suggest a better event to place the code into. May be its just a case of setting the cell state? Im not sure how that could be done.
Thank you
the answer is most likely to check and adjust the value prior to the calculation.
If Grow.Cells("HAW1").Value Is Nothing OrElse Grow.Cells("HAW1").Value Is DBNull.Value Then Grow.Cells("HAW1").Value = ""

For loop for bowling score array

I am trying to create a bowling program that will display the scores given in a multi-line text box. I've manage to get the program giving an output, but when it runs it skips asking for new inputs and just gives 5 0s on seperate lines and nothing else. I'm completely lost, any help is very much appreciated
EDIT: Sorry should have changed errors to reflect the programs changes, it looks like this now. It gives 0's instead of using the value I gave it, but it does ask for each input now.
For gameNumber As Integer = 1 To 5 Step 1
lblEnterScore.Text = "Enter Score for game #" & gameNumber
Dim Testint As Integer ' define an Integer for testing
Try
Testint = CInt(txtScoreInput.Text) ' try to convert whatever they entered to Int
Catch
MessageBox.Show("Entry is not an Integer") ' If you are here then the CInt failed for some reason, send a message
txtScoreInput.SelectAll()
txtScoreInput.Focus()
Exit Sub
End Try
If txtScoreInput.Text.Contains(".") Then
MsgBox("Bowling Score must be a whole number.")
txtScoreInput.SelectAll()
txtScoreInput.Focus()
Exit Sub
End If
If txtScoreInput.Text > MAXIMUM_SCORE Or txtScoreInput.Text < MINIMUM_SCORE Then
MsgBox("Bowling Score must be between 1 and 300.")
txtScoreInput.SelectAll()
txtScoreInput.Focus()
Exit Sub
End If
scoreInput(gameNumber) = CInt(txtScoreInput.Text)
' and store it in the array
' and increment the gamecounter for the next time through the loop
Next
'btnEnterScore.Enabled = False
' place the good score into the multi-line textbox
txtScoreOutputs.Text = gameScore & vbCrLf & txtScoreOutputs.Text
End Sub
If it was me, here's what I would do... Just a suggestion; I also cut out over half of your code and stopped it from throwing exceptions as well... You can put this in a click event or where ever you need it as well. You can modify this as well to take as many as you want from user input as well not just limit them from entering score's. Your user also has the option to get out of that loop when they choose to do so as well, not keeping them inside the loop...
Private ScoreLists As New List(Of Integer) 'Hold your inputted values
Private Const MAXIMUM_SCORE As Integer = 300 'Max score
Private Const MINIMUM_SCORE As Integer = 1 'Min score
Private blnStop As Boolean = False
Try
For gameNumber As Integer = 1 To 5
Dim intScore As Integer = 0
Do Until (intScore >= MINIMUM_SCORE And intScore <= MAXIMUM_SCORE) OrElse blnStop
If Not Integer.TryParse(InputBox("Please enter a score for game # " & gameNumber.ToString), intScore) Then
If MsgBox("Bowling Score must be a whole number. Stop getting scores?.", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
blnStop = True
Exit For
End If
End If
Loop
ScoreLists.Add(intScore)
Next
'Display the results...
For i As Integer = 0 To ScoreLists.Count - 1
txtScoreOutputs.Text &= ScoreLists.Item(i.ToString)
Next
ScoreLists.Clear()
Catch ex As Exception
End Try
Construct your logic like this (pseudo):
Loop
{
AskUserForInput()
ProcessUserInput()
}
The loop part will control how many scores the user is prompted to enter. The AskUserForInput() function will prompt the user to type in a new score, and the ProcessUserInput() function will get the value and store it in an array or print it to the screen, etc.
Your current logic is not waiting for any new user input before trying to add to the scores. You are also getting zeros because you're setting the txtScoreOutputs with gameScore, which doesn't look like it's been set to the user's input.

How to count Maximum rows in Visual Basic

I have the code for the next button. The data in the database show up normally.
The problem is when I click next button, the data will be repeated again as --> data1 > data2 > data3 > data1 > data2...
I've to been told that I should count the maximum rows but I didn't know how to do it; I've search for the coding as well, but nothing that I understand came out.
Please help me~~~ (I am not very good with English, sorry)
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnNext.Click
btnBack.Enabled = True
da.Fill(dt)
If position >= 0 Then
position = position + 1
Me.lblID.Text = dt.Rows(position).Item("RefNo")
Me.txtboxName.Text = dt.Rows(position).Item("Name")
Me.rtxtboxAddress.Text = dt.Rows(position).Item("Address")
Me.txtboxContactNo.Text = dt.Rows(position).Item("ContNo")
Me.txtboxFaxNo.Text = dt.Rows(position).Item("FaxNo")
Me.txtboxBrand.Text = dt.Rows(position).Item("Brand")
Me.txtboxModel.Text = dt.Rows(position).Item("Model")
Me.txtboxSN.Text = dt.Rows(position).Item("SN")
Me.rtxtboxProblems.Text = dt.Rows(position).Item("Problems")
Me.rtxtboxTechRemark.Text = dt.Rows(position).Item("TechRemark")
Me.rtxtboxServChange.Text = dt.Rows(position).Item("ServiceChange")
Me.rtxtboxPartChange.Text = dt.Rows(position).Item("PartsChange")
Me.txtboxTotal.Text = dt.Rows(position).Item("TotalPrice")
End If
End Sub
I don't know if this is also need to be told, but... there is two different class
1) database.vb - sql coding
2) forms.vb - coding for my visual basic form
Please help me!!
THANKS EVERYONE WHO HELO ME WITH THE ANSWERS!! I HAVE FOUND THE SOLUTION OF THE QUESTION AFTER RE-FIGURED THE CODING.
I didn't figure out the position value and the row value is same. My value of position = 0 and dt.Rows.Count = 4, since I have 4 data; so when the position = 0, the row = 1. I get confused about that; I thought both value is starting with 0.
A bit more code... this is bad code... don't rely on it for production since I'm sure there are plenty of corner cases it doesn't handle, but I wanted to give you a picture of how you could handle it and at least a glimpse of stuff you might need to worry about (e.g. 0 rows in the data table). Please work through this and try to understand the code... don't simply copy/paste.
Private Sub btnNext_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnNext.Click
If position >= dt.Rows.Count Then
// No more rows to show after this one, so disable the next button
btnNext.Enabled = False
End If
// Put check for zero in btnBack.Click to make sure it doesn't go below 0
// I'm assuming position starts off at 0, and that you're showing the very
// first row by default
position += 1
// This is to handle a condition like having 0 rows in the data table,
// in which case don't want to do the next part...
If position > dt.Rows.Count Then Exit Sub
// Only necessary once really, but we'll do it each time anyway...
btnBack.Enabled = True
da.Fill(dt)
position += 1
// Update various textboxes and labels...
End Sub
dt.Rows.Count is the number of rows in the Rows collection. Anytime you want to check the whether you've reached the maximum number of rows compare the row count to dt.Rows.Count
da, dt, and position should be declared outside your sub above. The Fill statement should also be executed outside the sub.
In the sub:
If position >= 0 and position < dr.rows.count then
do stuff above
else
Beep ' out of range
end if