将最大项添加到ListBox以避免IndexOutOfRange [英] Add max items to ListBox to avoid IndexOutOfRange

查看:46
本文介绍了将最大项添加到ListBox以避免IndexOutOfRange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须连续更改文件名,我用stringbuilder数组更改它们,将它们放在之前ListBox和AfterListBox中:虽然少于19项(height / itemHeight),但一切正常很好,如果没有足够的地方,我得到IndexOutOfRange异常



2.如果我想使用try-catch,我必须把它放在哪里?



我在Program.cs中遇到错误 Application.Run(new MainForm()); Main()中,我是否必须在此处捕获异常,或者在方法Button_click中将项目添加到listBoxes?



我已经看到,如果你不知道该怎么做,你一定不能解决异常,但我想知道一个可视化的例子(不是msdn.com中使用控制台的那个)



提前致谢



我收到错误:

i have to change file names serially, i change them with a stringbuilder array, put them in a "before" ListBox and in a "After" ListBox: while there are less than 19 items (height/itemHeight), everything works fine, if there isn't enough place, i get IndexOutOfRange exception

2.in the case i want to use try-catch, where do i have to put it?

I get the error in Program.cs at line Application.Run(new MainForm()); in Main() , do i have to catch exceptions here, or in the method Button_click, that add items to listBoxes?

i've seen that if you don't know what to do, you mustn't cacth exception, but i wanted to know a visual example (not the one in msdn.com, that uses console)

Thanks in advance

I get the error here:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());//error here
    }

here i work with listboxes
      
    for (int i = 0; i < songsnum; i++)
    {
        songListBef.Items.Add(Path.GetFileName(full_fname_ex[i]));
        fname_noex_b[i] = new StringBuilder(Path.GetFileNameWithoutExtension(full_fname_ex[i]));
        fname_noex_b[i].Remove(0, toRemove_bef);
        fname_noex_b[i].Remove(fname_noex_b[i].Length - toRemove_aft, toRemove_aft);
    }

...
    
    for (int i = 0; i < songsnum; i++)
    {
        fname_noex_b[i].Append(Path.GetExtension(full_fname_ex[i]));
        songListAft.Items.Add(fname_noex_b[i].ToString());
    }

推荐答案

ListBix不太可能是异常的来源,因为您只是添加项目。我的猜测是数组(或列表或其他容器)full_name_ex和fname_noex_b只包含19项。



我建议你去调试/异常...... Visual Studio并设置Checkboxes以在抛出它们时获取异常。这应该有助于使调试器完全正确。
The ListBix is unlikely to be the source of the exception as you are only adding items. My guess is that the Arrays (or Lists or other containers) full_name_ex and fname_noex_b only contain 19 items.

I would suggest you go to Debug/Exceptions... in Visual Studio and set the Checkboxes to get the exceptions the moment they are thrown. This should help in getting the debugger to the exact loaction.


不确定这是否是您的问题,但可能是;看起来你正在尝试向ListBox添加一个项目而不是项目列表中的项目。



假设songum = 20并且你从0到20循环,这实际上是21项。如果你只有20个项目,那么你将得到一个IndexOutOfRange错误。



在你的循环中尝试这个,注意我们正在检查比songsnum值少一个...

Not sure if this is your issue but it probably is; it looks like you are trying to add one more item to the ListBox than there is in your item list.

Let's say songsum = 20 and you loop from 0 to 20, that's actually 21 items. If you only have 20 items then you will get an IndexOutOfRange error.

Try this in your loops, notice we are checking for one less than the songsnum value...
for (int i = 0; i < songsnum - 1; i++)
{
    songListBef.Items.Add(Path.GetFileName(full_fname_ex[i]));
    fname_noex_b[i] = new StringBuilder(Path.GetFileNameWithoutExtension(full_fname_ex[i]));  
    fname_noex_b[i].Remove(0, toRemove_bef);
    fname_noex_b[i].Remove(fname_noex_b[i].Length - toRemove_aft, toRemove_aft);
}



...


...

for (int i = 0; i < songsnum - 1; i++)
{
    fname_noex_b[i].Append(Path.GetExtension(full_fname_ex[i]));
    songListAft.Items.Add(fname_noex_b[i].ToString());
}


这篇关于将最大项添加到ListBox以避免IndexOutOfRange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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