Solved

VB.Net Changing control properties from outside form

Posted on 2014-01-16
7
774 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 70

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 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