为什么trackBar2.Maximum显示16步而不是15?

时间:2014-10-14 12:25:29

标签: c# .net winforms

位图是数组,包含从0到14的15个索引。 但是在将trackBar2.Maximum设置为bitmaps Length后,我在trackBar2上看到16个地方不是15。

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            trackBar2.Enabled = true;
            trackBar2.Maximum = bitmaps.Length;
            for (int i = 0; i < bitmaps.Length; i++)
            {
               ConvertTo24(bitmaps[i]);
            }
            timer2.Stop();
            b1.Dispose();
        }

然后在trackBar2滚动事件中:

private void trackBar2_Scroll(object sender, EventArgs e)
        {
            LoadPictureAt(trackBar2.Value, sender);
            mymem = ToStream(bitmaps[trackBar2.Value], ImageFormat.Bmp);
            backTexture = TextureLoader.FromStream(D3Ddev, mymem);
            scannedCloudsTexture = new Texture(D3Ddev, 512, 512, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default);
            timer1.Stop();
            Button1Code();
            timer1.Start();
            pictureBox1.Refresh();
        }

当我将scrollBar2移动到顶部时,它会抛出异常:索引超出了数组的范围

System.IndexOutOfRangeException was caught
  HResult=-2146233080
  Message=Index was outside the bounds of the array.
  Source=My Weather Station
  StackTrace:
       at mws.ScanningClouds.trackBar2_Scroll(Object sender, EventArgs e) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\ScanningClouds.cs:line 709
       at System.Windows.Forms.TrackBar.OnScroll(EventArgs e)
       at System.Windows.Forms.TrackBar.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WmVScroll(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
       at System.Windows.Forms.Control.DefWndProc(Message& m)
       at System.Windows.Forms.Control.WmMouseMove(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.TrackBar.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at mws.Program.Main() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Program.cs:line 28
  InnerException: 

第709行是:

mymem = ToStream(bitmaps[trackBar2.Value], ImageFormat.Bmp);

trackBar2最小值设置为0我试图将其设置为1,但后来我在同一行上获得异常。同样的例外,即使现在trackBar2还有15个步骤,当我将trackBar2滚动条拖到顶部时,仍有相同的异常。

1 个答案:

答案 0 :(得分:1)

TrackBar&#39; Minimum&amp; Maximum界限是包容性的。尝试设置trackBar2.Maximum = bitmaps.Length - 1

如果你想要一个友好的&#34; GUI,最好设置.Minimum = 1&amp; .Maximum = bitmaps.Length并将其Value用作int pictureIndex = trackBar2.Value - 1;