1

I am working on vb.net window application where i have to find Control in window form by string name. For that i used the following code:-

Dim lbl As New Label
Dim st As String

st = "lblFeeder" + i.ToString()
lbl = CType(Me.Controls(st), Label)
lbl.Text = dt.Rows(37)("AnaTagValue")

But when the program runs it gives a error

"Object reference not set to an instance of an object."

at the postion

"lbl.Text = dt.Rows(37)("AnaTagValue")"

When i debug it i Found "Nothing" at the position "lbl = CType(Me.Controls(st), Label)"

How can I solve this?

Sunny Sandeep
  • 971
  • 7
  • 18
  • 53
  • You don't check if you have found the control or not. Remember, controls can be contained in controls container other than the form itself (GroupBoxes, panels) – Steve Feb 08 '18 at 10:55
  • I edited my question – Sunny Sandeep Feb 08 '18 at 10:59
  • If you get a Nothing then there is no control with that name in the Form (Me) controls collection. If you are sure that a control with that name exists then it is not inside the form controls collection but probably inside a GroupBox or Panel or some other controls container. You should find the correct container and use its collection for reference the label – Steve Feb 08 '18 at 11:02
  • 1
    This control is present inside a form panel which is also inside another panel. – Sunny Sandeep Feb 08 '18 at 11:12
  • 1
    then _Me.Panel1Name.Panel2Name.Controls_ for the first parameter of CType – Steve Feb 08 '18 at 11:13
  • @Steve it really works fine for me. Thanks a lot. – Sunny Sandeep Feb 08 '18 at 11:19

0 Answers0