-
Jan 20th, 2021, 06:00 AM
#1
Thread Starter
New Member
[RESOLVED] System.NullReferenceException Error
Hello,
I am getting NullReferenceException error in the Controls("ComboBox" & m).Text = "No" and probably i didn't define ComboBox and i don't know how to do it. Can you please help?
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim m As Integer
For m = 35 To 77
Controls("ComboBox" & m).Text = "No"
Next m
End Sub
-
Jan 20th, 2021, 06:50 AM
#2
Re: System.NullReferenceException Error
Indexing the Controls collection with a name that doesn't exist will return Nothing, and you can't get a property of Nothing. Why do you think that ComboBoxes will just appear out of thin air? If you want ComboBoxes on your form then add them to your form. That said, if you added them to some other container, e.g. a Panel, then they will be in the Controls collection of that container, not of the form.
-
Jan 20th, 2021, 07:00 AM
#3
Thread Starter
New Member
Re: System.NullReferenceException Error
Originally Posted by jmcilhinney
Indexing the Controls collection with a name that doesn't exist will return Nothing, and you can't get a property of Nothing. Why do you think that ComboBoxes will just appear out of thin air? If you want ComboBoxes on your form then add them to your form. That said, if you added them to some other container, e.g. a Panel, then they will be in the Controls collection of that container, not of the form.
Hello jmcilhinney,
You are like The Dooo of programing I added the ComboBoxes in TabControl1, does this make a difference? If so how can i put it to my code?
-
Jan 20th, 2021, 08:44 AM
#4
Re: System.NullReferenceException Error
Code:
TabControl1.TabPages(x).Controls("ComboBox" & m).text
where x is the index of the tabpage
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Jan 20th, 2021, 08:53 AM
#5
Re: System.NullReferenceException Error
Originally Posted by Delaney
Code:
TabControl1.TabPages(x).Controls("ComboBox" & m).text
where x is the index of the tabpage
If the TabPage was added to the TabControl in the designer then there will be a field for it, just like other controls. In that case, you would generally refer to the TabPage by that field, e.g.
Code:
TabPage1.Controls("ComboBox" & m).Text = "No"
-
Jan 20th, 2021, 09:34 AM
#6
Thread Starter
New Member
Re: System.NullReferenceException Error
Thank you so much guys, solved the problem
-
Jan 21st, 2021, 12:29 AM
#7
Thread Starter
New Member
Re: System.NullReferenceException Error
I have one more question. When i try to use the same method with SelectedIndex = -1, it's saying "SelectedIndex is not a member of Control". How can i combine it with the Control method?
-
Jan 21st, 2021, 12:36 AM
#8
Re: System.NullReferenceException Error
Indexing the Controls collection returns a Control reference, so you can only access members of the Control class via that reference. As is always the case, if the actual object referred to is a more specific type and you want to access members of that type then you need to cast the reference you have as that type. The analogy I always use is a vet who knows that all their patients are animals but not what specific type of animal they will be, so can only do things that apply to all animals until the owner takes their pet out of the box and reveals the specific type of animal it is. If you don't know how to cast in VB then that's what you should research.
-
Jan 21st, 2021, 02:29 AM
#9
Thread Starter
New Member
Re: System.NullReferenceException Error
Solved it, thanks again
Code:
TabPage1.Controls("ComboBox" & m).Text = Nothing
Last edited by Textiler; Jan 21st, 2021 at 02:52 AM.
-
Jan 21st, 2021, 10:34 AM
#10
Re: System.NullReferenceException Error
The other option would be:
Code:
DirectCast(TabPage1.Controls("ComboBox" & m),ComboBox).SelectedIndex = -1
My usual boring signature: Nothing
-
Jan 21st, 2021, 02:21 PM
#11
Thread Starter
New Member
Re: System.NullReferenceException Error
Originally Posted by Shaggy Hiker
The other option would be:
Code:
DirectCast(TabPage1.Controls("ComboBox" & m),ComboBox).SelectedIndex = -1
Thank you, working great
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|