ListBox not working?!

M

MrQuan

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
 
B

Branco Medeiros

MrQuan wrote:
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.
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.
 
S

Stephany Young

"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\".
 
M

MrQuan

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
 
M

MrQuan

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
 
R

RobinS

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.
------------------------------------
 
S

Stephany Young

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.
 
S

Stephany Young

Can you please explain the reason that it would fail because of the '\'
before DOCS & '\' after?
 
B

Branco Medeiros

MrQuan said:
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.
 
C

Cor Ligthert [MVP]

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
 
N

Newbie Coder

It was failing because the path was C:\\Docs\

The drive letter being picked up already contains the :\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top