473,325 Members | 2,308 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,325 software developers and data experts.

ListBox not working?!

I must say, I'm completly stumped at what should be a very simple
sub...

It's purpose is to fill a ListBox with filenames from the path
specified. I know Files() receives the filenames, however the ListBox
doesn't do anything!! No items appear, I've tried everything. No
exceptions arise, nothing.

Here's the sub... am I missing something? I've also tried adding the
items individually (e.g. ListBox1.Add(Files(0)) etc, with no avail).
Public Sub FillListBox()

Dim Path As String
Dim Files() As System.Object

Path = DriveLetter & "\DOCS\"
Files = Directory.GetFiles(Path)

ListBox1.BeginUpdate()
ListBox1.Items.AddRange(Files)
ListBox1.EndUpdate()

End Sub
Many thanks,

MrQuan

Jan 1 '07 #1
11 1515

MrQuan wrote:
<snip>
It's purpose is to fill a ListBox with filenames from the path
specified. I know Files() receives the filenames, however the ListBox
doesn't do anything!! No items appear, I've tried everything. No
exceptions arise, nothing.
<snip>
Public Sub FillListBox()

Dim Path As String
Dim Files() As System.Object

Path = DriveLetter & "\DOCS\"
Files = Directory.GetFiles(Path)

ListBox1.BeginUpdate()
ListBox1.Items.AddRange(Files)
ListBox1.EndUpdate()

End Sub
<snip>

It works ok here.

Maybe you set the ListBox's DataSource and forgot to clear it?

Regards,

Branco.

Jan 1 '07 #2
"The path parameter is permitted to specify relative or absolute path
information. Relative path information is interpreted as relative to the
current working directory."
Source: Documentation of Directory.GetFiles method.

The point you need to be looking closer at is the DriveLetter variable.

I suspect that, at the point that you concatenate into the Path variable
that it's value is a single character, e.g. "C".

This will retult in a value for the Path variable of "C\DOCS\".

This value will be interpreted by the GetFiles methods as a releative path.

If you have not explicity set CurrentDirectory then CurrentDirectory will be
the path to your executable and GetFiles will operate relative to that
directory.

For example, if your executable is in C:\MyWidget\bin\Debug, the GetFiles
method will return those files in C:\MyWidget\bin\Debug\C\DOCS which, I
further suspect, does not exist.

If you want to specify a non-relative path then your Driveletter variable
should contain "C:" or your concatenation should be Path = DriveLetter &
":\DOCS\".
"MrQuan" <mr******@hotmail.comwrote in message
news:11**********************@h40g2000cwb.googlegr oups.com...
>I must say, I'm completly stumped at what should be a very simple
sub...

It's purpose is to fill a ListBox with filenames from the path
specified. I know Files() receives the filenames, however the ListBox
doesn't do anything!! No items appear, I've tried everything. No
exceptions arise, nothing.

Here's the sub... am I missing something? I've also tried adding the
items individually (e.g. ListBox1.Add(Files(0)) etc, with no avail).
Public Sub FillListBox()

Dim Path As String
Dim Files() As System.Object

Path = DriveLetter & "\DOCS\"
Files = Directory.GetFiles(Path)

ListBox1.BeginUpdate()
ListBox1.Items.AddRange(Files)
ListBox1.EndUpdate()

End Sub
Many thanks,

MrQuan

Jan 1 '07 #3
Thanks Stephany,

DriveLetter is a string that does contain the colon. I.e. "C:", not
just "C". I double-checked this. Everything works until I get to the
ListBox code, Files() does indeed fill with filenames of the path
specified (I checked this with a breakpoint).

I'm baffled by the problem. I've deleted the control and added it
again, so as to be completly unbound etc... and still nothing. Very
strange!

Many thanks,
MrQuan
Stephany Young wrote:
"The path parameter is permitted to specify relative or absolute path
information. Relative path information is interpreted as relative to the
current working directory."
Source: Documentation of Directory.GetFiles method.

The point you need to be looking closer at is the DriveLetter variable.

I suspect that, at the point that you concatenate into the Path variable
that it's value is a single character, e.g. "C".

This will retult in a value for the Path variable of "C\DOCS\".

This value will be interpreted by the GetFiles methods as a releative path.

If you have not explicity set CurrentDirectory then CurrentDirectory will be
the path to your executable and GetFiles will operate relative to that
directory.

For example, if your executable is in C:\MyWidget\bin\Debug, the GetFiles
method will return those files in C:\MyWidget\bin\Debug\C\DOCS which, I
further suspect, does not exist.

If you want to specify a non-relative path then your Driveletter variable
should contain "C:" or your concatenation should be Path = DriveLetter &
":\DOCS\".
"MrQuan" <mr******@hotmail.comwrote in message
news:11**********************@h40g2000cwb.googlegr oups.com...
I must say, I'm completly stumped at what should be a very simple
sub...

It's purpose is to fill a ListBox with filenames from the path
specified. I know Files() receives the filenames, however the ListBox
doesn't do anything!! No items appear, I've tried everything. No
exceptions arise, nothing.

Here's the sub... am I missing something? I've also tried adding the
items individually (e.g. ListBox1.Add(Files(0)) etc, with no avail).
Public Sub FillListBox()

Dim Path As String
Dim Files() As System.Object

Path = DriveLetter & "\DOCS\"
Files = Directory.GetFiles(Path)

ListBox1.BeginUpdate()
ListBox1.Items.AddRange(Files)
ListBox1.EndUpdate()

End Sub
Many thanks,

MrQuan
Jan 2 '07 #4
Thanks Branco,

I deleted the ListBox control and added it again fresh so as to avoid
the problem you describe. I'm now beginning to think there may be a
problem with the IDE??? I restarted my IDE (VB 2005 Express) and I
still get this trouble!

Many thanks,
MrQuan
Branco Medeiros wrote:
MrQuan wrote:
<snip>
It's purpose is to fill a ListBox with filenames from the path
specified. I know Files() receives the filenames, however the ListBox
doesn't do anything!! No items appear, I've tried everything. No
exceptions arise, nothing.
<snip>
Public Sub FillListBox()

Dim Path As String
Dim Files() As System.Object

Path = DriveLetter & "\DOCS\"
Files = Directory.GetFiles(Path)

ListBox1.BeginUpdate()
ListBox1.Items.AddRange(Files)
ListBox1.EndUpdate()

End Sub
<snip>

It works ok here.

Maybe you set the ListBox's DataSource and forgot to clear it?

Regards,

Branco.
Jan 2 '07 #5

I put your code into a program and tried it out, and it
works fine.

Are you calling FillListBox() in your Form Load routine?

Also, you should define Files() as String, not System.Object.
Directory.GetFiles() returns a string. That won't affect
whether it works or not, but it's still a good idea.

Robin S.
------------------------------------

"MrQuan" <mr******@hotmail.comwrote in message
news:11**********************@k21g2000cwa.googlegr oups.com...
Thanks Stephany,

DriveLetter is a string that does contain the colon. I.e. "C:", not
just "C". I double-checked this. Everything works until I get to the
ListBox code, Files() does indeed fill with filenames of the path
specified (I checked this with a breakpoint).

I'm baffled by the problem. I've deleted the control and added it
again, so as to be completly unbound etc... and still nothing. Very
strange!

Many thanks,
MrQuan
Stephany Young wrote:
>"The path parameter is permitted to specify relative or absolute path
information. Relative path information is interpreted as relative to
the
current working directory."
Source: Documentation of Directory.GetFiles method.

The point you need to be looking closer at is the DriveLetter
variable.

I suspect that, at the point that you concatenate into the Path
variable
that it's value is a single character, e.g. "C".

This will retult in a value for the Path variable of "C\DOCS\".

This value will be interpreted by the GetFiles methods as a releative
path.

If you have not explicity set CurrentDirectory then CurrentDirectory
will be
the path to your executable and GetFiles will operate relative to
that
directory.

For example, if your executable is in C:\MyWidget\bin\Debug, the
GetFiles
method will return those files in C:\MyWidget\bin\Debug\C\DOCS which,
I
further suspect, does not exist.

If you want to specify a non-relative path then your Driveletter
variable
should contain "C:" or your concatenation should be Path =
DriveLetter &
":\DOCS\".
"MrQuan" <mr******@hotmail.comwrote in message
news:11**********************@h40g2000cwb.googleg roups.com...
>I must say, I'm completly stumped at what should be a very simple
sub...

It's purpose is to fill a ListBox with filenames from the path
specified. I know Files() receives the filenames, however the
ListBox
doesn't do anything!! No items appear, I've tried everything. No
exceptions arise, nothing.

Here's the sub... am I missing something? I've also tried adding
the
items individually (e.g. ListBox1.Add(Files(0)) etc, with no
avail).
Public Sub FillListBox()

Dim Path As String
Dim Files() As System.Object

Path = DriveLetter & "\DOCS\"
Files = Directory.GetFiles(Path)

ListBox1.BeginUpdate()
ListBox1.Items.AddRange(Files)
ListBox1.EndUpdate()

End Sub
Many thanks,

MrQuan

Jan 2 '07 #6
In that case, does DriveLetter contain "C:" or does it contain "C:\"?

If it is the latter then the resulting Path would be "C:\\DOCS\" which would
cause you problems. In this case the concatenation should be:

Path = DriveLetter & "DOCS\"

If it is the former then Path should indeed contain "C:\DOCS\". Try
displaying the value of Path directly after the concatenation and see what
you get.

Another thing to try is declaring Files() as String. Directory.GetFiles
returns a string array so there is no need to 'convert' it back to Object.
"MrQuan" <mr******@hotmail.comwrote in message
news:11**********************@k21g2000cwa.googlegr oups.com...
Thanks Stephany,

DriveLetter is a string that does contain the colon. I.e. "C:", not
just "C". I double-checked this. Everything works until I get to the
ListBox code, Files() does indeed fill with filenames of the path
specified (I checked this with a breakpoint).

I'm baffled by the problem. I've deleted the control and added it
again, so as to be completly unbound etc... and still nothing. Very
strange!

Many thanks,
MrQuan
Stephany Young wrote:
>"The path parameter is permitted to specify relative or absolute path
information. Relative path information is interpreted as relative to the
current working directory."
Source: Documentation of Directory.GetFiles method.

The point you need to be looking closer at is the DriveLetter variable.

I suspect that, at the point that you concatenate into the Path variable
that it's value is a single character, e.g. "C".

This will retult in a value for the Path variable of "C\DOCS\".

This value will be interpreted by the GetFiles methods as a releative
path.

If you have not explicity set CurrentDirectory then CurrentDirectory will
be
the path to your executable and GetFiles will operate relative to that
directory.

For example, if your executable is in C:\MyWidget\bin\Debug, the GetFiles
method will return those files in C:\MyWidget\bin\Debug\C\DOCS which, I
further suspect, does not exist.

If you want to specify a non-relative path then your Driveletter variable
should contain "C:" or your concatenation should be Path = DriveLetter &
":\DOCS\".
"MrQuan" <mr******@hotmail.comwrote in message
news:11**********************@h40g2000cwb.googleg roups.com...
>I must say, I'm completly stumped at what should be a very simple
sub...

It's purpose is to fill a ListBox with filenames from the path
specified. I know Files() receives the filenames, however the ListBox
doesn't do anything!! No items appear, I've tried everything. No
exceptions arise, nothing.

Here's the sub... am I missing something? I've also tried adding the
items individually (e.g. ListBox1.Add(Files(0)) etc, with no avail).
Public Sub FillListBox()

Dim Path As String
Dim Files() As System.Object

Path = DriveLetter & "\DOCS\"
Files = Directory.GetFiles(Path)

ListBox1.BeginUpdate()
ListBox1.Items.AddRange(Files)
ListBox1.EndUpdate()

End Sub
Many thanks,

MrQuan

Jan 2 '07 #7
Can you please explain the reason that it would fail because of the '\'
before DOCS & '\' after?
"Newbie Coder" <ne**********@pleasespamme.comwrote in message
news:OF**************@TK2MSFTNGP02.phx.gbl...
Your example fails for 2 reasons:

The '\' before DOCS & '\' after

The sample application is added to this post

Here's a simple example:

Add a ComboBox1, ListBox1, Button1 to the form

---------------------------------------------

Imports System.io

Private Sub PopulateDrives()
Dim strDrives() As String = Directory.GetLogicalDrives()
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox1.Items.Clear()
For Each strDrive As String In strDrives
ComboBox1.Items.Add(strDrive)
Next
ComboBox1.SelectedIndex = 1
End Sub

Private Sub PopulateListBox(ByVal sDrive As String)
Dim strPath As String = Path.Combine(sDrive, "Docs")
' ListBox1.Items.Clear()
If Directory.Exists(strPath) = True Then
Dim strFiles() As String = Directory.GetFiles(strPath)
ListBox1.BeginUpdate()
ListBox1.Items.Clear()
For Each strFile As String In strFiles
ListBox1.Items.Add(strFile)
Next
ListBox1.EndUpdate()
Else
MessageBox.Show(String.Format("'{0}' doesn't exist", strPath),
"Path Error")
End If
End Sub

Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object,
ByVal
e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
PopulateListBox(ComboBox1.SelectedItem)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Application.Exit()
End Sub

--------------------------------------

I hope this helps,

Newbie Coder

Jan 2 '07 #8

MrQuan wrote:
Thanks Branco,

I deleted the ListBox control and added it again fresh so as to avoid
the problem you describe. I'm now beginning to think there may be a
problem with the IDE??? I restarted my IDE (VB 2005 Express) and I
still get this trouble!
<snip>

You could try adding *another* listbox (one not named ListBox1, i.e.,
instead of deleting the original) and using *that* as the target for
the filling method, to see if has the same problem...

HTH.

Regards,

Branco.

Jan 2 '07 #9
I did the same as Robin with some changes but only because I had not set the
import and not a directory docs. It works perfectly

Dim Path As String
Dim Files() As System.Object

Path = "C:" & "\Windows\"
Files = IO.Directory.GetFiles(Path)

ListBox1.BeginUpdate()
ListBox1.Items.AddRange(Files)
ListBox1.EndUpdate()
Cor
Jan 2 '07 #10
My bet is that he isn't calling the routine when he loads his form.

Robin S.
------------------------
"Cor Ligthert [MVP]" <no************@planet.nlwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>I did the same as Robin with some changes but only because I had not
set the import and not a directory docs. It works perfectly

Dim Path As String
Dim Files() As System.Object

Path = "C:" & "\Windows\"
Files = IO.Directory.GetFiles(Path)

ListBox1.BeginUpdate()
ListBox1.Items.AddRange(Files)
ListBox1.EndUpdate()
Cor


Jan 2 '07 #11
It was failing because the path was C:\\Docs\

The drive letter being picked up already contains the :\

"Stephany Young" <noone@localhostwrote in message
news:O5*************@TK2MSFTNGP02.phx.gbl...
Can you please explain the reason that it would fail because of the '\'
before DOCS & '\' after?
"Newbie Coder" <ne**********@pleasespamme.comwrote in message
news:OF**************@TK2MSFTNGP02.phx.gbl...
Your example fails for 2 reasons:

The '\' before DOCS & '\' after

The sample application is added to this post

Here's a simple example:

Add a ComboBox1, ListBox1, Button1 to the form

---------------------------------------------

Imports System.io

Private Sub PopulateDrives()
Dim strDrives() As String = Directory.GetLogicalDrives()
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox1.Items.Clear()
For Each strDrive As String In strDrives
ComboBox1.Items.Add(strDrive)
Next
ComboBox1.SelectedIndex = 1
End Sub

Private Sub PopulateListBox(ByVal sDrive As String)
Dim strPath As String = Path.Combine(sDrive, "Docs")
' ListBox1.Items.Clear()
If Directory.Exists(strPath) = True Then
Dim strFiles() As String = Directory.GetFiles(strPath)
ListBox1.BeginUpdate()
ListBox1.Items.Clear()
For Each strFile As String In strFiles
ListBox1.Items.Add(strFile)
Next
ListBox1.EndUpdate()
Else
MessageBox.Show(String.Format("'{0}' doesn't exist",
strPath),
"Path Error")
End If
End Sub

Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object,
ByVal
e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
PopulateListBox(ComboBox1.SelectedItem)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Application.Exit()
End Sub

--------------------------------------

I hope this helps,

Newbie Coder



Jan 3 '07 #12

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

Similar topics

3
by: Andrew | last post by:
I'm having a major problem with a databound listbox in C#. In the constructor for the form I am trying to pre-select some of the items based in information in the database. When I step through the...
8
by: Bill | last post by:
I'm trying to create a wizardlike interface using a couple listboxes. I know you've seen it before. You double click on an item in one listbox and it "moves" it to the other. I used to approach...
9
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of text in a textbox, press the 'Enter' key, and have...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
4
by: amber | last post by:
Hello I'm not sure if I should give up trying to find an answer here...or just keep posting my problem.. I'm having problems with a listbox.. I have a listbox that is populated when a user...
5
by: Dave | last post by:
Hi All, I have a windows form that contains 2 listboxes and 2 buttons. The listbox on the right is populated by a database routine (This is the easy part). The listbox on the left is populated...
11
by: John Dann | last post by:
I'm still struggling to find a way of reordering the items within the same single listbox with drag and drop. I think I've got the drag working but it's the drop code I can't figure out. What I...
4
by: lgbjr | last post by:
Hi All, I've got a listbox on a VB.NET form. when the form opens, the ListBox SelectionMode is set to Single. while running various routines on the form, items get added to the list box (results...
6
by: kucheravy | last post by:
Hi, everybody. I have this problem. When I put a <asp:ListBox on a web page and populate the data in the page Page_Load event the ViewState for the control is saved and loaded (after postback)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.