Answered by:
Read the value in the datepicker control

Question
-
Hi,
I have a datepicker control in ClassA.
From ClassB, want to read the date in the datepicker control. How can i do this?
Thanks in advanceFriday, December 11, 2009 5:56 PM
Answers
-
Hi,
Declare the datepicker control on classA as public.
Then you can access it form ClassB by using Class'a s instance.
Ex
ClassA a = new ClassA();
DateTime dt=a.dateTimePicker1.Value;
or you can write a property on ClassA that encapsulates the value of datetimepicker
ex
public DateTime DateTimeOfControl
{
get{return dateTimePicker1.Value;}
set{dateTimePicker1.Value=value;}
}- Marked as answer by Jing0 Monday, December 21, 2009 2:10 AM
Friday, December 11, 2009 6:02 PM -
In the below code there are two ways you can get the value of datetimepicker. For first way you can create a public property and access it from first form.
Public Class Form2 Public ReadOnly Property SelectedDate() As Date Get Return DateTimePicker1.Value End Get End Property End Class Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f As New Form2 f.ShowDialog() MsgBox(f.DateTimePicker1.Value) 'First Way MsgBox(f.SelectedDate) 'Second Way End Sub End Class
I would recommend to use First way (Creating public property). Also you can create a public property or variable in module and can access that from anywhere.
Gaurav Khanna- Marked as answer by Jing0 Monday, December 21, 2009 2:11 AM
Friday, December 11, 2009 6:04 PM -
Hi,
Based on my understanding, you have two projects. You create a userControl in Project ReservationUserControls. Then use this control in your main project.
I create two projects on my side.
UserControl project has a UserControl named MyControl.vb.
The setup project is named VBTest1218.
MyControl in the UserControl project:
Public Class MyControl
Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
' use a public property to get the datetimepicker value
Public ReadOnly Property DTPtime() As DateTime
Get
Return DateTimePicker1.Value
End Get
End Property
End Class
I want to use the MyControl in my main project VBTest1218.
First, I need to add UserControl project as a reference to VBTest1218. Then I can use Mycontrol of Usercontrol project in my main project.
Click Add reference ->choose projects tab -> choose UserControl project-> Ok button
My main form:
Imports UserControl
Public Class Form1
Dim DTPcontrol As New MyControl
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(DTPcontrol)
End Sub
' show the datetimepicker out
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Console.WriteLine(DTPcontrol.DTPtime.ToString())
End Sub
End Class
Then I can get the correct datetimepicker value. (output: 2009/12/18 下午12:38:49)
You can also modify the dateTimePicker modifier property to public. Then you can get the datetimepicker directly.
MessageBox.Show(DTPcontrol.DateTimePicker1.Value.ToString())
If there is anything unclear, please feel free to tell us.
By the way, as @tamer Oz said, if this is an asp.net application, you can get a better response at asp.net forum. http://forums.asp.net
Best regards,
Ling Wang
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.- Marked as answer by Jing0 Monday, December 21, 2009 2:13 AM
Friday, December 18, 2009 4:39 AM
All replies
-
Hi,
Declare the datepicker control on classA as public.
Then you can access it form ClassB by using Class'a s instance.
Ex
ClassA a = new ClassA();
DateTime dt=a.dateTimePicker1.Value;
or you can write a property on ClassA that encapsulates the value of datetimepicker
ex
public DateTime DateTimeOfControl
{
get{return dateTimePicker1.Value;}
set{dateTimePicker1.Value=value;}
}- Marked as answer by Jing0 Monday, December 21, 2009 2:10 AM
Friday, December 11, 2009 6:02 PM -
In the below code there are two ways you can get the value of datetimepicker. For first way you can create a public property and access it from first form.
Public Class Form2 Public ReadOnly Property SelectedDate() As Date Get Return DateTimePicker1.Value End Get End Property End Class Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f As New Form2 f.ShowDialog() MsgBox(f.DateTimePicker1.Value) 'First Way MsgBox(f.SelectedDate) 'Second Way End Sub End Class
I would recommend to use First way (Creating public property). Also you can create a public property or variable in module and can access that from anywhere.
Gaurav Khanna- Marked as answer by Jing0 Monday, December 21, 2009 2:11 AM
Friday, December 11, 2009 6:04 PM -
Hi,
I tried property as you suggsted but it only returns " / / ".
The date is not returned.
when i tried datepicker.value, it says value is not a member of datepicker.
I tried to make it public but did not help. (In my case, classA is in a User control. I hope this does not change any thing in the way you access it.)
Thanks.Friday, December 11, 2009 6:41 PM -
COuld you send the structure of your user control.
Where is class a and where is datetimepicker is declared? And where do you want to access it?Friday, December 11, 2009 6:47 PM -
Hi Tamer,
UsercontrolA - This is in a different solution. I am importing this dll into the main application.
dpDemanddate is the datepicker control on this User control.
On UsercontrolA :
Public Class ReservationsLeg
Public ReadOnly Property selecteddemanddate() As String
Get
Return dpDemandDate.DateValue
End GetEnd Property
End ClassMain Application - This is a different solution from the UsercontrolA. Here i import the UsercontrolA.
imports UsercontrolA
public Class DemandReservation
Dim Nextleg as ReservationsLeg
Private Sub btnNextLeg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextLeg.Click
lbl_date.text = Nextleg.selecteddemanddate ----- returning " / / "
End Sub
End Class
Friday, December 11, 2009 6:59 PM -
Hi,
I don't get the structure of UserControlA
is it?
public class userControlA
public class ReservationsLeg
..........................
end class
End class
or
public class userControlA
End class
public class ReservationsLeg
..........................
end class
and how do you access the control on usercontrol.
By the way are you sure that your datetimepicker control has value and it is returning / / ?Friday, December 11, 2009 7:06 PM -
Hi tamer,
We have a project called ReservationUserControls.
In this project, i have a usercontrol named ReservationsLeg. In the code behind of this control, i have the public class ReservationsLeg
and how do you access the control on usercontrol.
Most of the work is done on the user control side adn we show it on the main application.
To access it, in the main application:
dim nextleg as ReservationsLeg
nextleg.property ------ properties, method calls etc.
datetimepicker does not have a value property. It says value is not a member of datepicker. I found a datevalue property so, i tried that and it was showing " / / ".Friday, December 11, 2009 8:33 PM -
Hi tamer,
I import ReservationUserControls. into the main application without which icannot access this.
As i said earlier this is in a different solution from the main application.Friday, December 11, 2009 8:36 PM -
I think your project is asp.net project
Since it is related to asp.net it is better to ask at forums.asp.net.
Also checking that if your control has value when you try to get the value is a good idea, maybe it retuns / / because it does not have any value selected.Friday, December 11, 2009 8:38 PM -
Hi,
Based on my understanding, you have two projects. You create a userControl in Project ReservationUserControls. Then use this control in your main project.
I create two projects on my side.
UserControl project has a UserControl named MyControl.vb.
The setup project is named VBTest1218.
MyControl in the UserControl project:
Public Class MyControl
Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
' use a public property to get the datetimepicker value
Public ReadOnly Property DTPtime() As DateTime
Get
Return DateTimePicker1.Value
End Get
End Property
End Class
I want to use the MyControl in my main project VBTest1218.
First, I need to add UserControl project as a reference to VBTest1218. Then I can use Mycontrol of Usercontrol project in my main project.
Click Add reference ->choose projects tab -> choose UserControl project-> Ok button
My main form:
Imports UserControl
Public Class Form1
Dim DTPcontrol As New MyControl
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(DTPcontrol)
End Sub
' show the datetimepicker out
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Console.WriteLine(DTPcontrol.DTPtime.ToString())
End Sub
End Class
Then I can get the correct datetimepicker value. (output: 2009/12/18 下午12:38:49)
You can also modify the dateTimePicker modifier property to public. Then you can get the datetimepicker directly.
MessageBox.Show(DTPcontrol.DateTimePicker1.Value.ToString())
If there is anything unclear, please feel free to tell us.
By the way, as @tamer Oz said, if this is an asp.net application, you can get a better response at asp.net forum. http://forums.asp.net
Best regards,
Ling Wang
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.- Marked as answer by Jing0 Monday, December 21, 2009 2:13 AM
Friday, December 18, 2009 4:39 AM