Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Dear all,
Can any one explain(with sample code) how to create UI with controls at runtime using VB.Net. e.g. if user needs to add 2 labels & 1 textbox at a particular X,Y position(Based on Drag-drop) on the form he should be able to do it. Some other user want to add 5 labels at specific coordinate; he should be able to do it.

Thanks in advance..
[no name] 30-Jul-13 10:19am    
Create the controls and set the location. What is the problem?
Sergey Alexandrovich Kryukov 30-Jul-13 12:59pm    
The usual problem is "MyControl.Parent = ..." or "myParentControl.Controls.Add(MyControl);"
Please see my answer: I provide and advice how to generate required "sample code".
ZurdoDev 30-Jul-13 12:57pm    
There is too much code to do this and there is no magic code that will work everywhere. For sample code, please ask Mr. Google.
Sergey Alexandrovich Kryukov 30-Jul-13 12:59pm    
Not really. Please see my answer — this is all one needs. Nothing to Google about.
ZurdoDev 30-Jul-13 13:17pm    
You don't answer their question at all. They want the user to be able to drag and drop controls.

1 solution

The sample code already lies under your fingers, you just need to spot it.

I'll better explain you a pretty productive method of finding out all required techniques.

To start with, you should understand that there is no creation of controls "not in runtime", or during design time. No matter what you do, the control are always created during runtime. And of course, you need to learn what really happens, instead of using just the designer without understanding the essence of things. The designer is merely a visual tool used to generate some code which is actually executed during runtime. (And you should use the designer with care; overusing it is a usual problem leading to a lot of repetitive manual work without proper reuse and supportability.)

That said, here is what you can do: create the controls and their layout the way you want using the designer. When you are done, you will see some auto-generated code; it will be placed as a child node of the form node. Open the generated code and learn how to right the same thing. That would be the same very "sample code" you requested in your question. That's all.

Share this answer
Manfred Rudolf Bihy 30-Jul-13 13:17pm    
Now that answer gets a well deserved 5! If people only took the time to look at what the visual designer generated as code there wouldn't be so many repetitive questions about how to "dynamically" generate controls at "runtime". That's the way it is done after all. :)

Sergey Alexandrovich Kryukov 30-Jul-13 14:34pm    
Right. Thank you, Manfred.
DDR-4 1-Aug-13 1:55am    
Thanks SA..
Sergey Alexandrovich Kryukov 1-Aug-13 8:13am    
You are very welcome.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900