|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (2 bit)
Join Date: May 2011
Posts: 3
|
Problem with Visual basic next and previous button code
I am working with visual basic and am a complete noob wen it comes to programming, i am trying to teach myself and i have found that you have to be very patient but i am trying to write a code for a next and previous button for a picture viewer, i would like to have a code for this but if someone can teach me how to write one that would be awesome but please explain it in noob terms. I am however a computer tech so i know functions of the pc just not programming and code, just trying to expand my knowledge
|
|
|
|
|
|
#2 |
|
Member (10 bit)
Join Date: Jan 2010
Location: N. Calif.
Posts: 529
|
Do you already have code that reads the disk and loads the picture names into an array? If so, it would be pretty easy to have the next button increment the index into the array to load the next picture while the previous button would decrement the index. You would need to grey out the previous button when the index pointed to the first picture and grey out the next button when the index was on the last picture. Either that or you could "wrap" it so when on picture 1 and previous button clicked it would set index to the last picture, when on last picture and next clicked set index to point to first picture.
__________________
Been using, building, repairing and programming computers for nearly 30 years now. |
|
|
|
|
|
#3 |
|
Member (2 bit)
Join Date: May 2011
Posts: 3
|
I am not sure if i do, you tell me lol, heres my code.
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub FlowLayoutPanel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles FlowLayoutPanel1.Paint End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenButton.Click If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then PictureBox1.Load(OpenFileDialog1.FileName) End If End Sub Private Sub PreviousButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles previous_button.Click End Sub Private Sub NextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles next_button.Click End Sub Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk End Sub Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click End Sub End Class |
|
|
|
|
|
#4 |
|
Member (10 bit)
Join Date: Jan 2010
Location: N. Calif.
Posts: 529
|
The code you have there will load a single picture and display it. It's more involved to be able to select multiple pictures and cycle through them with Previous and Next buttons.
Here something I quickly threw together that might help you get started: Code:
Public Class ViewerForm
Public files() As String ' array to hold list of selected files
Public fileIndex As Integer 'index to which picture is to be displayed
Private Sub btnSelectPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectPicture.Click
Dim fcount As Integer 'count of selected files
' display dialog for user to select files
If ofdSelectPicture.ShowDialog = DialogResult.OK Then
' files contains list of the files the user selected to view
files = ofdSelectPicture.FileNames
'check to see if more than 1 file selected, if so, make Prev and Next buttons visible
'then show first picture
fcount = files.Length
If fcount > 1 Then
btnNext.Visible = True
btnPrev.Visible = True
fileIndex = 0
ShowPic(fileIndex)
End If
End If
End Sub
Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
' Close the window and exit the application
Me.Close()
End Sub
' this sub is called by either Next or Prev buttons to show the next/previous picture
Private Sub ShowPic(ByVal FileIndex)
Dim file As String
file = files(FileIndex)
Me.Text = "Picture Viewer(" & file & ")"
picShowPicture.Image = Image.FromFile(files(FileIndex))
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
'increment fileIndex to poin to the next picture
fileIndex = fileIndex + 1
If fileIndex >= files.Length Then
fileIndex = 0
End If
'show the picture
ShowPic(fileIndex)
End Sub
Private Sub btnPrev_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrev.Click
'decrement fileIndex to point to the previous picture
fileIndex = fileIndex - 1
If fileIndex < 0 Then
fileIndex = files.Length - 1
End If
'show the picture
ShowPic(fileIndex)
End Sub
In the properties for the ofdSelectPicture OpenFileDialog you will need to change the Multiselect property to True and you will need to set the Filter Property to this: "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*" (leave off the quotes) Hope this will get you started on the road to be able to program in VB. Last edited by strollin; 05-13-2011 at 07:03 PM. |
|
|
|
|
|
#5 |
|
Member (2 bit)
Join Date: May 2011
Posts: 3
|
thanks, I was looking over the code you wrote and it means nothing to me lol. I think i need to go to school for this so that i can be taught instead of teaching myself, I need to learn how to understand code and how to write it before i can do anything, i new it would be hard and take some time but i thought it would be a bit easier than what I'm seeing. although when you where telling me to create and name buttons a certain way and changing properties of the openfiledialog i actually new how to do all that. See i can understand programs and how to use them just not the flippin code, blah i know alot about computer but dang this is complicated. anywho, thanks for your help
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|