Automatically updating a date field in visual basic - vb.net

So I'm trying to build an app for a builder to give new estimates to customers.
One of the features is that when he tries to create a new estimate, the quote date will get automatically get filled with the date of when the estimate is created.
And then a second date field is auto-filled to be 30 days later, so the builder can know when the quote expires. See below:
As you can see, the date fields in the Access table is not filled in.
However, it works when you change the date manually using the date picker and set it to something other than the default, see below:
now it shows up.
Here's the code I used to automatically set the dates of the date picker:
Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Adds today's date to the quote date as a defalut
QuoteDateDateTimePicker.Value = Date.Today
' Creates automatic expiry date for quote in 30 days
QuoteExpiryDateTimePicker.Value = Date.Today.AddDays(30D)
When I press the save button I expected the data would be added to the table.
Save button:
' Save Quote
Private Sub btnSaveEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveEdit.Click
' This causes the Total button to be clicked so that when
' users try to save the quote without pressing the "total"
' button it doesn't save an empty quote.
btnTotal_Click(sender, e)
' Save quote
QuotesBindingSource.EndEdit()
If TotalTextBox.Text = "" Then
MsgBox("Please enter a some estimates")
End If
QuotesTableAdapter.Update(QuotesDataSet.quotes)
End Sub

Related

Delay textbox-textchanged in vb

I am a simple beginner and need small help:
I have textbox1 and textbox2.
Supposed when you put a number (for ex. 21) in the textbox1, I need textbox2 to give me the double number (42). I mean that I need textbox2.text=2*textbox1.text
I used this simple code :
Private Sub TextBox1_Textlength(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles TextBox1.TextChanged
TextBox2.Text = 2*TextBox1.Text
The problem is: when I wrote (in textbo1) only one digit everything it's ok, but I could not write two (or more) digits.
How do I make a delay (interval) which allows me to type a number like (1990 for example) before firing textbox1_changed?
TextChanged fires every time you change the content of the textbox. So there is no way to block this behavior. Perhaps you could add a button and move the recalculation on the button click event or better add an event handler for the Validating event.
This event is triggered when you exit from the control and you have also the option to check the input and block the exiting from the TextBox control
Private Sub textBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
Dim v as Integer
if Not Int32.TryParse(TextBox1.Text, v) Then
e.Cancel = True
MessageBox.Show("Please type a valid number")
Else
TextBox2.Text = (v * 2).ToString
End If
End Sub
Notice that when you handle the user input expecting a numeric value you should apply particular attention because you don't know what the user types. In this case the Int32.TryParse seems to be the appropriate approach.
Another suggestion is to enable immediately the Option Strict ON in your project, the default is OFF and this allows very dangerous code like treating a string like it was a number.
You don't need to. Just allow the TextChanged event to fire every time, and the second text box will always display twice the value of the first one, so when 1 is entered, it will show 2, when the 9 is added, it will show 38, and so on. You should of course address Steve's concerns about validation, and implied type conversions.

VB Listbox item information output to secondary form - all labels

I am currently working on a project that is giving me some issues. I am using a listbox that has predetermined information in its index items. When I select a item, a second form appears that gives the information about that line. Example:
List Box Selected items = Bob -----> Second form all Labels - Bob, work ID number, Job title and phone number.
the information can not be changed and is part of an array. I have the array set up and everything is ready to go except I can not figure out how to get the information to the second form.
Any help would be greatly appreciated
Try this
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim xFrm As New Form2
xFrm.Label1.Text = ListBox1.SelectedItem
xFrm.Show()
End Sub

DateTimePicker validation

I want to validate the DateTimePicker control in vb.net. I am using it for date of joining for the employee so i dont want the control to allow selection of future dates..only dates till the current date can be allowed to select. I have tried the following code:
Private Sub DateTimePickerDOJ_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePickerDOJ.ValueChanged
If DateTimePickerDOJ.Value > Date.Today Then
MessageBox.Show("You Cannot Select a Future Date!", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
The above code only displays the message correctly but its of no use becoz the future date is getting selected.
can anyone please help me out with dis problem?
You can use the DateTimePicker.MaxDate property to achieve that.
"Gets or sets the maximum date and time that can be selected in the control."

VB 2008 Displaying data in ListBox then Transfer the data to another ListBox

In VB 2008 I created 2 list box. The first list box is to load all the data in my database in a specific row, the other list box is when I double click on the data/item on the first list box the specific data/item need to be transfer to the second list box.
I manage to transfer the data, but the output it gave was wrong. Instead of the actual name of the given data/item the output it gave was System.Data.DataRowView. I tried using .ToString() but nothing happens. I used the drag and drop method for the data adapter connection and the database I'm using is MySQL. I use the "Use data bound items" on list box 1.
You should do it like this,
Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) _
Handles ListBox1.DoubleClick
' checks if the item is empty
If ListBox1.SelectedItem.ToString.Length <> 0 Then
' adds on listbox 2
ListBox2.Items.Add(ListBox1.Text)
End If
End Sub
See this,
with simple code you can use this
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
ListBox2.Items.Add(ListBox1.SelectedItem)
End Sub

Selecting Multiple dates in a DateTimePicker in vb.net?

Is there anyway i can select multiple dates in a Date Time Picker in Vb.net?
Use BoldedDates Property in your month calendar to get the array of DateTime objects. You can use this to get your collection and finally clear the bold dates after storing it, so that it can be reused.
Selecting Multiple dates in a DateTimePicker in vb.net?
If you want to select a range of dates using a DateTimePicker, then the best way if probably two DateTimePicker controls a from date and a to date.
You can then change the date of the opposite control when the first date is picked to make sure the FromDate is before the ToDate and vice versa
I found MonthCalendar control which seems to be perfect for this purpose but i cant find the option (method, property) to get the selectedDates and then use them.
If you want to use a MonthCalendar control you can specify a MaximumSelectionCount property and then the user can select dates by clicking on one and shift-clicking on another date
You can retrieve the dates the user selected by using the SelectionRange, or SelectionStart and SelectionEnd properties
How about if i want to select random dates, like 4th, 6th, 10th Feb.
This is more complex. A suggestion - you could use the BoldedDates array of the MonthCalendar and when a user clicks on a day, you bold it. You can then read the array after they have finished their selection? Maybe someone else has a suggestion for this?
Private listDates As List(Of DateTime)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Suppose that you have the following list of dates below
listDates = New List(Of DateTime)()
listDates.Add(DateTime.Now)
listDates.Add("12/22/2012")
listDates.Add(DateTime.Now.AddDays(-10))
End Sub
Protected Sub Calendar1_DayRender(ByVal sender As Object, ByVal e As DayRenderEventArgs) Handles Calendar1.DayRender
'Set Default properties
e.Day.IsSelectable = False
e.Cell.BackColor = System.Drawing.Color.Gray
'Now loop through the list of dates and make it
'Selectable
For Each d As DateTime In listDates
Calendar1.SelectedDates.Add(d)
If e.Day.IsSelected Then
e.Cell.BackColor = System.Drawing.Color.Green
e.Day.IsSelectable = True
End If
Next
End Sub
Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged
'Print the selected date
Response.Write("You have selected: " & Calendar1.SelectedDate.ToShortDateString())
End Sub
i found it, and try. it work,
original source from here
There is no way to do this inside the existing DateTimePicker control.
The best alternative solution will depend on your specific situation. What's the context? If you're expecting the user to pick dates which are close together (and the range isn't too big) a reasonably simple solution is to use another control which will allow multiselect on a list.
For example a CheckedListBox or DataGridView.
Then populate it with a list of dates. In effect offering the user a picklist of dates.