Solved

VB.Net Changing control properties from outside form

Posted on 2014-01-16
7
757 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
Comment Utility
It seems that _Test_FieldNames is Nothing, which is causing the exception.
0
 
LVL 69

Expert Comment

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

Expert Comment

by:Fernando Soto
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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 39

Expert Comment

by:Kyle Abrahams
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for all the feedback!  I have got it working now, and I'm grateful for your thoroughness.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

9 Experts available now in Live!

Get 1:1 Help Now