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."
Related
I am creating a program that uses a table of karate members from a database that allows the user to select a date from a DateTimePicker control. The program displays the first name, last name, phone #'s and dates joined of all members who joined before the selected date. I need to use a a parameterized query to retrieve the matching table rows and display them in a DataGridView Control. After that, I need add radio button that allows the user to choose before this date or on or after this date.
So far, I created the DateTimePicker control from Dates_Joined in the members data set and included the data grid view. I then added two radio buttons from the Dates_Joined in the members data set, and tried creating a query for each. It is not working properly for getting the filtered rows after specifying the date from the picker and then clicking on either the before or on or after this date radio button.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'KarateDataSet.Members' table. You can move, or remove it, as needed.
Me.MembersTableAdapter.Fill(Me.KarateDataSet.Members)
End Sub
Private Sub BeforeToolStripButton_Click(sender As Object, e As EventArgs) Handles BeforeToolStripButton.Click
Try
Me.MembersTableAdapter.Before(Me.KarateDataSet.Members, New System.Nullable(Of Date)(CType(Date_JoinedToolStripTextBox.Text, Date)))
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub FillByToolStripButton_Click(sender As Object, e As EventArgs) Handles FillByToolStripButton.Click
Try
Me.MembersTableAdapter.FillBy(Me.KarateDataSet.Members, New System.Nullable(Of Date)(CType(Date_JoinedToolStripTextBox1.Text, Date)))
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
End Class
I've made a program in VB where the user selects from a drop down box whether they would like to find out speed, distance or time. There are three textboxes on the form where the user can enter speed, distance and time, and they don't enter anything for the one which they want to find out.
When the user chooses to find out speed, I want the speed textbox to be changed to read only, when the user chooses to find out distance, I want that textbox to be changed to read only, etc.
Thanks in advance.
You can make a Textbox read only by using the following code:
textbox1.ReadOnly = true
You can also found out more here.
Handle the SelectedIndexChanged event and set the Enabled property in there.
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.TextBoxSpeed.Enabled = ComboBox1.SelectedItem <> "Speed"
Me.TextBoxTime.Enabled = ComboBox1.SelectedItem <> "Time"
Me.TextBoxDistance.Enabled = ComboBox1.SelectedItem <> "Distance"
End Sub
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
I want to display the current user name in a "Prepared By" Field(Parameter Field) of a crystal report using vb.net. How can i achieve that? I have the current username in a string variable. Now how can i link it to the Prepared By field?
Please provide me some input.
Thanks in Advance.
Give this a try, you must have the field in your report first though...
Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim Report1 As New CrystalReport1
Report1.SetParameterValue("crTextBox", Form1.TextBox1.Text)
CrystalReportViewer1.ReportSource = Report1
End Sub
In the SetParameterValue you specify the field name from your report and the field you want from your form or whatever it maybe; in your case your variable.
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.