Set Controls dinamically? is this for experts?

  • Thread starter Thread starter Marcos Beccar Varela
  • Start date Start date
M

Marcos Beccar Varela

Hello! I´m a newbbie in VB.NET and I´m having a problem. I have many forms
and I need to create a label (for example) in each of them.
Is there a way to make a function which I can call it from any form and that
this function creates a label and sets a text and position in the form,
where the text and position are parameters of the function.

createlabel("Text on the label", 23, 450)

This function can be in a module?? Can anyone send me a litttle code for
reference on how this can be made?

Thank you VERY much!!
Marcos
 
Marcos,

You can, the only problem that you have to keep an eye on is that when an
object goes out of scope (is not needed any more) that object will be
cleanded up by the Garbage Collector (not direct however when your program
reaches idle time).

All in that object created subobjects (which have no reference anymore) will
be removed as well.

When you do what you ask, than you can use two methods the shared or non
shared because what I wrote above I would in this case avoid "shared".

When you use such a class non shared you get something in a simple way
as(you can as well create your own labelclass by inheriting)
\\\
Dim myLabelBuilder as new LabelBuilder
me.controls.Add(myLabelBuilder.AddLabel(("Text on the label", 23, 450))
///

Where the class can than be
\\\
Public Class LabelBuilder
Public function AddLabel(byval Text as String, byval X as Integer, Byval
Y as integer) as Label.
dim mylabel as new label
mylabel.Text = Text
myLabel..Location = New System.Drawing.Point(X, Y)
return mylabel
End class
////
However what you replace is
\\\
dim mylabel as new label
mylabel.Text = "Text on the label",
myLabel..Location = New System.Drawing.Point(23, 450)
me.controls.add(mylabel)
///
Not that much in my opinion

(all typed in this message so wach typos)

I hope however that this clears somethings

Cor
 
Thank you Cor, I get the idea, and that was exactly what I needed.
You said that the code doesn´t replace too much, but yes, because I just
needed an exaple, there maney labels anda other objects in that function.

//In a module
Public Class mkc_designcre
Public Function mkf_addlabel(ByVal Text As String, ByVal X As Integer,
ByVal Y As Integer) As Label
Dim nlabel As New Label
nlabel.Text = Text
nlabel.Location = New System.Drawing.Point(X, Y)
Return nlabel
End Function
End Class
I tried this, and I´t doesn´t return any errors, but nothing is visible.


//In the form
Dim mkd_labelb As New mkc_designcre
Me.Controls.Add(mkd_labelb.mkf_addlabel("Text on the label", 25, 23))


Thank you again, it was of big help, I´m working to find the final solution
to get it visible
Marcos
 
Marcos,

On a new form where on was nothing I did this

\\\
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim mkd_labelb As New mkc_designcre
Me.Controls.Add(mkd_labelb.mkf_addlabel("Text on the label", 25,
23))
End Sub
End Class

Public Class mkc_designcre
Public Function mkf_addlabel(ByVal Text As String, _
ByVal X As Integer, ByVal Y As Integer) As Label
Dim nlabel As New Label
nlabel.Text = Text
nlabel.Location = New System.Drawing.Point(X, Y)
Return nlabel
End Function
End Class
///
And than I saw that text in the uper left side "Text on the label"

I hope this helps?

Cor
 
Thank you Cor, my mistake, an image was not letting me see the text because
of the forecolor!
Thank again!
Marcos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top