I'm having problems with the following: I have 2 textboxes masked to ShortDate. When I insert a date into the 1st textbox I need the 2nd textbox to display a date 2 years later.
For example, if I put 10/08/2013 the 2nd box should show 10/08/2014.
Here is the code:
Private Sub txtAcquired_LostFocus(sender As Object, e As EventArgs) Handles txtAcquired.LostFocus
Dim Acquired As Date
Acquired = txtAcquired.Text
txtDisposal.Text = Acquired.AddYears(2)
End Sub
It works fine when the day value of the date is > 9 but when I put a date with day value between 1 and 9 it does not work, e.g. if I put 04/04/2011 the 2nd box shows 40/42/013_.
If someone can help on this problem that will be much appreciated.
There is a fault with the date returned. This fixes it.
Private Sub txtAcquired_LostFocus(sender As Object, e As EventArgs) Handles txtAcquired.LostFocus
Dim Acquired As Date
If Date.TryParse(txtAcquired.Text, Acquired) Then
txtDisposal.Text = Acquired.AddYears(2).ToString("MM/dd/yyyy")
End If
End Sub
Related
I was making a cinema booking software where after I choose the seats, it stores the number of seats I selected in an integer counter.
I have two NumericUpDown controls: nudAdult & nudKids
My problem is I have to make sure the maximum value of both NumericUpDown controls can't exceed the seatNum counter.
For eg: If the number of seats I chose is 3, both values of nudAdult & nudKids cannot exceed 3 when added together. So nudAdult can be 2 and nudKids can be 1 and I can't increase anymore than that.
I would appreciate if anyone could help me or give me some pointers to solve this problem.
Thank you for any help.
Edit: This might have been a wrong approach but it worked to an extend
Private Sub nudAdult_ValueChanged(sender As Object, e As EventArgs) Handles nudAdult.ValueChanged
totalCount = Convert.ToInt32(nudAdult.Value) + Convert.ToInt32(nudKids.Value)
Call CheckIfExceed(nudAdult)
End Sub
Private Sub nudKids_ValueChanged(sender As Object, e As EventArgs) Handles nudKids.ValueChanged
totalCount = Convert.ToInt32(nudAdult.Value) + Convert.ToInt32(nudKids.Value)
Call CheckIfExceed(nudKids)
End Sub
Private Sub CheckIfExceed(c As NumericUpDown)
Dim left As Integer
If totalCount <= seatCounter Then
left = seatCounter - totalCount
c.Maximum = totalCount + left
Else
c.Maximum = c.Value - 1
End If
End Sub
You don't have to check anything. As I said, think about a physical problem. Let's say that you have 10 balls and two bags and you can put the balls into the bags. The maximum number of balls you can put in either bag is obviously the number of balls in that bag plus the number of balls not yet in either bag. That means that, when you start and both bags are empty, then maximum for each bag is the total number of balls. BOOM! You now know that you need to set the Maximum of each NumericUpDown to the total number of seats at the start.
Now, when you place a ball in a bag, the maximum number of balls that can be placed in the other bag obviously decrements by one. BOOM! You now know that when the Value of one of your NumericUpDown controls changes, you need to change the Maximum of the other the same amount in the opposite direction.
Look at that! Problem solved with less than a minute considering an analogous physical problem.
Private total As Integer 'Set total here.
Private Sub Start()
NumericUpDown1.Maximum = total
NumericUpDown2.Maximum = total
End Sub
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
NumericUpDown2.Maximum = total - NumericUpDown1.Value
End Sub
Private Sub NumericUpDown2_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown2.ValueChanged
NumericUpDown1.Maximum = total - NumericUpDown2.Value
End Sub
Here is what I have so far to populate a couple drop down boxes in visual basic.
I may need global variables but I am not sure how to do that. I have been told to use Dim and the variable names of the drop boxes but I keep getting errors saying they are already friends within the form.
*This is a homework assignment, however I am not asking you to do the assignment but simply asking help with the problem I am having within the drop boxes. I don't expect you wonderful people to earn my grades for me! Thank you for any help!
Public Class Form1
'// Mortgage Calculator Assignment 1.
'// Can calculate payments on a given loan at a given rate for a given term.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
'// Inputs rate starting at 3.25% through 6.75% at steps of 0.25% into the drop down box.
Private Sub cbRate_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbRate.SelectedIndexChanged
For i As Decimal = 3.25 To 6.75 Step 0.25
cbRate.Items.Add(i & "%")
Next
End Sub
'// Inputs term in years from 10 through 40 at steps of 5 year intervals into the drop down box.
Private Sub cbTerm_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbTerm.SelectedIndexChanged
For i As Integer = 10 To 40 Step 5
cbTerm.Items.Add(i)
Next
End Sub
End Class
You do not need global variables.
Try just populating the combo boxes in the form load:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'// Inputs rate starting at 3.25% through 6.75% at steps of 0.25%
' into the drop down
For i As Decimal = 3.25 To 6.75 Step 0.25
cbRate.Items.Add(i & "%")
Next
'// Inputs term in years from 10 through 40 at steps of 5 year intervals
' into the drop down box.
For i As Integer = 10 To 40 Step 5
cbTerm.Items.Add(i)
Next
End Sub
Edit: the combo boxes were declared (probably) for you when you added them to the form in the designer.
Could someone please check my code? i don't seem to get the correct answer after the initial run of my code.
i want it would be like.
5-1=4 then if i press the minus button again and i want the new value of my textbox to be subtracted from the difference which in case is 4.
i'm getting the right answer from the 1st process but not when i want to subtract another value from my difference.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If C <> 0 Then
Dif = C - Val(TextBox1.Text)
C = Dif
Label1.Text = Dif
TextBox1.Clear()
End If
C = Val(TextBox1.Text)
TextBox1.Clear()
End Sub
I don't know what you're trying to accomplish exactly, but there are several problems with your code. Unless it's initialized elsewhere, the first time you click Button2 the variable 'C' is zero, so the 'If' instruction is ignored, C gets the value from the textbox and then clears the textbox.
On a second click C has value (assuming that textbox wasn't empty in the first place) and then you substract the content of Textbox1 (assuming it has something) from the value of 'C', and then display it into a label.
Wouldn't it be simpler to assign the contents of TextBox1 and TextBox2 to two variables (checking in the process to see if those contents are valid, i.e. numeric) and then display the result of substracting both variables into Label1?
I have to get the days between a specific date entered by the user, and the current date in a text box.
So, the simplest code as I think will be as follows:
Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
TextBox1.Text = (DateTimePicker1.Value - Today).ToString
Now, suppose the user selects the date to be 02 March 2013, then the result of this is like 11.12:48:21 but I just want the data to be like 11 i.e without the time.
I tried changing it to Value.Day and Value.DayOfYear, but it does not give the desired output.
I just want to calculate the number of days between today, and the user selected date in a dd or ddd or dddd manner.
Can someone guide me in the right way?
I solved it using this:
TextBox1.Text = (DateTimePicker1.Value.Date - Today.Date).Days
Just one word made so much difference.
Did you mean like
Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
TextBox1.Text = DateTimePicker1.Value.AddDays(-Today.Day).ToString("dd MMM yyy")
End Sub
I have a program below that doesn't seem to be doing what I want it to do. In general, the pseudocode is: enter the number of miles (miles.text), click button, check: is the mileage entered equal to or less than the mileage radius (milestotextbox) in the database? If so, grab the truckload rate that corresponds to that radius (truckloadratetext) and display it in a textbox called "rate" (rate.text) and if not, continue looking until EOF. I've shown the code below. It lets me enter the mileage but won't check and display the result.
The data in the table looks like this:
ID MILESTO TRUCKLOADRATE
1 50 200
2 100 300
3 200 700
4 300 800
So if someone enters a mileage like 10, I want it to take the truckload rate of $200. If someone enters 250, the rate would then be 800. I'm not too hung up right now about what happens if a mileage is out of range. Just trying to figure out why the mechanics of something like this isn't working. It's my first time using records with a LOOP command so I'm trying to keep it straightforward with my program.
What could I be doing wrong? Thank you in advance and hope all has a great New Years!
Public Class Form1
Private Property EOF As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the '_test_2DataSet.test' table. You can move, or remove it, as needed.
Me.TestTableAdapter.Fill(Me._test_2DataSet.test)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Do Until EOF()
If Val(MilestoTextBox.Text) <= Val(Miles.Text) Then
rate.Text = TruckloadTextBox.Text
End If
Loop
End Sub
End Class
I neither know where you set the EOF variable nor do i understand its purpose. Have a look at following example which shows how to loop all rows of a DataTable(ORDER BY MILESTO ASC) to find the closest value greater than the given value:
Dim mileAge = Int32.Parse(Miles.Text)
Dim rate = 0
For Each row In _test_2DataSet.test
If mileAge <= row.MILESTO Then
rate = row.TRUCKLOADRATE
Exit For
End If
Next
If rate <> 0 Then
TxtRate.Text = rate.ToString
End If
If you cannot order by MILESTO initially or you simply want to see another approach that is not a database query, try this LINQ-To-DataSet approach:
rate = (From r In _test_2DataSet.test Order By r.MILESTO
Where mileAge <= r.MILESTO
Select r.TRUCKLOADRATE).FirstOrDefault
If you want to query the database, follwoing SQL returns the nearest TRUCKLOADRATE that is greater/equal than the #MileAge-parameter:
SELECT TOP (1) TRUCKLOADRATE
FROM Test
WHERE (MILESTO >= #MileAge)
ORDER BY MILESTO - #MileAge
Add a query to your DataAdapapter that returns a single value and has a meaningful name like getTruckloadRateByMileAge. Then it's that simple:
Dim loadRate = DirectCast(daTest.getTruckloadRateByMileAge(mileAge), Decimal)
Sorry for the delay in answering the question here and I want to thank everyone who answered, especially Tim.
In summary, Where a user wants to search through a dataset and compare a user-inputted value against some kind of index (in my case, if a mileage entered is less than or equal to some boundary value) and get the corresponding record that satisfies that criteria, the solution that works (thank you Tim!) is:
Dim mileAge = Int32.Parse(Miles.Text)
Dim rate = 0
For Each row In _test_2DataSet.test
If mileAge <= row.MILESTO Then
rate = row.TRUCKLOADRATE
Exit For
End If
Next
If rate <> 0 Then
TxtRate.Text = rate.ToString
End If
In the example, mileage is converted to a value that can be used to compare against a column in the dataset called MILESTO, which is a mile radius that corresponds to a price for transporting goods called TRUCKLOADRATE. The program iterates through each row in the dataset until the condition that mileage <= row.MILESTO is satisfied.
My original thought was using a search until EOF and this method works better.
Thanks all and again my apologies for the delay in summing this up for other users.