Solved

VB.Net Changing control properties from outside form

Posted on 2014-01-16
7
769 Views
Last Modified: 2014-01-21
I have a checkbox, the properties of which I would like to change from outside the form in which it resides.  When I include the code to do this, however, the form won't open.  Here's the basic code:

Dim UseCheck As System.Windows.Forms.CheckBox = MyForm.chkProcess
With UseCheck
    .Checked = False
    .Enabled = True
End With

Open in new window


I have isolated this code block as the place where my form won't open.  When I try, the Show() method breaks, and I get this error:

Exception error, even before form is opened.
I was thinking that sharing the control might get around this problem, but when I try the code shown below, I get the error: "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."  The whole thing is befuddling.  Please help.  ~Peter Ferber

Public Shared ListTables As System.Windows.Forms.CheckBox = chkProcess

Open in new window

0
Comment
Question by:PeterFrb
7 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 39788653
It seems that _Test_FieldNames is Nothing, which is causing the exception.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39788705
have you changed the Modifiers property of the checkbox to Public?
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39788723
Hi PeterFrb;

As @wdosanjos stated it seems as if you are attempting to assign a value to a variable that does not exist yet. In order to help you to overcome this issue can you please post both classes involved. The form of the control you are trying to change and the class that you are trying to do it from.

Also when changing a field from outside the class you should do this through a property and not directly.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39788738
This sounds like you are attempting to modify the properties of the controls BEFORE the form has been created.

The following fails
        Dim f As Form1
        f.Show()

This is creating the form before displaying it
        Dim f As Form1
        f = New Form1
        f.Show()

You must create the form and the controls it contains before running your code to modify things.  Eg. you can run this code in the Form_Load event.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39788769
Another note . . . it's better to keep a reference to the instance of the form itself, and access the checkbox through that.
0
 
LVL 27

Accepted Solution

by:
Ark earned 500 total points
ID: 39791701
Hi
I suggest using a singleton class to share data between forms:
Public Class Class1
    Implements System.ComponentModel.INotifyPropertyChanged
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) _
        Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Private _isCheck1Checked, _isChek1Enabled As Boolean

    Private Shared _inst As Class1

    Public Shared ReadOnly Property Instance As Class1
        Get
            If _inst Is Nothing Then _inst = New Class1
            Return _inst
        End Get
    End Property

    Public Property IsCheck1Checked As Boolean
        Get
            Return _isCheck1Checked
        End Get
        Set(ByVal value As Boolean)
            _isCheck1Checked = value
            RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("Check1Checked"))
        End Set
    End Property

    Public Property IsCheck1Enabled As Boolean
        Get
            Return _isChek1Enabled
        End Get
        Set(ByVal value As Boolean)
            _isChek1Enabled = value
            RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("Check1Enabled"))
        End Set
    End Property
'More properties if you need here
End Class

Open in new window

Now, in form with CheckBox (Form1):
Public Class Form1
    Private c As Class1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        c = Class1.Instance
        CheckBox1.Checked = c.IsCheck1Checked
        CheckBox1.Enabled = c.IsCheck1Enabled

        AddHandler c.PropertyChanged, AddressOf Class1_PropertyChanged
    End Sub

    Private Sub Class1_PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs)
        Select Case e.PropertyName
            Case "Check1Checked"
                CheckBox1.Checked = c.IsCheck1Checked
            Case "Check1Enabled"
                CheckBox1.Enabled = c.IsCheck1Enabled
        End Select
    End Sub
End Class

Open in new window

Whenever you need change properties:
      Class1.Instance.IsCheck1Checked = True
        Class1.Instance.IsCheck1Enabled = False

Open in new window

0
 

Author Closing Comment

by:PeterFrb
ID: 39797798
Thanks for all the feedback!  I have got it working now, and I'm grateful for your thoroughness.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 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