Results 1 to 6 of 6

Thread: [RESOLVED] [2005] User Control Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    In my house
    Posts
    70

    Resolved [RESOLVED] [2005] User Control Question

    Hello,

    I put together a user control with two date pickers and labels. I am to set the labels text property and get the number of days between the date ranges. I have the code to do this, but when I test the control, the labels do not have the new text property. I haven't tested the date range portion yet. I have followed the steps in the text book on building the solution so that the control is available when using the form. Could someone let me know what I'm doing wrong with setting the label text? Here is my control code:

    VB Code:
    1. Public Class DateRange
    2.  
    3.     Public WriteOnly Property PickUp() As String
    4.         Set(ByVal value As String)
    5.             Label1.Text = "PickUp Day"
    6.         End Set
    7.     End Property
    8.  
    9.     Public WriteOnly Property DropOff() As String
    10.         Set(ByVal value As String)
    11.             Label2.Text = "DropOff Day"
    12.         End Set
    13.     End Property
    14.  
    15.     Public ReadOnly Property GetDays() As String
    16.         Get
    17.             'Declare variables
    18.             Dim DayCount As Integer
    19.  
    20.             'Determine number of days
    21.             DayCount = CInt((DateDiff(DateInterval.Day, DateTimePicker1.Value, DateTimePicker2.Value)))
    22.  
    23.             Return CStr(DayCount)
    24.  
    25.         End Get
    26.     End Property
    27. End Class

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] User Control Question

    You're setting the Text of the Labels to a predefined string no matter what the caller passes. The 'value' argument contains the string the the caller is trying to set, so you have to assing that to the Label's Text property:
    VB Code:
    1. Public WriteOnly Property PickUp() As String
    2.         Set(ByVal value As String)
    3.             Label1.Text = [U]value[/U]
    4.         End Set
    5.     End Property
    6.  
    7.     Public WriteOnly Property DropOff() As String
    8.         Set(ByVal value As String)
    9.             Label2.Text = [U]value[/U]
    10.         End Set
    11.     End Property
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    In my house
    Posts
    70

    Re: [2005] User Control Question

    Hello,

    Sorry, I'm a little confused. Where does "Value" come from? Do I set this through the controls property window (the label text field) for each label then use the Get and Set portion in the public property? I don't see where else "Value" would come from.

    Thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] User Control Question

    You don't do anything. It's is the parameter passed to the Set method:
    VB Code:
    1. Public WriteOnly Property PickUp() As String
    2.         Set(ByVal [U]value[/U] As String)
    3.             Label1.Text = [U]value[/U]
    4.         End Set
    5.     End Property
    When you use a property you are implicitly calling either the Get or Set method from the property definition. Your properties are WriteOnly so they only have a setter. When someone assigns a value to your property like this:
    VB Code:
    1. myDateRange.PickUp = SomeValue
    They are implicitly calling this method:
    VB Code:
    1. Set(ByVal value As String)
    2.             Label1.Text = value
    3.         End Set
    and SomeValue is passed as the 'value' parameter.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    In my house
    Posts
    70

    Re: [2005] User Control Question

    Ok, I understand now, I think. I use the generic "Value" in the control, then when I use the control on the form I set the value for the label text by using:

    VB Code:
    1. myDateRange.PickUp = SomeValue

    Thank you very much!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Location
    In my house
    Posts
    70

    Re: [2005] User Control Question

    Just got it to work. Thanks again. I'm sure I'll have more control questions in the near future.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width