ListBox无法正常工作?! [英] ListBox not working?!

查看:66
本文介绍了ListBox无法正常工作?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须说,我完全不知道应该是一个非常简单的

sub ...


它的目的是使用指定路径

的文件名填充ListBox。我知道Files()接收文件名,但是ListBox

什么也没做!没有项目出现,我已经尝试了一切。没有

异常出现,什么都没有。


这里是子...我错过了什么?我也试过单独添加

项目(例如ListBox1.Add(Files(0))等,但没有用)。

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()


结束子

非常感谢,


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

推荐答案



MrQuan写道:

< snip>

MrQuan wrote:
<snip>

它的目的是用指定路径

的文件名填充ListBox。我知道Files()接收文件名,但是ListBox

什么也没做!没有项目出现,我已经尝试了一切。否

异常出现,没有。
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>

<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()


结束子
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>


这里工作正常。


也许你设置了ListBox的数据源并忘了清除它? br />

问候,


布兰科。

<snip>

It works ok here.

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

Regards,

Branco.


"路径允许参数指定相对或绝对路径

信息。相对路径信息被解释为相对于

当前工作目录。

来源:Directory.GetFiles方法的文档。


你需要仔细观察的是DriveLetter变量。


我怀疑,你连接到Path变量

它的价值是单个字符,例如C。


这将返回C\DOCS \的Path变量的值。


此值将被GetFiles方法解释为相关路径。


如果您没有明确设置CurrentDirectory,那么CurrentDirectory将是

您的路径可执行文件和GetFiles将相对于那个

目录运行。


例如,如果您的可执行文件位于C:\ MyWidget \ bin \\ \\ Debug中, GetFiles

方法将返回C:\ MyyWidget \ bin \Debug \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 。


如果你想指定一个非相对路径,那么你的Driveletter变量

应该包含C:或者你的串联应该是Path = DriveLetter&

":\ DOCS \"

" MrQuan" < mr ****** @ hotmail.comwrote in message

news:11 ********************** @ h40g2000cwb .googlegr oups.com ...
"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...

>我必须说,我完全不知所措应该是什么应该是非常简单的

sub ...


它的目的是用指定路径

的文件名填充ListBox。我知道Files()接收文件名,但是ListBox

什么也没做!没有项目出现,我已经尝试了一切。没有

异常出现,什么都没有。


这里是子...我错过了什么?我也试过单独添加

项目(例如ListBox1.Add(Files(0))等,但无效)。


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


非常感谢,


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



谢谢Stephany,


DriveLetter是一个包含冒号的字符串。即C:,而不是

ListBox代码,Files()确实填充了指定路径的文件名

(我用断点检查了这一点)。


我对这个问题感到困惑。我已经删除了控件并再次添加了

,以便完全不受约束等等......但仍然没有。非常好

很奇怪!


非常感谢,

MrQuan

Stephany Young写道:
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:

"允许路径参数指定相对或绝对路径

信息。相对路径信息被解释为相对于

当前工作目录。

来源:Directory.GetFiles方法的文档。


你需要仔细观察的是DriveLetter变量。


我怀疑,你连接到Path变量

它的价值是单个字符,例如C。


这将返回C\DOCS \的Path变量的值。


此值将被GetFiles方法解释为相关路径。


如果您没有明确设置CurrentDirectory,那么CurrentDirectory将是

您的路径可执行文件和GetFiles将相对于那个

目录运行。


例如,如果您的可执行文件位于C:\ MyWidget \ bin \\ \\ Debug中, GetFiles

方法将返回C:\ MyyWidget \ bin \Debug \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 。


如果你想指定一个非相对路径,那么你的Driveletter变量

应该包含C:或者你的串联应该是Path = DriveLetter&

":\ DOCS \"。


" MrQuan" < mr ****** @ hotmail.comwrote in message

news:11 ********************** @ h40g2000cwb .googlegr oups.com ...
"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...

我必须说,我完全难以理解应该是一个非常简单的

sub ...


它的目的是用指定路径

的文件名填充ListBox。我知道Files()接收文件名,但是ListBox

什么也没做!没有项目出现,我已经尝试了一切。没有

异常出现,什么都没有。


这里是子...我错过了什么?我也试过单独添加

项目(例如ListBox1.Add(Files(0))等,但没有用)。

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()


结束子

非常感谢,


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


这篇关于ListBox无法正常工作?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆