accessing from objects from a sub routine

BreakTheSilence

New member
Joined
May 28, 2006
Messages
3
Programming Experience
Beginner
accessing form objects from a sub routine

Hello im new to vb and i working on writing an sub routine that will access form objects based on a value that was passed to the sub.

For example:
Public sub check1_CheckedChanged
sub_routine(1)
end sub

Public sub_routine(location as string)
text + location.text = "something" < ----- this is my problem
end sub

Result: text1.text would now say "something"

I dont know how to call the object with its base name and a variable. I know its something simple and i am looking way to deep into it but i cant figure it out. Any help would be Great!!!!

Here is my source code:
VB.NET:
    Public Sub openaudiofile(ByVal location As String)
        Dim Temp_textbox
        Dim Temp_checkbox
        Dim Temp_array
        Temp_textbox = "txtaudio" & location
        Temp_checkbox = "chkaudio" & location
        Temp_array = ArrayOfAudio(location)

        If Temp_checkbox.Checked = True Then <---- ERROR starts here
            Temp_textbox.Enabled = True
            Temp_textbox.Text = ""

            If frmmain.ofd1.ShowDialog() = Windows.Forms.DialogResult.OK Then
                Temp_array = frmmain.ofd1.FileName
                Temp_textbox = frmmain.ofd1.FileName
            Else
                Temp_textbox.Enabled = False
                Temp_array = ""
                Temp_textbox.Text = ""
                Temp_checkbox.Checked = False
            End If
        Else
            Temp_textbox.Enabled = False
            Temp_array = ""
            Temp_textbox.Text = ""
        End If
    End Sub
My temp variables are not working; i get a error message saying "missingmemberexception was unhandled" "public member 'Checked' on type 'String' not found".

Any help would great...thanks!
- Jesse
 
Last edited:
What you're trying to do is a bad idea, should be avoided if at all possible and is usually completely unnecessary. Why do you need to pass a string to this method? I have seen numerous cases where people were trying to do this and there was a much better way available. There are very, very few situations where it is necessary to identify controls using a string instead of an identifier. You should give us some more background, like where this string is coming from in the first place. I'm 99% sure that there will be a better way.

You should also fix your local variable declarations. You haven't specified a type for any of those three variables. You should ALWAYS, ALWAYS, ALWAYS specify the type of EVERY variable WITHOUT exception. If it can be any type then you specify Object. In your case it's pretty obvious that they are supposed to be more specific types.

I'll explain exactly why that error is occurring. You have omitted the type of the Temp_checkbox variable so it defaults to Object. You are concatenationg two Strings and assigning the result to that variable, so it now refers to a String object. You then try to access a member of the CheckBox class, which you obvioulsy intended it to refer to, and the String object has no such member. If you'd declare the variable as type CheckBox like you should have then you'd have got an error when you tried to assign the String in the first place.
 
I had a feeling i was doing this the completely wrong way, let me explain my situation to provide you with some background information so you can see where im coming from. What i need to happen is when the user checkes the "enabled" checkbox it will open a open file dialog box that will allow the user to search for audio files. Once the user selects the file it will assign the file location to a textbox and a value in a string array. I have 5 of these checkbox and textbox objects on my form(maxium of 5 audio files can be selected). I didnt want to write the same code for each checkbox change event in case i had to change something so i decided to create a sub routine. When the user checks the enabled box it sends a string value of (1-5) depending on the box being checked. I was hoping my sub routing could then take the variable being passed and use it to perform the tasks on that selection.

for example:
1.user checks checkbox1 (checked state = true)
2.openaudiofile(1) is executed.
3.openaudiofile sub performs tasks
a. Set openfiledialog file location to textbox1
b. Set openfiledialog file location to array(0)

So instead of coding each checkbox change event with the code listed above i wanted to just create 1 sub that would do it for all the checkboxs based on the variable being passed.

jmcilhinney i would just like to say thanks for taking the time to look at my question, i realize i am quite the newbie. Hope this Helps!!
 
With five checkboxes named CheckBox1 to CheckBox5, the easiest way is to double click your first CheckBox and add the four other CheckBoxes after the "Handles" command, but you can also add handlers by code ( AddHandler CheckBox2.CheckedChanged, AddressOf CheckBox1_CheckedChanged, ...)

Here is an idea of the code :

VB.NET:
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, _
    CheckBox4.CheckedChanged, CheckBox5.CheckedChanged
        Dim Tb As CheckBox = DirectCast(sender, CheckBox)
        Select Case Tb.Name
            Case "CheckBox1"
                Console.WriteLine("You have clicked on CB1")
            Case "CheckBox2"
                Console.WriteLine("You have clicked on CB2")
            Case "CheckBox3"
                Console.WriteLine("You have clicked on CB3")
            Case "CheckBox4"
                Console.WriteLine("You have clicked on CB4")
            Case "CheckBox5"
                Console.WriteLine("You have clicked on CB5")
        End Select
    End Sub

Of course, you need to evaluate if the checkbox is enabled first.
 
You should never identify things with strings or numbers if you don't have to. Always use identifiers if possible:
VB.NET:
'This collection stores the check boxes and their corresponding text boxes.
Private textBoxesByCheckBox As New Dictionary(Of CheckBox, TextBox)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Create the relationship between the check boxes and text boxes.
    Me.textBoxesByCheckBox.Add(Me.CheckBox1, Me.TextBox1)
    Me.textBoxesByCheckBox.Add(Me.CheckBox2, Me.TextBox2)
    Me.textBoxesByCheckBox.Add(Me.CheckBox3, Me.TextBox3)
    Me.textBoxesByCheckBox.Add(Me.CheckBox4, Me.TextBox4)
    Me.textBoxesByCheckBox.Add(Me.CheckBox5, Me.TextBox5)
End Sub

Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged, _
                                                                                                        CheckBox4.CheckedChanged, _
                                                                                                        CheckBox3.CheckedChanged, _
                                                                                                        CheckBox2.CheckedChanged, _
                                                                                                        CheckBox1.CheckedChanged
    Dim cbx As CheckBox = DirectCast(sender, CheckBox)

    If cbx.Checked Then
        'Display an OpenFileDialog.
        Using ofd As New OpenFileDialog
            ofd.Filter = "Wave Audio Files (*.wav)|*.wav"

            If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
                'Set the Text of the text box that corresponds to the check
                'box that raised the event to the path of the selected file.
                Me.textBoxesByCheckBox(cbx).Text = ofd.FileName
            End If
        End Using
    Else
        'Clear the text box that corresponds to the check box that raised the event.
        Me.textBoxesByCheckBox(cbx).Clear()
    End If
End Sub
 

Latest posts

Back
Top