Results 1 to 8 of 8

Thread: Picture Box double click runs event twice...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    98

    Picture Box double click runs event twice...

    I am creating aplication that displays images in many different ways using Picture Boxes. Single page with tabs, double page using fading in/out effect,
    double page 'page flip' effect and single page auto scroll effect which I have problem with. I am dinamicaly creating picture boxes, loading images, placing
    'path' to a file in tag and adding handler to each picture box. All fine...
    Code:
    Private Sub CoverPageContemp()          
                 For j As Integer = 1 To numControls  
                    'MAIN PICTURE VIEW AREA
                    picViewer = New PictureBox
                    picViewer.Height = PIC_HEIGHT             'Assign height 
                    picViewer.Width = PIC_WIDTH                '& width values to picBox
                    picViewer.Name = "PicView" & j.ToString             'Name the current picBox -  PicView0
                    picViewer.Location = ptLbl
                    picViewer.BorderStyle = BorderStyle.FixedSingle
                    TabControl2.SelectedTab.Controls.Add(picViewer)     'add control
                    ptLbl.Y += PIC_HEIGHT + 5    'increase ptlbl.Y to place next picturebox to the bottom
    
                    index = DataGridView3.SelectedRows.Item(j - 1).Index
                    Dim chedk As Integer
                    Dim NoImage As String
                    Dim level As String
    
    
                 
                    CoverPage = 1
                    Dim myimage As Image = picViewer.Image
                    Dim g As Graphics = Graphics.FromImage(myimage)
                    Dim mainFont As Font
                    Dim textStyle As New FontStyle
                    mainFont = New Font("Pristina", 32, FontStyle.Bold) 'Times New Roman
                    Dim textArea As Rectangle = New Rectangle(20, 250, 400, 100)
                    Dim textFormat As StringFormat = New StringFormat
                    textFormat.LineAlignment = StringAlignment.Center
                    textFormat.Alignment = StringAlignment.Center
                    Dim text As String = DataGridView3.Rows(index).Cells(2).Value 
                    g.DrawString(text, mainFont, Brushes.Black, textArea, textFormat)
    
                    'COMPOSER NAME
    
                    Dim mainFont1 As Font
                    Dim textStyle1 As New FontStyle
                    mainFont1 = New Font("Monotype Corsiva", 48, FontStyle.Italic)
                    Dim textArea1 As Rectangle = New Rectangle(0, 120, 440, 100)
                    Dim textFormat1 As StringFormat = New StringFormat
                    textFormat1.LineAlignment = StringAlignment.Center
                    textFormat1.Alignment = StringAlignment.Center
                    Dim text1 As String = DataGridView3.Rows(index).Cells(1).Value 
                    g.DrawString(text1, mainFont1, Brushes.Red, textArea1, textFormat1)
                    picViewer.SizeMode = PictureBoxSizeMode.StretchImage
    
                    Dim dgv3path As String = DataGridView3.Rows(index).Cells(0).Value
                    picViewer.Tag = dgv3path
    
                    picViewer.Cursor = Cursors.Hand
                    ToolTip1.SetToolTip(picViewer, "Double_Click to View Sheet Music.")
                    ToolTip1.ShowAlways = True
                    ToolTip1.Active = True
    
                    AddHandler picViewer.DoubleClick, AddressOf picViewer_Clicked   'Adding handler to each picture box
    
                    g.Dispose()
                Next
            
            SplitContainer1.Visible = True
            SplitContainer1.BringToFront()
            SplitContainer1.Dock = DockStyle.Fill
    
    
        End Sub
    When double clicking on the P.Box image is loaded in first P.Box .
    Code:
    Private Sub picViewer_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)
           
            If BookFormToolStripMenuItem1.CheckState = CheckState.Checked Then
                pathload = (DirectCast(sender, PictureBox).Tag)
                If currentpath = pathload Then
                    Exit Sub
                End If
                currpage12 = 0
                currpage22 = 0
                totalPages1 = 0
                currentpath = pathload
                Call doublebookstyle()
    
           
    
            ElseIf AutoScrollToolStripMenuItem.CheckState = CheckState.Checked Then
                pathload = (DirectCast(sender, PictureBox).Tag)
                If currentpath = pathload Then
                    Exit Sub
                End If
    
                currpage12 = 0
                currpage22 = 0
                totalPages1 = 0
                currentpath = pathload
                
                Call AutoScrollOnePage()
                
        End Sub
    Every additional image (frame) from .tif file is automaticly scroll from the top.
    Code:
     Private Sub AutoScrollOnePage()
            If pathload Is Nothing Then
                Exit Sub
            End If
            
            Dim extention As String = pathload
            extention = extention.Substring(extention.LastIndexOf(".") + 1).ToLower
            If extention = "tif" Then
                fs1 = File.OpenRead(pathload)
                srcBmp = CType(Bitmap.FromStream(fs1), Bitmap)
                srcBmp1 = CType(Bitmap.FromStream(fs1), Bitmap)
                totalPages1 = CInt(srcBmp.GetFrameCount(FrameDimension.Page) - 1)
            Else
               
                 Exit Sub
               
            End If
    
            Dim Tpcount As Integer = TabControl3.TabPages.Count
            For i As Integer = Tpcount - 1 To 1 Step -1
                TabControl3.TabPages.RemoveAt(i)
            Next
    
            Dim autopage = New TabPage
            autopage.Name = "TabPg" & 1
            TabControl3.TabPages.Add(autopage)
            autopage.Text = "Auto_Scroll"
            autopage.BackColor = Color.Silver
            autopage.AutoScroll = True
            autopage.BorderStyle = BorderStyle.Fixed3D
            TabControl3.SelectTab(autopage.Name)
            autopage.IsAccessible = True
    
            TestPictureC = New PictureBox
            TestPictureD = New PictureBox
    
            TestPictureC.Height = Image.FromFile(pathload).Height       'Assign height 
            TestPictureC.Width = Image.FromFile(pathload).Width + 10        'Assign Width 
            TestPictureC.SizeMode = PictureBoxSizeMode.Normal
            TestPictureC.BackColor = Color.White
            Dim ptloc As Integer = (TabControl3.Width - TestPictureC.Width) / 2 - 20 '140
            Dim ptLbl As Point = New Point(ptloc, 1)  'the controls position
            TestPictureC.Location = ptLbl
            TestPictureC.BorderStyle = BorderStyle.None
            TabControl3.SelectedTab.Controls.Add(TestPictureC) 'add control
    
            TestPictureD.Height = Image.FromFile(pathload).Height       'Assign height 
            TestPictureD.Width = Image.FromFile(pathload).Width    '760      'Assign Width 
            TestPictureD.SizeMode = PictureBoxSizeMode.CenterImage
            TestPictureD.BackColor = Color.White
            TestPictureD.Location = TestPictureC.Location
            TestPictureD.BorderStyle = BorderStyle.FixedSingle
            TabControl3.SelectedTab.Controls.Add(TestPictureD)
            
            
           
            For j As Integer = 0 To totalPages1 - 1
    
                currentPage1 = j
    
                srcBmp1 = CType(Bitmap.FromStream(fs1), Bitmap)
                srcBmp1.SelectActiveFrame(FrameDimension.Page, currentPage1)
                TestPictureD.Image = srcBmp1
    
                Dim currj1 As Integer = (totalPages1 + 1) - (totalPages1 - j)
                If currj1 > totalPages1 Then
                    Exit Sub
                End If
    
                srcBmp.SelectActiveFrame(FrameDimension.Page, currj1)
    
                Tpic = 0
    
                For i As Integer = 0 To TestPictureC.Height - 1 Step 1
                    AddHandler TestPictureD.Paint, AddressOf TestPicture0_paint
    
                    TestPictureC.Height = i
                    TestPictureC.Image = srcBmp
                    TestPictureC.BringToFront()
                    TestPictureD.Refresh()
                    TestPictureC.Refresh()
                    RemoveHandler TestPictureD.Paint, AddressOf TestPicture0_paint
    
                    Application.DoEvents()
    
    
                 Next
    
            Next
            
            RemoveHandler TestPictureD.Paint, AddressOf TestPicture0_paint
    
        End Sub
    I am also using Paint Event of a PictureBox to display gradiant color to imitate shadow in front of the scrolling image.
    Code:
     Private Sub TestPicture0_paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)
            If AutoScrollToolStripMenuItem.CheckState = CheckState.Checked Then
    
                Tpic += 1
                Dim rect As Rectangle = New Rectangle(0, Tpic - 2, TestPictureC.Width, 20)
                Dim lgb As New System.Drawing.Drawing2D.LinearGradientBrush(rect, Color.Gray, Color.Transparent, Drawing2D.LinearGradientMode.Vertical)
                e.Graphics.FillRectangle(lgb, rect)
                e.Dispose()
    
            End If
        End Sub
    The problem is: if I let the event finish displaying all pages(frames) then there is no problem with double clicking different P.Box and run different file.
    However, if I double click a different P.Box when first event is still runing then
    file gets open run as it should but when finish, it starts displaying the file again ?
    Sorry for posting so much code, but I wanted you to check what am I doing wrong. Thanks for any help..( I had to shorten the first code_to many characters in the post..)

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Picture Box double click runs event twice...

    You know you're using the Picture Box's Click event and not DoubleClick event, right?

    Well, it would appear that way at least. I don't actually see an event handler there, but the _Click is visible =/
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Picture Box double click runs event twice...

    Private Sub picViewer_Clicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles picViewer.DoubleClicked

  4. #4

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    98

    Re: Picture Box double click runs event twice...

    Please explain, by adding handler : AddHandler picViewer.DoubleClick AddressOf picViewer_Clicked ' I have to double click the Picture Box to load a file ?

    Code:
     picViewer.Cursor = Cursors.Hand
                    ToolTip1.SetToolTip(picViewer, "Double_Click to View Sheet Music.")
                    ToolTip1.ShowAlways = True
                    ToolTip1.Active = True
    
                    AddHandler picViewer.DoubleClick, AddressOf picViewer_Clicked   'Adding handler to each picture box
    
                    g.Dispose()
                Next
            
            SplitContainer1.Visible = True
            SplitContainer1.BringToFront()
            SplitContainer1.Dock = DockStyle.Fill
    AirlineSim....
    Private Sub picViewer_Clicked(ByVal sender As Object, ByVal e As System.EventArgs) Handles picViewer.DoubleClicked '

    I am getting error: ' Handles clause requires a WithEvents variable.....

  5. #5
    Fanatic Member
    Join Date
    Dec 2009
    Location
    Milan
    Posts
    810

    Re: Picture Box double click runs event twice...

    Sorry I missed that part of the code, is it because you have clicked and double click as a handler? remove one and test it.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    98

    Re: Picture Box double click runs event twice...

    Back to square one...

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    98

    Re: Picture Box double click runs event twice...

    I still have not found solution to this problem. What I have observed is that the last image from previous double_click does not get dispose and is redrown again. I have created Boolean and placed it in Button Click event.
    Code:
     Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click
    
            test11 = True
           
        End Sub
    Then I have palaced 'test11' inside 'If' statement:
    Code:
    For j As Integer = 0 To totalPages1 - 1
    
                currentPage1 = j
    
                srcBmp1 = CType(Bitmap.FromStream(fs1), Bitmap)
                srcBmp1.SelectActiveFrame(FrameDimension.Page, currentPage1)
                TestPictureD.Image = srcBmp1
    
                Dim currj1 As Integer = (totalPages1 + 1) - (totalPages1 - j)
                If currj1 > totalPages1 Then
                    Exit Sub
                End If
                srcBmp.SelectActiveFrame(FrameDimension.Page, currj1)
    
                Tpic = 0
    
                For i As Integer = 0 To TestPictureC.Height - 1 Step 1
    
                    AddHandler TestPictureD.Paint, AddressOf TestPicture0_paint
    
                    TestPictureC.Height = i
                    TestPictureC.Image = srcBmp
                    TestPictureC.BringToFront()
                    TestPictureD.Refresh()
                    TestPictureC.Refresh()
    
                    RemoveHandler TestPictureD.Paint, AddressOf TestPicture0_paint
                    Application.DoEvents()
    
                    If test11 = True Then
    
    
                        test11 = False
                        MsgBox("00000 " & test11)
                        Exit Sub
    
                    End If
    
                Next
    
            Next
    Now, if I click the ToolStripButton7 and set the 'test11' to true and than double_click on different Picture Box then
    all works as it should. I want to be able to do this in 'Private Sub picViewer_Clicked()' when double_clicking different P.Box. I have tryed using 'ToolStripButton7.PerformClick()' but it dosen't work. So it seem that some how I have to dispose, the last double_click or something before 'Private Sub AutoScrollOnePage()' get call again. Please help, I have been pulling my hair for couple days now....
    Last edited by adamarek; Jul 10th, 2010 at 04:19 PM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    98

    Re: Picture Box double click runs event twice...

    I am willing to try any suggestion, any one ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width