Results 1 to 19 of 19

Thread: Combo box issue

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17

    Combo box issue

    Ok i have a combo box... that gets populated apon running....
    I want to be able to have something selected and click a button and have an action taken when the button is clicked...
    Here is my Code:
    VB Code:
    1. Dim pathC, PathA, PathD, PathE As Object
    2.     Dim mp3, wav As Object
    3.  
    4. Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5.         mp3 = ".Mp3"
    6.         wav = ".Wav"
    7.         pathC = "C:\"
    8.         PathA = "A:\"
    9.         PathD = "D:\"
    10.         PathE = "E:\"
    11.         cmbDrives.Items.Add(pathC)
    12.         cmbDrives.Items.Add(PathA)
    13.         cmbDrives.Items.Add(PathD)
    14.         cmbDrives.Items.Add(PathE)
    15.         cmbType.Items.Add(mp3)
    16.         cmbType.Items.Add(wav)
    17.     End Sub
    When i Click the button Search i want it to look at the selected field in the combobox (CmbDrives) and just tell me what is selected...(through a msgbox)
    there is more but that is my only question untill i get stuck again
    Remember im a n00b

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    VB Code:
    1. MessageBox.Show(ComboBox1.SelectedItem.ToString)

    ?

  3. #3
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    There are two ways of doing that.

    VB Code:
    1. MsgBox(ComboBox1.SelectedIndex) 'Displays a number from 0-TheNumberOfItems-1
    2. MsgBox(ComboBox1.Text) ' Displays the current text in the combo.

    EDIT: Damn mendhak. You beat me to the punch...
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17
    VB Code:
    1. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    2.         If cmbDrives.SelectedItem(mp3) Then
    3.             MessageBox.Show("yay")
    4.         End If
    5.     End Sub
    i get an error
    cast from string ".mp3" to integer no valid ...
    Remember im a n00b

  5. #5
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    You are using cmbDrives, but you add the MP3 to cmbType?!?
    You probably should check cmbType instead.

    VB Code:
    1. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    2.         If cmbType.Text=mp3 Then
    3.             MessageBox.Show("yay")
    4.         End If
    5.     End Sub
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17
    VB Code:
    1. Imports System.IO
    2. Public Class form1
    3.     Inherits System.Windows.Forms.Form
    4.     Dim pathC, PathA, PathD, PathE As Object
    5.     Dim mp3, wav As Object
    6. #Region " Windows Form Designer generated code "
    7.  
    8.     Public Sub New()
    9.         MyBase.New()
    10.  
    11.         'This call is required by the Windows Form Designer.
    12.         InitializeComponent()
    13.  
    14.         'Add any initialization after the InitializeComponent() call
    15.  
    16.     End Sub
    17.  
    18.     'Form overrides dispose to clean up the component list.
    19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    20.         If disposing Then
    21.             If Not (components Is Nothing) Then
    22.                 components.Dispose()
    23.             End If
    24.         End If
    25.         MyBase.Dispose(disposing)
    26.     End Sub
    27.  
    28.     'Required by the Windows Form Designer
    29.     Private components As System.ComponentModel.IContainer
    30.  
    31.     'NOTE: The following procedure is required by the Windows Form Designer
    32.     'It can be modified using the Windows Form Designer.  
    33.     'Do not modify it using the code editor.
    34.     Friend WithEvents clbResults As System.Windows.Forms.CheckedListBox
    35.     Friend WithEvents btnSearch As System.Windows.Forms.Button
    36.     Friend WithEvents btnDelete As System.Windows.Forms.Button
    37.     Friend WithEvents btnMove As System.Windows.Forms.Button
    38.     Friend WithEvents Label1 As System.Windows.Forms.Label
    39.     Friend WithEvents Label2 As System.Windows.Forms.Label
    40.     Friend WithEvents Label3 As System.Windows.Forms.Label
    41.     Friend WithEvents Label4 As System.Windows.Forms.Label
    42.     Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
    43.     Friend WithEvents FolderBrowserDialog1 As System.Windows.Forms.FolderBrowserDialog
    44.     Friend WithEvents Label5 As System.Windows.Forms.Label
    45.     Friend WithEvents cmbDrives As System.Windows.Forms.ComboBox
    46.     Friend WithEvents cmbType As System.Windows.Forms.ComboBox
    47.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    48.         Me.clbResults = New System.Windows.Forms.CheckedListBox
    49.         Me.cmbDrives = New System.Windows.Forms.ComboBox
    50.         Me.cmbType = New System.Windows.Forms.ComboBox
    51.         Me.btnSearch = New System.Windows.Forms.Button
    52.         Me.btnDelete = New System.Windows.Forms.Button
    53.         Me.btnMove = New System.Windows.Forms.Button
    54.         Me.Label1 = New System.Windows.Forms.Label
    55.         Me.Label2 = New System.Windows.Forms.Label
    56.         Me.Label3 = New System.Windows.Forms.Label
    57.         Me.Label4 = New System.Windows.Forms.Label
    58.         Me.ProgressBar1 = New System.Windows.Forms.ProgressBar
    59.         Me.FolderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog
    60.         Me.Label5 = New System.Windows.Forms.Label
    61.         Me.SuspendLayout()
    62.         '
    63.         'clbResults
    64.         '
    65.         Me.clbResults.Location = New System.Drawing.Point(208, 24)
    66.         Me.clbResults.Name = "clbResults"
    67.         Me.clbResults.Size = New System.Drawing.Size(280, 244)
    68.         Me.clbResults.TabIndex = 0
    69.         '
    70.         'cmbDrives
    71.         '
    72.         Me.cmbDrives.Location = New System.Drawing.Point(144, 24)
    73.         Me.cmbDrives.Name = "cmbDrives"
    74.         Me.cmbDrives.Size = New System.Drawing.Size(48, 21)
    75.         Me.cmbDrives.TabIndex = 1
    76.         '
    77.         'cmbType
    78.         '
    79.         Me.cmbType.Location = New System.Drawing.Point(136, 48)
    80.         Me.cmbType.Name = "cmbType"
    81.         Me.cmbType.Size = New System.Drawing.Size(56, 21)
    82.         Me.cmbType.TabIndex = 1
    83.         '
    84.         'btnSearch
    85.         '
    86.         Me.btnSearch.Location = New System.Drawing.Point(128, 80)
    87.         Me.btnSearch.Name = "btnSearch"
    88.         Me.btnSearch.TabIndex = 2
    89.         Me.btnSearch.Text = "Search"
    90.         '
    91.         'btnDelete
    92.         '
    93.         Me.btnDelete.Location = New System.Drawing.Point(128, 112)
    94.         Me.btnDelete.Name = "btnDelete"
    95.         Me.btnDelete.TabIndex = 3
    96.         Me.btnDelete.Text = "Delete"
    97.         '
    98.         'btnMove
    99.         '
    100.         Me.btnMove.Location = New System.Drawing.Point(128, 256)
    101.         Me.btnMove.Name = "btnMove"
    102.         Me.btnMove.TabIndex = 4
    103.         Me.btnMove.Text = "Move"
    104.         '
    105.         'Label1
    106.         '
    107.         Me.Label1.Location = New System.Drawing.Point(8, 24)
    108.         Me.Label1.Name = "Label1"
    109.         Me.Label1.Size = New System.Drawing.Size(104, 16)
    110.         Me.Label1.TabIndex = 6
    111.         Me.Label1.Text = "1. Select Drive"
    112.         '
    113.         'Label2
    114.         '
    115.         Me.Label2.Location = New System.Drawing.Point(8, 48)
    116.         Me.Label2.Name = "Label2"
    117.         Me.Label2.Size = New System.Drawing.Size(104, 16)
    118.         Me.Label2.TabIndex = 6
    119.         Me.Label2.Text = "2. Select File Type"
    120.         '
    121.         'Label3
    122.         '
    123.         Me.Label3.Location = New System.Drawing.Point(8, 120)
    124.         Me.Label3.Name = "Label3"
    125.         Me.Label3.Size = New System.Drawing.Size(120, 16)
    126.         Me.Label3.TabIndex = 6
    127.         Me.Label3.Text = "Delete Checked Files"
    128.         '
    129.         'Label4
    130.         '
    131.         Me.Label4.Location = New System.Drawing.Point(8, 88)
    132.         Me.Label4.Name = "Label4"
    133.         Me.Label4.Size = New System.Drawing.Size(104, 16)
    134.         Me.Label4.TabIndex = 6
    135.         Me.Label4.Text = "Search for Files"
    136.         '
    137.         'ProgressBar1
    138.         '
    139.         Me.ProgressBar1.Location = New System.Drawing.Point(208, 264)
    140.         Me.ProgressBar1.Name = "ProgressBar1"
    141.         Me.ProgressBar1.Size = New System.Drawing.Size(280, 16)
    142.         Me.ProgressBar1.TabIndex = 7
    143.         '
    144.         'Label5
    145.         '
    146.         Me.Label5.Location = New System.Drawing.Point(8, 256)
    147.         Me.Label5.Name = "Label5"
    148.         Me.Label5.Size = New System.Drawing.Size(120, 16)
    149.         Me.Label5.TabIndex = 8
    150.         Me.Label5.Text = "Move Selected Files"
    151.         '
    152.         'form1
    153.         '
    154.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    155.         Me.ClientSize = New System.Drawing.Size(496, 285)
    156.         Me.Controls.Add(Me.Label5)
    157.         Me.Controls.Add(Me.ProgressBar1)
    158.         Me.Controls.Add(Me.Label1)
    159.         Me.Controls.Add(Me.btnMove)
    160.         Me.Controls.Add(Me.btnDelete)
    161.         Me.Controls.Add(Me.btnSearch)
    162.         Me.Controls.Add(Me.cmbDrives)
    163.         Me.Controls.Add(Me.clbResults)
    164.         Me.Controls.Add(Me.cmbType)
    165.         Me.Controls.Add(Me.Label2)
    166.         Me.Controls.Add(Me.Label3)
    167.         Me.Controls.Add(Me.Label4)
    168.         Me.Name = "form1"
    169.         Me.Text = "Move Um (v 1.1) "
    170.         Me.ResumeLayout(False)
    171.  
    172.     End Sub
    173.  
    174. #End Region
    175.  
    176.     Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    177.         mp3 = ".Mp3"
    178.         wav = ".Wav"
    179.         pathC = "C:\"
    180.         PathA = "A:\"
    181.         PathD = "D:\"
    182.         PathE = "E:\"
    183.         cmbDrives.Items.Add(pathC)
    184.         cmbDrives.Items.Add(PathA)
    185.         cmbDrives.Items.Add(PathD)
    186.         cmbDrives.Items.Add(PathE)
    187.         cmbType.Items.Add(mp3)
    188.         cmbType.Items.Add(wav)
    189.     End Sub
    190.  
    191.     Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    192.         If cmbDrives.SelectedItem(mp3) Then
    193.             MessageBox.Show("happy")
    194.         End If
    195.     End Sub
    196. End Class
    brings up the error
    Remember im a n00b

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17
    whopps didnt see u posted
    Remember im a n00b

  8. #8
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    We posted at the same time S**t happens.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17
    Now lemme explain what i want to happen.... this application im creating is going to search for certain file types in certain drives....

    I have anougher combo box (holds the information: .mp3, .wav)
    i want when search btn is clicked to check for the drive (got it doing that) and to check to see what file type was selected... i want it to scan the drive for thoose file types... then populate them into a checkboxlist... possible?
    Remember im a n00b

  10. #10
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Yes, it's possible.
    In order for this sample to work you need the following:
    A new form, with 2 combos, 1 button and 1 checklistbox.
    Name the combos cmbDrive and cmbType Set their drop down style to "DropDown". Name the button btnSearch.
    Leave the rest "as is".

    Then paste this code into the form.

    VB Code:
    1. Private Sub Search(ByVal Path As String, ByVal Filter As String)
    2.         Dim dInfo As IO.DirectoryInfo
    3.         Dim dInfos() As IO.DirectoryInfo
    4.         Dim fInfo As IO.FileInfo
    5.         Dim fInfos() As IO.FileInfo
    6.  
    7.         'Search for files in the current path
    8.         Try
    9.             Console.WriteLine("Searchin " & Path)
    10.             fInfos = New IO.DirectoryInfo(Path).GetFiles(Filter)
    11.             For Each fInfo In fInfos
    12.                 CheckedListBox1.Items.Add(fInfo.FullName)
    13.                 CheckedListBox1.Refresh()
    14.             Next
    15.         Catch
    16.         End Try
    17.  
    18.         'Search for subdirs in the currentpath
    19.         Try
    20.             dInfos = New IO.DirectoryInfo(Path).GetDirectories
    21.             For Each dInfo In dInfos
    22.                 'call search again the check for files in the subdir.
    23.                 Me.Search(dInfo.FullName, Filter)
    24.             Next
    25.         Catch
    26.         End Try
    27.  
    28.     End Sub
    29.     Private Sub frmTemp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    30.         cmbDrive.Items.Add("C:\")
    31.         cmbDrive.Items.Add("D:\")
    32.  
    33.         cmbType.Items.Add(".wav")
    34.         cmbType.Items.Add(".mp3")
    35.     End Sub
    36.     Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    37.         CheckedListBox1.Items.Clear()
    38.  
    39.         Me.Search(cmbDrive.Text, "*" & cmbType.Text)
    40.     End Sub
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17
    well ty... i got 2 more questions... one could be long.. idk never used it... and 1 easy.... is there a way so i just name the file not the whole directory its found in?
    and then can i apply a Progress bar?
    Remember im a n00b

  12. #12
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Yeah, the name part is easy. Just use fInfo.Name instead of fInfo.FullName.

    The progressbar is a bit harder. For it to work properly you would need to know the number of files that you will end up with.
    The closest thing would be to make a progressbar with a max value of 100, and the just reset it and start over whenever it runs full. This will not be an accurate measurement, since it just counts away, but it will let the user know that something is going on.

    Just add a counter within each "For Each fInfo In Finfos".
    If the value of that counter exceeds the max of the progressbar set it to 0. Then set the progressbar value=counter.
    Remember to do a refresh.

    Hope it helps.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  13. #13

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17
    well of course there is an issue....
    i changed the fullname. to name... and now nothing happens.... nothing is displayed at all
    Remember im a n00b

  14. #14
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Strange. Are you sure you changed the right line?

    CheckedListBox1.Items.Add(fInfo.FullName)

    to

    CheckedListBox1.Items.Add(fInfo.Name)
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  15. #15

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17
    well heres my program so far.. i made the changes so u can check it out... maybe its jsut not working on my comp... when i run the prog and search it does nothing........
    Attached Files Attached Files
    Remember im a n00b

  16. #16
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Found it.

    the line
    VB Code:
    1. Me.Search(dinfo.Name, Filter)

    should still be FullName.
    Only fInfo should be Name.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  17. #17

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17
    i thank you
    Remember im a n00b

  18. #18

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Location
    Berlin, Connecticut
    Posts
    17
    ok but now... this question is abit harder....
    when i search i find some outrageous mp3's.... is there a way where i can search only certain folders..... Or by pas like system and game folders?
    Remember im a n00b

  19. #19
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Well, that's a bit harder.

    You can easily check for System and Hidden, but games and programs are a lot harder.

    You can check dInfo.Attributes to see if it's a hidden and/or system directory before calling For Each fInfo In fInfos...
    VB Code:
    1. If (dInfo.Attributes And IO.FileAttributes.System) = IO.FileAttributes.System Then
    2.             DontSearch = True
    3.         End If
    4.         If (dInfo.Attributes And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
    5.             DontSearch = True
    6.         End If


    If you want to, you could also check dInfo.Name to see if it should be searched or not, but that would require a list of names that should be known beforehand.
    Perhaps a collection of strings that the user can enter themselves.
    Then check the collection to see if the name is present. If it is, then don't search.
    This way the user can narrow the search as they like.
    If you don't want that, I can't see any other easy way to determine wether or not to search it.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width