r/visualbasic • • Apr 04 '19

Variables issue

Hi ppl, i'm not that pro person in VB.

what i'm trying to do is to assign a variable for an item (e.g. Label or Textbox)

a variable looks like their name and the other properties so when i'm in any sub in the code, i can call that variable like: Label1.VarName

i know this is a wrong way but is there any way to such a thing ?

Thanks.

My code:

Dim n As Integer

Private Sub TextBox1(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown

If e.KeyCode = Keys.Enter Then Create_new_list_item()

End Sub

Sub Create_new_list_item()

ReDim _labels(n)

_labels(n) = New Label()

With _labels(n)

Dim MyN As Integer = n

.Name = "Label_" + n.ToString

.Text = MyN.ToString

.Width = 100

.Visible = True

.Location = New Point(0, 70 + (n * 24))

AddHandler .DoubleClick, AddressOf Editvalue

End With

Me.Panel1.Controls.Add(_labels(n))

n += 1

End Sub

Sub Editvalue(ByVal sender As Object, ByVal e As EventArgs)

sender.Visible = False

MsgBox(sender.MyN.ToString)

End Sub

6 Upvotes

7 comments sorted by

1

u/andrewsmd87 Web Specialist Apr 04 '19

Can you go over your use case with me? Like don't worry about the technical aspects, what is the problem you're trying to solve?

2

u/00jamiil Apr 04 '19

I did a runtime creation for Label and Textbox when ever i pressed enter and both of them are laying above each other, anyway i want whenever i double clicked the label to hide it and show the textbox then when i press enter in the textbox the label will show again and the textbox will be hidden.

remember that these items created while the program is running and everyone of them is assigned to a value in an array.

i hope u understand me XD

2

u/At_least_im_Bacon Apr 04 '19

Why not just enable or disable the textbox in double click instead of replacing it with a label?

2

u/00jamiil Apr 04 '19

Nice idea!

but i think when the textbox is disabled i can't double click it

1

u/00jamiil Apr 04 '19

Nvm, i used .ReadOnly instead of .Enabled

thanks guys

3

u/At_least_im_Bacon Apr 04 '19

You might be right. Try the read-only property.

3

u/andrewsmd87 Web Specialist Apr 04 '19

Not really. So are you saying you have a form where they click a label and it adds a text box for them, and they can do this as many times as they want?