Calla

Sorry folks, newbie question. Go easy on me.

I have a variable x with a numeric value (1,2..)
I also have several textboxes (textbox1,textbox2..)
can I concatenate 'textbox' and the value of x when referencing a textbox
e.g. textbox(x).text to make 'textbox1.text'
it sounds daft but it's driving me mad.
Any help most appreciated.
Thanks all.

Calla.


Re: Visual Basic Language combining a class name with the value of a variable

Alex Moura

You can, through the controls item property:

For example:

for i = 1 to 2

ctype(controls.item("TextBox" & i), TextBox).Text = "Hello" & i

next i






Re: Visual Basic Language combining a class name with the value of a variable

Spidermans_DarkSide - MSP, VSIP

Calla wrote:
Sorry folks, newbie question. Go easy on me.

I have a variable x with a numeric value (1,2..)
I also have several textboxes (textbox1,textbox2..)
can I concatenate 'textbox' and the value of x when referencing a textbox
e.g. textbox(x).text to make 'textbox1.text'
it sounds daft but it's driving me mad.
Any help most appreciated.
Thanks all.

Calla.

Hi Calla,

I am trying to think of a use for that idea that you have.

Another way to reference controls such as textboxes is to change the HANDLES statement at the end of a SUBroutine header like this.>>

Private Sub TextBoxes_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click, TextBox2.Click

You can then tell which textbox was clicked on or which one had a textChanged event happen to it or whatever.>>>>

Code Snippet

Public Class Form1

'This highlighted line should be one line of code in your code window.>>>>

Private Sub TextBoxes_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click, TextBox2.Click

Select Case sender.name.ToString

Case "TextBox1"

MessageBox.Show("Textbox1 was clicked on!!")

Case "TextBox2"

MessageBox.Show("Textbox2 was clicked on!!")

End Select

End Sub

'This highlighted line should be one line of code in your code window.>>>>

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged

Select Case sender.name.ToString

Case "TextBox1"

MessageBox.Show("The text of Textbox1 text was changed!!")

Case "TextBox2"

MessageBox.Show("The text of Textbox2 text was changed!!")

End Select

End Sub

End Class

You might find this idea quite useful.

Regards,

S_DS






Re: Visual Basic Language combining a class name with the value of a variable

Spidermans_DarkSide - MSP, VSIP

Alex Moura wrote:

You can, through the controls item property:

For example:

for i = 1 to 2

ctype(controls.item("TextBox" & i), TextBox).Text = "Hello" & i

next i

Hi Alex,

I thought OPTION STRICT ON would have disallowed the bits I have highlighted below being missing from your code snippet.

Why does it disallow late-binding please

Code Snippet

For i As Integer = 1 To 2

CType(Controls.Item("TextBox" & i.ToString), TextBox).Text = "Hello" & i.ToString

Next i

By the way I really like line two of your code, very neat and clever.

Regards,

S_DS






Re: Visual Basic Language combining a class name with the value of a variable

Alex Moura

I believe you're correct - as an aside, in Orcas (vs 2008) the for part is actually legal even with option strict on (we're doing smarter type inference) - My code was meant for option strict off, and I didn't have to use ctype, but by doing so I get some extra intellisense and better performance, even with option strict off Smile






Re: Visual Basic Language combining a class name with the value of a variable

Calla



Thanks very much fellas. I'm off to bed now but I'll have a go at this tomorrow and see how I go.
Ta.





Re: Visual Basic Language combining a class name with the value of a variable

Calla



Incidentally, it's for a banjo chord finder thingy I'm having a go at writing. You might have seen those programs for guitar which show a fretboard and and show notes on the fretboard which form, say, a scale you have chosen.
I'm just puzzling over the best (easiest) way of displaying certain labels, placed over the top of a picture of a banjo neck, depending upon things like which key has been chosen and which scale/chord/tuning has been chosen.

You may be hearing from me again very soon when I get stuck.

Calla.




Re: Visual Basic Language combining a class name with the value of a variable

Spidermans_DarkSide - MSP, VSIP

Calla wrote:


Incidentally, it's for a banjo chord finder thingy I'm having a go at writing. You might have seen those programs for guitar which show a fretboard and and show notes on the fretboard which form, say, a scale you have chosen.
I'm just puzzling over the best (easiest) way of displaying certain labels, placed over the top of a picture of a banjo neck, depending upon things like which key has been chosen and which scale/chord/tuning has been chosen.

You may be hearing from me again very soon when I get stuck.

Calla.

Hi Calla,

Add to this thread then if you like later as it isn't too long yet.

I would draw a picture for each chord with the test already on it in PAINT or similiar program.

Go to the WINDOWS Start button and you will find PAINT in the Accessories menu.

Then using a comboBox, if Chord "A" is selected then show the appropriate picture in a pictureBox.

I could paste a bit of code here which could do this quite easily.

I will just leave you to draw the pictures as you might not like my drawings.

Anyway i do not know the banjo chord fingering diagrams or those for any stringed instrument for that matter.

I wish I had carried on with an instrument when I was younger. Oh well.

Regards,

S_DS