[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

Object reference not set to an instance of an object.

Im trying to use this code:

Me.Controls("Button2").Enabled = False

but VB allways shows the following error: Object reference not set to an instance of an object.

the control Button2 exists in my form

somebody knows what is wrong?????
0
altariamx2003
Asked:
altariamx2003
  • 3
  • 2
  • 2
  • +2
1 Solution
 
Paul JacksonCommented:
Is the button contained within another control perhaps? if so you need to reference that controls controls collection i.e.

myPanel.Controls("Button2").Enabled = false
0
 
miketonnyCommented:
it's because it can't locate your control "Button2", can you double check your button name?
0
 
miketonnyCommented:
in your button's property window, check what's the 'Name' field contains, that's the name you should use,
alternatively, you can use your control's index, in this case it doesn't matter what's the name of your control.

        Me.Controls(0).Enabled = False

Assume 0 here is your button2 control, index starts at 0 which is the first control you put on.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can SEARCH for the control matching that name like this:

    Dim ctls() As Control = Me.Controls.Find("Button2", True)
    If ctls.Length > 0 Then
        ctls(0).Enabled = False

        ' If you need to cast it then:
        If TypeOf ctls(0) Is Button Then
            Dim btn As Button = CType(ctls(0), Button)
            btn.Text = "You found Me!"
        End If
    End If
0
 
altariamx2003Author Commented:
the button is inside 2 panels

can I use something like this????

me.controls("panel1").controls("panel2").controls("Button2").enabled = false
0
 
altariamx2003Author Commented:
I mean

Panel1
   |_ Panel2
           |_ Button2

this is the order and where I can find the button
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can do that...but why would you?
0
 
Moizsaif123Commented:
Check this link and like mentioned above, first find the control be it panel or button then find its ID in html form by control.ClientID

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_27018478.html
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You might want to mention whether your project is WinForms or WebForms next time...it makes a difference!
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.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now