Search code examples
vb.netvariablescheckboxtextchanged

VB.net - Target object using string (Object is under tab then panel then splitter)


In php and javascript I can do something like this

If ($a == 1){
  $set_num = 1
} Else {
  $set_num = 2
}

textbox_ . $set_num = "Some text here"
checkbox_ . $set_num = "Some text here"
radio_ . $set_num = "Some text here"

How can I do this in vb.net?

enter image description here

In my design page I have many panel, splitter, textbox, checkbox and other things and please take note that this design is under the panel tab (index 2). What I want is to create a short code (Short as possible) so when the user types in a certain textbox I will know what schedule to activate so every time the user clicks the apply button the system will just send the set that has a last text_change.

This is my code in vb.net and I wanted to use me.controls to call an object in a string, but it's not working

     Dim lab As Label

     For i As Integer = 1 To 10
        lab = Me.Controls("tb_hour_" & i)
        lab.Text = "Test" & i
     Next

By the way, the name of my textbox, checkbox and radio button is something like this

tb_hour_1, tb_minute_1, cb_monday_1, etc. _1 stands for schedule 1 so for the schedule 2 it will be like this tb_hour_2, tb_minute_2, cb_monday_2, etc.

Actually my code is working, the problem comes in when I put the textboxes, checkboxes, etc inside the panel and splitter. The purpose of panel and splitter is just to have a design (my aim is the border).

Currently this is the code that I used to achieved my goal, but my code is so hard to maintain because I nested a condition. That's why I want to apply while loop and target all of the objects in a string

If Lbl_Temp_1.Text = "0" Then
        frmTerminal.Lbl_Settings_Cmd.Text = "CSR01^"
        frmTerminal.cmdSend_Click(sender, e)
    Else
        If Lbl_Temp_2.Text = "0" Then
            frmTerminal.Lbl_Settings_Cmd.Text = "CSR02^"
            frmTerminal.cmdSend_Click(sender, e)
        Else
            If Lbl_Temp_3.Text = "0" Then
                frmTerminal.Lbl_Settings_Cmd.Text = "CSR03^"
                frmTerminal.cmdSend_Click(sender, e)
            Else
                If Lbl_Temp_4.Text = "0" Then
                    frmTerminal.Lbl_Settings_Cmd.Text = "CSR04^"
                    frmTerminal.cmdSend_Click(sender, e)
                Else
                    If Lbl_Temp_5.Text = "0" Then
                        frmTerminal.Lbl_Settings_Cmd.Text = "CSR05^"
                        frmTerminal.cmdSend_Click(sender, e)
                    Else
                        If Lbl_Temp_6.Text = "0" Then
                            frmTerminal.Lbl_Settings_Cmd.Text = "CSR06^"
                            frmTerminal.cmdSend_Click(sender, e)
                        Else
                            If Lbl_Temp_7.Text = "0" Then
                                frmTerminal.Lbl_Settings_Cmd.Text = "CSR07^"
                                frmTerminal.cmdSend_Click(sender, e)
                            Else
                                If Lbl_Temp_8.Text = "0" Then
                                    frmTerminal.Lbl_Settings_Cmd.Text = "CSR08^"
                                    frmTerminal.cmdSend_Click(sender, e)
                                Else
                                    If Lbl_Temp_9.Text = "0" Then
                                        frmTerminal.Lbl_Settings_Cmd.Text = "CSR09^"
                                        frmTerminal.cmdSend_Click(sender, e)
                                    Else
                                        If Lbl_Temp_10.Text = "0" Then
                                            frmTerminal.Lbl_Settings_Cmd.Text = "CSR10^"
                                            frmTerminal.cmdSend_Click(sender, e)
                                        Else
                                            Tmr_Schedule_Get.Enabled = False
                                            frmTerminal.Tmr_Write_Check.Enabled = True
                                            Btn_Schedule.Enabled = False
                                            Me.Settings_Tab.TabPages(1).Enabled = True

                                            If Cb_Set_1.Checked = True Or Cb_Set_2.Checked = True Or Cb_Set_3.Checked = True Or Cb_Set_4.Checked = True Or Cb_Set_5.Checked = True Or Cb_Set_6.Checked = True Or Cb_Set_7.Checked = True Or Cb_Set_8.Checked = True Or Cb_Set_9.Checked = True Or Cb_Set_10.Checked = True Then
                                                Btn_Schedule.Enabled = True
                                            End If

                                        End If
                                    End If
                                End If
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End If

Solution

  • It seems that right now it isn't possible to target an object that is under the panel, and that panel is also under the panel.

    I managed to fix this issue by re-creating my design layout, as much as possible I limit the use of panels to one object is under 1 panel only, by doing it like that I can easily target the object using this code.

    For Each Ctrl_Panel_Set As Control In Schedule_Tab
            If TypeOf Ctrl_Panel_Set Is Panel And Ctrl_Panel_Set.Name.StartsWith("Panel_Set_") Then
                For Each Ctrl_Lbl_Temp In Ctrl_Panel_Set.Controls
                    If TypeOf Ctrl_Lbl_Temp Is Label Then
                        If Ctrl_Lbl_Temp.Name.StartsWith("Lbl_Temp_") Then
                            If Ctrl_Lbl_Temp.Text = "0" Then
                                Panel_Set_Num = CType(Ctrl_Panel_Set, Panel).Name
                                Set_Num = "CSR" & Panel_Set_Num.Substring(Panel_Set_Num.Length - 2) & "^"
                                Return Set_Num
                            End If
                        End If
                    End If
                Next
            End If
        Next Ctrl_Panel_Set