Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How-To create labels at run time

kmsmithkmsmith Posts: 4Member
I want to share some VB6 code with you. I wrote this because I wanted to check how many drives were on my system then display some data. If I did not have a floppy disk or CDROM inserted then I would get a failure error. This program will tell you if it finds a drive (or mapped drive) then it will display the drives wheteher it has amedia in the drives or not. It will use the DRIVECOUNTER for the max number of drives, then create labels for each drive. Just open a new project, cut and paste this code. Hope this helps others.
==============================================
Option Explicit
'Written by Kimberly Smith 5/04/2004
'This program will find your drives (mapped also)
'display info on for them, create labels,
'depending on how many drives it finds
'Demostrates how to create labels at runtime
'Showdrivelist and Showdriveinfo came from MSDN examples

Dim DriveCounter ' number of drives on system
Dim l As Label 'Label created on form at startup

Private Sub Start()
ShowDriveList
MsgBox ("Good Bye")
Unload Me
End
End Sub

Sub CreateLabels()
Dim i
Dim y As Variant
For i = 1 To DriveCounter
Dim z As Label
y = "lbl" + CStr(i)
Set z = Controls.Add("vb.label", y)
z.Width = 800
z.Height = 1700
z.Top = 100
z.Left = Me.Width / DriveCounter + (i * 1000)
z.Caption = "Dynamic Label " + CStr(i)
z.Visible = True
Next
MsgBox "The LABELS above were Created Dynamically at Run-time, number of labels depends on how many drives (or mapped drives) your system has. ", _
, "Dynamic Controls"
End Sub

Private Sub Form_Load()
Set l = Controls.Add("vb.label", "L")
l.Width = 5000
l.Height = 2000
l.Caption = "This label was created at runtime. This program will find your drives, diplay them, add labels to the form for each drive. Take a look at the source code."
l.Left = 2000
l.Top = 2000
l.Visible = True

Form1.Show
Start

End Sub

Sub ShowDriveList()
Dim fs, d, dc, s, n
Set fs = CreateObject("Scripting.FileSystemObject")
Set dc = fs.Drives
For Each d In dc
DriveCounter = DriveCounter + 1
If d.IsReady Then
Select Case d ' Evaluate Number.
Case 3
n = d.ShareName
Case Else
n = d.VolumeName
End Select
ShowDriveInfo (d)
End If
s = s & d.DriveLetter & " - "
s = s & n & vbCrLf
Next
CreateLabels
MsgBox s
Unload Me
End Sub

Sub ShowDriveInfo(drvPath)
Dim fs, d, s, t
Set fs = CreateObject("Scripting.FileSystemObject")
Set d = fs.GetDrive(drvPath)
Select Case d.DriveType
Case 0: t = "Unknown"
Case 1: t = "Removable"
Case 2: t = "Fixed"
Case 3: t = "Network"
Case 4: t = "CD-ROM"
Case 5: t = "RAM Disk"
End Select
s = "Drive " & d.DriveLetter & ": - " & t
If d.IsReady Then
s = s & vbCrLf & "Drive is Ready."
Else
s = s & vbCrLf & "Drive is not Ready."
End If
MsgBox s
l.Visible = False
End Sub

Comments

  • KDivad LeahcimKDivad Leahcim Posts: 3,948Member
    : I want to share some VB6 code with you. I wrote this because I wanted to check how many drives were on my system then display some data. If I did not have a floppy disk or CDROM inserted then I would get a failure error. This program will tell you if it finds a drive (or mapped drive) then it will display the drives wheteher it has amedia in the drives or not. It will use the DRIVECOUNTER for the max number of drives, then create labels for each drive. Just open a new project, cut and paste this code. Hope this helps others.
    : ==============================================
    : Option Explicit
    : 'Written by Kimberly Smith 5/04/2004
    : 'This program will find your drives (mapped also)
    : 'display info on for them, create labels,
    : 'depending on how many drives it finds
    : 'Demostrates how to create labels at runtime
    : 'Showdrivelist and Showdriveinfo came from MSDN examples
    :
    : Dim DriveCounter ' number of drives on system
    : Dim l As Label 'Label created on form at startup
    :
    : Private Sub Start()
    : ShowDriveList
    : MsgBox ("Good Bye")
    : Unload Me
    : End
    : End Sub
    :
    : Sub CreateLabels()
    : Dim i
    : Dim y As Variant
    : For i = 1 To DriveCounter
    : Dim z As Label
    : y = "lbl" + CStr(i)
    : Set z = Controls.Add("vb.label", y)
    : z.Width = 800
    : z.Height = 1700
    : z.Top = 100
    : z.Left = Me.Width / DriveCounter + (i * 1000)
    : z.Caption = "Dynamic Label " + CStr(i)
    : z.Visible = True
    : Next
    : MsgBox "The LABELS above were Created Dynamically at Run-time, number of labels depends on how many drives (or mapped drives) your system has. ", _
    : , "Dynamic Controls"
    : End Sub
    :
    : Private Sub Form_Load()
    : Set l = Controls.Add("vb.label", "L")
    : l.Width = 5000
    : l.Height = 2000
    : l.Caption = "This label was created at runtime. This program will find your drives, diplay them, add labels to the form for each drive. Take a look at the source code."
    : l.Left = 2000
    : l.Top = 2000
    : l.Visible = True
    :
    : Form1.Show
    : Start
    :
    : End Sub
    :
    : Sub ShowDriveList()
    : Dim fs, d, dc, s, n
    : Set fs = CreateObject("Scripting.FileSystemObject")
    : Set dc = fs.Drives
    : For Each d In dc
    : DriveCounter = DriveCounter + 1
    : If d.IsReady Then
    : Select Case d ' Evaluate Number.
    : Case 3
    : n = d.ShareName
    : Case Else
    : n = d.VolumeName
    : End Select
    : ShowDriveInfo (d)
    : End If
    : s = s & d.DriveLetter & " - "
    : s = s & n & vbCrLf
    : Next
    : CreateLabels
    : MsgBox s
    : Unload Me
    : End Sub
    :
    : Sub ShowDriveInfo(drvPath)
    : Dim fs, d, s, t
    : Set fs = CreateObject("Scripting.FileSystemObject")
    : Set d = fs.GetDrive(drvPath)
    : Select Case d.DriveType
    : Case 0: t = "Unknown"
    : Case 1: t = "Removable"
    : Case 2: t = "Fixed"
    : Case 3: t = "Network"
    : Case 4: t = "CD-ROM"
    : Case 5: t = "RAM Disk"
    : End Select
    : s = "Drive " & d.DriveLetter & ": - " & t
    : If d.IsReady Then
    : s = s & vbCrLf & "Drive is Ready."
    : Else
    : s = s & vbCrLf & "Drive is not Ready."
    : End If
    : MsgBox s
    : l.Visible = False
    : End Sub
    :
    :

    Very interesting, though I can do the same in far fewer lines and without using that da**able FSO and while still recieving events for runtime-created controls and maintain compat with VB5 and and and....
  • kmsmithkmsmith Posts: 4Member

    :
    : Very interesting, though I can do the same in far fewer lines and without using that da**able FSO and while still recieving events for runtime-created controls and maintain compat with VB5 and and and....
    :

    Then by all means do so. Always interested in condensing code and other ways to do the same thing.
  • KDivad LeahcimKDivad Leahcim Posts: 3,948Member
    : Then by all means do so. Always interested in condensing code and other ways to do the same thing.
    :

    The only reason I don't is because this is a board for requesting help and not a board for posting random, although often helpful, tidbits of info.

    Now, if someone were to ask how to write code that would enumerate the drives and display their type in a dynamic array of labels, then I'd be happy to help.
Sign In or Register to comment.