Solved

VB.Net Changing control properties from outside form

Posted on 2014-01-16
7
792 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

752 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