Solved

Accessing form controls (without 'sharing')

Posted on 2007-11-27
5
146 Views
Last Modified: 2013-11-07
Let's say I have frmMain and a label on it that displays the date.
I want to be able to click the date, and have a second form frmDate display, which only contains the calendar control, and update the label on frmMain with the date selected.

Currently, I'm running into "Reference to a non-shared member requires an object reference" while trying to simply use frmMain.Label1.Text=Cal.SelectionStart
I sort of understand why that is, and have read that using the shared keyword may alleviate the problem, however, one twist that I have is that frmMain may not be frmMain 'all the time'.

Ideally, I'd love to be able to pass the Date clicked back to the form's parent (I'm displaying with showdialog), but that property appears to be empty anyway.

Any way to easily accomplish what I'm after here?  Please ask if anything needs clarifying.  Thanx
0
Comment
Question by:sirbounty
  • 3
  • 2
5 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 250 total points
ID: 20359399
I would declare an event in frmDate:

Public Event DateSelected(ByVal SelDate As Date)

Raise it where you need it:

RaiseEvent DateSelected(Today)  'or whatever date

In frmMain, declare a handler and use it:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim frm As New frmDate
        AddHandler frm.DateSelected, AddressOf frm_DateSelected
        frm.ShowDialog()


    End Sub

    Private Sub frm_DateSelected(ByVal SelDate As Date)
        Label1.Text = SelDate.ToString("d")
    End Sub
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20359452
Precisely what I needed.
Thanx! :^)
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 20359503
If you can close the date form when you have selected a date, you can also create a property to read the date from:

Public Class frmDate
    Private mSelDate As Date
   

    Public Function SelDate() As Date
        Return mSelDate
       
    End Function

    Private Sub MonthCalendar1_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateSelected
        mSelDate = e.End
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()

    End Sub
End Class

Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim frm As New frmDate

        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Label1.Text = frm.SelDate.ToString("d")
        End If


    End Sub

   
End Class
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20360442
Hmm - yes, I will be closing it once the date is selected.
I'll try this out.  Thanx.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 20361050
Works great!  Thanx again!! :^)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now