本文介绍了将最大项添加到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());
    }

推荐答案


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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 09:38