105255
Goto Top

Visual Basic 2010 Picture Box mit Resources

Habe ein Programm geschrieben, dass bei einem Klick das nächste bzw. das vorherige Bild angezeigt wird.

Hallo da draußen,

Ich habe folgendes Programm entworfen:

a741185838e362e01b91aa556f8d803d


Wenn man auf den Button drückt, kommt das nächste Bild.

<code vb>
Public Class Form2
Private imgPictures As New List(Of Bitmap)
Private imageShowing As Integer = 0


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

Dim imgPictures(11) As Image
imgPictures(0) = My.Resources.HNI_0001
imgPictures(1) = My.Resources.HNI_0001__2_
imgPictures(2) = My.Resources.HNI_0001__3_
imgPictures(3) = My.Resources.HNI_0001__4_

btn_first.Enabled = False
btn_Previous.Enabled = False

PictureBox1.Image = imgPictures(0)

End Sub


Private Sub btn_First_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_First.Click

PictureBox1.Image = imgPictures(0)
imageShowing = 0
CheckButtons()

End Sub


Private Sub btn_Previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Previous.Click

PictureBox1.Image = imgPictures(imageShowing - 1)
imageShowing -= 1
CheckButtons()

End Sub


Private Sub btn_Next_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Next.Click

PictureBox1.Image = imgPictures(imageShowing + 1)
imageShowing += 1
CheckButtons()

End Sub


Private Sub btn_Last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Last.Click

PictureBox1.Image = imgPictures(imgPictures.Count - 1)
imageShowing = imgPictures.Count - 1
CheckButtons()


End Sub


Private Sub CheckButtons()

If imageShowing = 0 Then
btn_First.Enabled = False
btn_Previous.Enabled = False
btn_Next.Enabled = True
btn_Last.Enabled = True

ElseIf imageShowing = imgPictures.Count - 1 Then
btn_first.Enabled = True
btn_Previous.Enabled = True
btn_next.Enabled = False
btn_last.Enabled = False

Else
btn_First.Enabled = True
btn_Previous.Enabled = True
btn_Next.Enabled = True
btn_Last.Enabled = True

End If

End Sub
End Class


Wenn ich es Ausführe entsteht z.B. ein Fehler bei:

<code vb>
PictureBox1.Image = imgPictures(imageShowing + 1)


Hier kommt zum Beispiel der folgende Fehler:


System.ArgumentOutOfRangeException wurde nicht behandelt.
Message=Der Index lag außerhalb des Bereichs. Er muss nicht negativ und kleiner als die Auflistung sein.
Parametername: index
ParamName=index
Source=mscorlib
StackTrace:
bei System.ThrowHelper.ThrowArgumentOutOfRangeException()
bei System.Collections.Generic.List`1.get_Item(Int32 index)
bei WindowsApplication1.Form2.btn_Next_Click(Object sender, EventArgs e) in C:\Users\Administrator\documents\visual studio 2010\Projects\Login\Login\Form2.vb:Zeile 46.
bei System.Windows.Forms.Control.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
bei System.Windows.Forms.Control.WndProc(Message& m)
bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
bei System.Windows.Forms.Button.WndProc(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String commandLine)
bei WindowsApplication1.My.MyApplication.Main(String Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()
InnerException:


Was soll ich machen?
Ich habe den Code jetzt schon 2 Stunden lang durchgeschaut.

Kann mir bitte jemand helfen?

Content-Key: 185851

Url: https://administrator.de/contentid/185851

Ausgedruckt am: 28.03.2024 um 10:03 Uhr

Mitglied: MrTrebron
MrTrebron 03.06.2012 um 18:16:08 Uhr
Goto Top
Servus,

Also jetzt sag mal ehrlich:
Willst du VB.net nicht mal endlich von Grund auf lernen?

Du stellst hier Fragen die nicht Aufkommen würden wenn du mal die Bücher lesen würdest die die hier schon zur Genüge angeraten wurde. So ein Buch von Galileo in der OpenBook Variante koset dich keinen Cent, bringt dich aber wenn du es mal richtig durcharbeitest viel weiter.

Gruß
Norbert
Mitglied: catachan
catachan 04.06.2012 um 10:32:47 Uhr
Goto Top
Hi

du greifst auf einen Index zud en es nicht gibt bei : imgPictures(imageShowing + 1)
Irgendwann hat imageShowing den Maximalwert erreicht und wenn du dann noch 1 weiterfährst kommt es zu einem Speicherfehler.
Du musst also auf den Maximalwert prüfen und dann nicht mehr um 1 erhöhen.

Sollte dir allerdings selber klar sein wenn du endlich mal auf die anderen Mitglieder hören würdest und mal endlich eine ordentliche Einstiegslektüre in die Hand nimmst, statt jede Kleinigkeit hier zu posten.

LG