Example of code that dynamically creates controls on form for each HD or CD etc

  • Thread starter Thread starter Malakie
  • Start date Start date
M

Malakie

Hi all,

I am trying to self teach VB .net to myself. Not sure what is worse, trying
to accomplish this or thinking I can actually do it! ;-)

Anyhow, Would someone be able to give me some example VB net code that would
create buttons on a form dynamically for each drive (cd or HD or virtual
etc) found on a system; labeling each created button with one of the found
drive letters. It would also need to remove said button if the drive was
disconnected say in the case of an external usb drive and of course add
buttons as drives are added.

I have been researching this and trying everything I can on my own and am
getting no where fast. Basically I creating a index program for myself as
a learning project but am stuck at trying to figure out how to do this
function. If someone could give me some code that does this that I can
learn from and modify, play with etc, it would be most appreciated.

Thanks.
 
Malakie,

As this are your questions, then first in my opinion you should first create
an easier project for yourself.

This is not a newsgroup that creates code for others, there are many people
active here by helping others, but not with creating programs to them.

If you have questions, then show your code, a lot of people here will help
you to improve that. But not from scratch.

Just my own opinion.

Cor
 
Hi all,

I am trying to self teach VB .net to myself.  Not sure what is worse, trying
to accomplish this or thinking I can actually do it!  ;-)

Anyhow, Would someone be able to give me some example VB net code that would
create buttons on a form dynamically for each drive (cd or HD or virtual
etc) found on a system; labeling each created button with one of the found
drive letters.   It would also need to remove said button if the drive was
disconnected say in the case of an external usb drive and of course add
buttons as drives are added.

I have been researching this and trying everything I can on my own and am
getting no where fast.   Basically I creating a index program for myselfas
a learning project but am stuck at trying to figure out how to do this
function.  If someone could give me some code that does this that I can
learn from and modify, play with etc, it would be most appreciated.

Thanks.

--
until later.....

Malakie
MalakieUSN at Hotmail.com

Malakie,

That's a bit much for a simple demo :) But, the basic concept of
dynamic controls is fairly simple...

Dim b As New Button()
b.Text = "Hello!"
b.Position = new Point(150,150)
AddHandler b.Click, Me.b_Click

Me.Controls.Add (b)

That's pretty much all there is to it. Though, you need to calculate
a possition on the form possibly - replacing the hardcoded values
above :)
 
Tom,

In my idea is this far beside the problem of the OP, but as it is this, then
is in my idea this tip more complete.

http://www.vb-tips.com/DynamicControl.aspx


While this one is even more funny
http://www.vb-tips.com/MoveControl.aspx

Cor

"Tom Shelton" <[email protected]> schreef in bericht
Hi all,

I am trying to self teach VB .net to myself. Not sure what is worse,
trying
to accomplish this or thinking I can actually do it! ;-)

Anyhow, Would someone be able to give me some example VB net code that
would
create buttons on a form dynamically for each drive (cd or HD or virtual
etc) found on a system; labeling each created button with one of the found
drive letters. It would also need to remove said button if the drive was
disconnected say in the case of an external usb drive and of course add
buttons as drives are added.

I have been researching this and trying everything I can on my own and am
getting no where fast. Basically I creating a index program for myself as
a learning project but am stuck at trying to figure out how to do this
function. If someone could give me some code that does this that I can
learn from and modify, play with etc, it would be most appreciated.

Thanks.

--
until later.....

Malakie
MalakieUSN at Hotmail.com

Malakie,

That's a bit much for a simple demo :) But, the basic concept of
dynamic controls is fairly simple...

Dim b As New Button()
b.Text = "Hello!"
b.Position = new Point(150,150)
AddHandler b.Click, Me.b_Click

Me.Controls.Add (b)

That's pretty much all there is to it. Though, you need to calculate
a possition on the form possibly - replacing the hardcoded values
above :)
 
Hi,

Thanks. I will play with what you posted. As for simple demo, well I am
trying to make something that I will actually use as well and thought this
'feature' would be what I want in this kind of utility. So I figure there
is no better way to learn than do something that takes a bit of work and
thought.. And realizing I would probably need some help here and there
once I exhausted my ability to figure it out myself.

Thanks again
 
Hi,

First I am not looking for someone to write my code. I am looking for help
with a specific issue I am having with a project I using to learn from and
will hopefully be a useful utility I want to use. After working on this one
particular thing for days, I am just not figuring it out. So I can either
continue to try and figure it out without a clue on what I should be doing,
I can give up or I can come here and ask for some code examples that will
point me in the right direction so I can learn how to do this.

As for showing my code, how am I supposed to do that when I don't know what
to do what I am trying to do? I do not have any code for that specific
function, that is why I am here! As for the rest of the application if you
want me to upload all the code for that well I don't really see the point.
It is a form with some text boxes on it, a drive list, directory list and
that is it. I want to create buttons that show up dynamically for each
drive on the system, appearing and disappearing as I add or remove drives.
When I click a drive button, the drive and directory lists will update. I
have that working as it was simple to do with plain static hard coded
buttons. But I want to be able to use this on any of my machines here
without having to hardcode what drives exist on each machine.. If the tool
can dynamically add buttons for drives, problem solved.
 
Hi all,

I am trying to self teach VB .net to myself. Not sure what is worse, trying
to accomplish this or thinking I can actually do it! ;-)

Anyhow, Would someone be able to give me some example VB net code that would
create buttons on a form dynamically for each drive (cd or HD or virtual
etc) found on a system; labeling each created button with one of the found
drive letters. It would also need to remove said button if the drive was
disconnected say in the case of an external usb drive and of course add
buttons as drives are added.

I have been researching this and trying everything I can on my own and am
getting no where fast. Basically I creating a index program for myself as
a learning project but am stuck at trying to figure out how to do this
function. If someone could give me some code that does this that I can
learn from and modify, play with etc, it would be most appreciated.

Thanks.

System.IO.DriveInfo.GetDrives() will get you a list of drives. You
can iterate through that list and create the buttons from there.

For each button:
Dim btn As New Button
btn.Text = "some text"
AddHandler btn.Click, AddressOf ButtonClickEventHandler
' Set .Left and .Top properties
Me.Controls.Add(btn)

If you put a FlowLayoutPanel on your form, you can just create and add
buttons to it and the panel will deal with laying them out.

System.IO.FileSystemWatcher can be used to watch for changes in the
file system.
 
Back
Top