Hi

I have a set of Checkboxes each with a Textbox next it to. I have the following code, which is meant to loop through each CheckBoxe and if checked if gets the text contained in the textbox, these are linked through the tag of the checkbox, i have used it before and it worked fine but this time i am getting the following error:

Null Reference Exception was unhandled
Object reference not set to an instance of an object.

I have checked by simply getting get the tag and the value of the linked textbox and both are populating fine, but i don't want to have to manually write each link in IF Statements. The below code should take the Tag of the checked checkbox E.G txt1 if chk1 is checked and convert it into a TextBox so in the end it would be txtTest.Text = txt1.Text

Dim DisCheckBox As CheckBox() = {chk1, chk2, chk3, chk4, chk5}

For Each Chk In DisCheckBox

    If Chk.CheckState = CheckState.Checked Then

        txtTest.Text = CType(pnlChk.Controls(Chk.Tag.ToString()), TextBox).Text()

    End If

Next

Like i said i have used this before but don't understand why i'm getting this error, any help would be very grateful.

Recommended Answers

All 7 Replies

Could be that the CheckBox.Tag is empty.

If Not Chk.Tag Is Nothing Then txtTest.Text = CType(pnlChk.Controls(Chk.Tag.ToString), TextBox).Text

Or you could use this and skip using the .Tag of a CheckBox entirely.

'// use "txt" and the # at the end of each CheckBox's Name to locate control.
                txtTest.Text = CType(pnlChk.Controls("txt" & Chk.Name.Substring(Chk.Name.IndexOf("k") + 1)), TextBox).Text

If only needing the last TextBox's .Text, why not only set it once?

Dim DisCheckBox As CheckBox() = {chk1, chk2, chk3}
        Dim cb As New CheckBox '// sets the checked CheckBox.
        For Each Chk In DisCheckBox
            If Chk.CheckState = CheckState.Checked Then cb = Chk '// locate the CheckBox if checked.
        Next
        If Not cb.Name = "" Then '// check if cb has a .Name.  gets added if checked.
            '// use "txt" and the # at the end of each CheckBox's Name to locate control.
            txtTest.Text = CType(pnlChk.Controls("txt" & cb.Name.Substring(cb.Name.IndexOf("k") + 1)), TextBox).Text
        End If

Hi

Thanks for your reply and some of it i will use, but i am still getting:

Null Reference Exception was unhandled
Object reference not set to an instance of an object.

I used your code to check the Tag was not nothing and as i thought it passed that test and then errored on

txtTest.Text = CType(pnlChk.Controls(Chk.Tag.ToString()), TextBox).Text()

i also tried without the Tag and hard coding "txt" instead of the .Tag with no change.

I have now created a new Project to only include what i am trying to do, below is the code and the form has 5 Checkboxes / 5 Textboxes / Button and a Test Textbox. Also entered the Textbox name in the Tag of each Checkbox. I entered a value into the textbox and checked the related textbox and clicked the button

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ChangeCheckBox As CheckBox() = {chk1, chk2, chk3, chk4, chk5}

        For Each Chk In ChangeCheckBox

            If Chk.CheckState = CheckState.Checked Then

                If Not Chk.Tag Is Nothing Then

                    txtTest.Text = CType(pnlChk.Controls(Chk.Tag.ToString()), TextBox).Text()

                End If

            End If

        Next

    End Sub
End Class

If you can workout what i am doing wrong i would be very grateful.

Thanks again

I just created a new project with your provided code, added "txt1" to chk1.Tag and a few other CheckBoxes, and even left out some .Tags empty. It did not crash here, ran as it should.
Only time i got it to error with: Object reference not set to an instance of an object. is when I changed the .Tag for chk4 from "txt4" to "test4". Seems that one of your .Tags, if not all, do not have the proper Name(s) of the TextBox(es).

You could add a MsgBox in the For/Next loop to locate which CheckBox cause this issue.

If Not Chk.Tag Is Nothing Then MsgBox(Chk.Tag)

I can't beleive it, i've worked out why it wasn't working. Basically i didn't think it would make a difference but i failed to add that my Chk boxes are in one Panel and my Textboxs are in a different Panel, so obviously it cannot "See" the Textboxes.

So my new question is,

Is there away round this or should i just remove the Panels?

The reason i use the panels is so i can change properties of certain textboxes by looping through the controls of a panel instead of creating an array of controls or typing each control individually.

Thanks for your help codeorder and i most likely will use some of the code you listed in your first post and apologies for not describing the full layout, but i wasn't thinking that it would of made a difference but now it makes sense

There should be no issues if having the CheckBoxes in one Panel and TextBoxes in another. Your code locates the Panel and looks for a certain control within it. txtTest.Text = CType(pnlChk.Controls(Chk.Tag.ToString), TextBox).Text You could add all of your controls to the Form, not Panels, and loop through all of them to locate only your "txt#" TextBoxes.

For Each ctl As Control In Me.Controls
            If TypeOf ctl Is TextBox AndAlso IsNumeric(ctl.Name.Substring(ctl.Name.LastIndexOf("t") + 1)) Then
                ctl.BackColor = Color.LightSteelBlue
            End If
        Next

That will locate all TextBoxes that have a # at the end of the .Name and the letter prior to the number is "t".

You could however have the TextBoxes.Names end with "_x" for example, then only check If TypeOf ctl Is TextBox AndAlso ctl.Name.EndsWith("_x") Then .

thanks again Codeorder

And i'm even more of an idiot than my first reply, the error was because i didn't fully understand what CType(pnlChk.Controls(Chk.Tag.ToString()), TextBox).Text() was doing, and i was putting the wrong panel in, hence why i thought it couldn't "See" the other panel, anyways all sorted.

Thanks again

pls solve this
Private Sub create_mouse()
    'Dim mouse As New PictureBox
    Dim mouse As New PictureBox
    With mouse
        .Width = 10
        .Height = 10
        .BackColor = Color.Red
        .Top = r.Next(PictureBox1.Top, PictureBox1.Bottom - 10) //error null reference exception was handeled//
        .Left = r.Next(PictureBox1.Left, PictureBox1.Right - 10)
    End With
    Me.Controls.Add(mouse)
    mouse.BringToFront()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.