473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Set Controls dinamically? is this for experts?

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


Nov 21 '05 #1
4 1031
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


Nov 21 '05 #2
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
"Cor Ligthert" <no************@planet.nl> escribió en el mensaje
news:OW**************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #3
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
Nov 21 '05 #4
Thank you Cor, my mistake, an image was not letting me see the text because
of the forecolor!
Thank again!
Marcos
"Cor Ligthert" <no************@planet.nl> escribió en el mensaje
news:%2***************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Daniel | last post by:
Hello!!! I'm creating controls dinamically in ASP.NET using VB. The problem is that i don't know how to catch the event of each button that i've create (the buttons are created dinamically). ...
2
by: Marco | last post by:
How I can create TextBox dinamically. I have to create 10 textbox dinamically which name will be txtBox_01, txtBox_02, etc... Thanks.
1
by: Alfonso Paredes | last post by:
I need to add dinamically custom controls to a panel ( or something like it) but sometimes the number of controls is excesive (10 or more) so I'd like to add this controls to something that gives...
0
by: Rafi | last post by:
Hi, In my database I have table category where I store diffrent categories and subcategories. I decied to create LinkButtons dinamically with assigned name of category and its id. I also...
3
by: vladvadeanu | last post by:
I have this problem... I want to add controls to a WebForm (any kind of controls... panels, buttons, datagrids,...) at runtime and I could do that with HtmlControls :" Label myLabel = new Label();...
3
by: Leo J. Hart IV | last post by:
OK, here's another question for the experts: I am building a multi-step (3 steps actually) form using a panel for each step and hiding/displaying the appropriate panel/panels depending on which...
2
by: Fabio Cavassini | last post by:
I have this code that load HTML tags (no including <html> or <body>) into a DIV dinamically... after correctly retrieving the HTML, I assign it to my DIV Container ...
12
by: nat | last post by:
Hi all, I was going to post this in the AJAX forum, but apparently someone posted an AJAX/C# question in there today and was told to try this forum instead. So here goes: I'm trying to figure...
32
by: Mateo | last post by:
I have char *array and it is dinamically alocated.... When I pass it to other function, I need to determine size of this array.... Problem: sizeof operator does not work with dinamically alocated...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.