English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I just want to say that I am very new to VB so please forgive my ignorance.

I need to create a String / Array such that Button(1) is equal to Button1 and Button(2) is equal to Button2 and Button(73) would thus be equal to Button73.

If I say:

Dim Button() as String = {Button1, Button2, Button3}

it says: Value of type 'System.Windows.Forms.Button' cannot be converted to 'String'.

Please help? How can I convert all 100 of my buttons into a string or array so that I can refer to them as Button(54) for instance or Button(75)?

Please remember that I am just starting Visual Basic, so the more detail you give me and the more simple and dummy-proof your answer is, the much much higher possibility there will be of me choosing you as my Best Answer.

Thank you so much for your help!

2007-06-30 16:13:57 · 2 answers · asked by djdeity25 1 in Computers & Internet Programming & Design

2 answers

What are you going to do with the buttons? Because you can "refer" to them using there name.
txt1.Text = Button1.Name

During runtime you can create a new button and add a ref. point to all the buttons you create...

Private Sub btnCNB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCNB.Click
Try
'Make a counter
'so the user cant go over three(3)
m_ControlCount += 1
'If the user has not created more than three(3) buttons
If m_ControlCount < 5 Then
'X as a new button control
Dim x As New Button
'Set values
x.Name = "btn" & m_ControlCount.ToString
x.Text = "Button " & m_ControlCount.ToString
'Set location
x.Location = New Point(Me.m_Location.X + 150, Me.m_Location.Y)
m_Location.Y += x.Height + 5
'Make a click event,

AddHandler x.Click, AddressOf myButtonHandler_Click
'Add button as a control
Me.Controls.Add(x)
End If
'#############################...
'To add to a TAB button ect ect
'myTab.controls.add(x)
Catch
End Try
End Sub

Private Sub myButtonHandler_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
'If it is a button then...
If TypeOf sender Is Button Then
'The the text of the object pressed
'as a button
MsgBox(CType(sender, Button).Text + " was pressed!", _
MsgBoxStyle.OKOnly, Me.Text)
End If
Catch
End Try
End Sub

++++++++++++++++++++++++++=
But what you are trying to is is create a button string array...does not sound right. TAKE THIS...
Dim str as String
dim int as Integer = str
It is like trying to tell the computer a letter is a number. Does not work... (it may work unless you declare "option strict on")
A type "button" is not the same as a "string"
A "button" is a windows.form.object, and a "string" is a variable
That is how you want to declare a button array
Dim a() As Button = {Me.button1, Me.button2}

Any questions e-mail me

2007-07-03 15:54:53 · answer #1 · answered by Anonymous · 0 0

Instead of looping through a collection of buttons, try this:

Create a listbox and load the listbox with the names of your 100 functions.

In the onclick event, execute the appropriate function based on the current selected item in the listbox via select case.

2007-06-30 16:49:51 · answer #2 · answered by Lorenzo H 3 · 1 0

fedest.com, questions and answers