Solved

VB.Net Changing control properties from outside form

Posted on 2014-01-16
7
785 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 63

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 28

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 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