5 Replies - 1762 Views - Last Post: 25 November 2012 - 09:27 AM Rate Topic: -----

#1 apateonas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 12

User Control and Form

Posted 24 November 2012 - 10:04 AM

Hallo, i'm trying to fix a siple kiosk for my store. In my project i have a UserControl for thumbnails and a form for full preview etc. In user control there is a checkbox, how i programme it so when a checkbox state is checked from a button to the form the checked files to be copied in a folder ?

Thank you in advance

UserControl code
Public Class Thumbnail
    Private strFileName As String = ""
    Private Sub DrawThumbNail()

        Dim dAspectRatio As Double
        Dim bm As Bitmap
        Dim iTNHeight As Integer, iTNWidth As Integer

        Try
            ' Display the filename on the label
            lblFileName.Text = IO.Path.GetFileName(strFileName)

            ' Check that the variable strFileName contains something
            If strFileName <> "" Then

                ' Load the photo into a bitmap
                bm = New Bitmap(strFileName)

                ' Calculate the aspect ration of the image
                dAspectRatio = bm.Width / bm.Height

                ' Calculate the width and height of the thumbnail to be created
                If dAspectRatio > 1 Then
                    ' If the image is wider than it is tall, set it's width to be the width of the
                    ' picturebox and calculate it's height using the aspect ratio
                    iTNWidth = picThumbnail.Width
                    iTNHeight = CInt(iTNWidth / dAspectRatio)
                Else
                    ' If the image is taller than it is wide, set it's height to be the height of the
                    ' picturebox and calculate it's width using the aspect ratio
                    iTNHeight = picThumbnail.Height
                    iTNWidth = CInt(iTNHeight * dAspectRatio)
                End If

                ' Check we have valid heights and widths
                If iTNHeight > 0 And iTNWidth > 0 Then
                    ' Generate the thumbnail
                    picThumbnail.Image = bm.GetThumbnailImage(iTNWidth, iTNHeight, Nothing, IntPtr.Zero)
                End If

                ' Dispose of the bitmap 
                bm.Dispose()
            Else
                ' Tf strFileName is empty, clear the picturebox
                picThumbnail.Image = Nothing
            End If

        Catch ex As Exception
            MsgBox("An exception occured when trying to render the thumbnail for the image '" & _
              strFileName & "' with the message:" & vbCrLf & vbCrLf & ex.ToString, _
              MsgBoxStyle.Exclamation)
        End Try

    End Sub
    Public Property FileName() As String
        Get
            Return strFileName
        End Get
        Set(ByVal Value As String)
            strFileName = Value
            DrawThumbnail()
        End Set
    End Property
    Private Sub Thumbnail_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        DrawThumbnail()
    End Sub
    Public Event ThumbnailMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    Public Event ThumbnailDoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
    Public Event ThumbnailClick(ByVal sender As Object, ByVal e As System.EventArgs)
    Private Sub Control_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblFilename.Click, picThumbnail.Click
        RaiseEvent ThumbnailClick(Me, e)
    End Sub

    Private Sub Control_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblFilename.DoubleClick, picThumbnail.DoubleClick
        RaiseEvent ThumbnailDoubleClick(Me, e)
    End Sub
    Private Sub picThumbnail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picThumbnail.Click

    End Sub
    Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lblFilename.MouseDown, Panel1.MouseDown
        Dim eNew As System.Windows.Forms.MouseEventArgs
        ' Convert the sender to a Control so we can access its position
        Dim c As Control = DirectCast(sender, Control)
        'Create the new MouseEventArgs
        eNew = New System.Windows.Forms.MouseEventArgs(e.Button, e.Clicks, e.X + c.Top, e.Y + c.Left, e.Delta)
        RaiseEvent ThumbnailMouseDown(Me, eNew)
    End Sub
    Public Sub New(ByVal strFN As String)
        MyBase.New()
        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'Initialize our thumbnail control
        strFileName = strFN ' assign the filename for the thumbnail
        DrawThumbnail() ' draw it
    End Sub

    Private Sub picCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picCheck.CheckedChanged
        If picCheck.CheckState = CheckState.Checked Then
            Me.Select
        End If
    End Sub
End Class



Form Code
Imports System.IO
Imports System.Drawing

Public Class Form2
    ' Constants to define the size and spacing of our thumbnails
    Const iTNWidth As Integer = 120
    Const iTNHeight As Integer = 120
    Const iHPadding As Integer = 5
    Const iVPadding As Integer = 5
    ' Sets the initial zoom factor to 100%
    Dim dZoomFactor As Double = 1
    ' Specifies that the zoom factor will be changed in increments of 15%
    Const dZoomIncrements As Double = 0.15
Private Sub SetThumbnailPosition(ByRef tnThumbnail As Thumbnail, ByRef cntlThumbnailContainer As ScrollableControl, ByVal iThumbNailNo As Integer)

    Dim iColumns As Integer, iRow As Integer, iCol As Integer
    Dim iLeft As Integer, iTop As Integer

    ' Calculate the number of thumbnails we can fit across the control
    iColumns = (cntlThumbnailContainer.Width - 30) \ (iTNWidth + iHPadding)
    ' If the number if less then 1, set it to 1 so the thumbnails will always be displayed
    If iColumns < 1 Then iColumns = 1

    ' Calculate the position of our thumbnail in our control in terms of the
    ' row and column of grid of displayed thumbnails
    iRow = (iThumbNailNo \ iColumns) + 1
    iCol = (iThumbNailNo Mod iColumns) + 1

    ' Now calculate the position in Pixels.  We use the defined constants to calculate the 
    ' position of the thumbnail and add the AutoScrollPosition of the control we are
    ' going to be arranging it on - this is necessary to ensure that the thumbnails are
    ' displayed in the correct location if the control is not currently scrolled to the top
    ' of its available area.
    iLeft = (iCol * iHPadding) + ((iCol - 1) * iTNWidth)
    iLeft += cntlThumbnailContainer.AutoScrollPosition.X()
    iTop = (iRow * iVPadding) + ((iRow - 1) * iTNWidth)
    iTop += cntlThumbnailContainer.AutoScrollPosition.Y()

    ' Set the thumbnails position
    tnThumbnail.Left = iLeft
    tnThumbnail.Top = iTop
    End Sub
    Private Sub OnThumbnailDoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim tnThumbnail As Thumbnail
        If sender.GetType Is GetType(Thumbnail) Then
            tnThumbnail = DirectCast(sender, Thumbnail)
            picPhoto.Image = Bitmap.FromFile(tnThumbnail.FileName)
            FitPhotoToPanel()
            ReDrawPhoto(0, 0)
        End If
    End Sub
    Public Function AddThumbnail(ByRef cntlThumbnailContainer As ScrollableControl, ByVal strFileName As String) As Thumbnail

        Dim tnNewThumbnail As Thumbnail

        ' Create a new thumbnail
        tnNewThumbnail = New Thumbnail(strFileName)
        ' Assign its height, width and make sure it is visible
        With tnNewThumbnail
            If .Width <> iTNWidth Then .Width = iTNWidth
            If .Height <> iTNHeight Then .Height = iTNHeight
            .Visible = True
        End With
        ' Add the thumbnail to the specified control
        cntlThumbnailContainer.Controls.Add(tnNewThumbnail)
        ' Set up the event handler
        AddHandler tnNewThumbnail.ThumbnailDoubleClick, AddressOf OnThumbnailDoubleClick

        ' Return the thumbnail we just created
        Return tnNewThumbnail

    End Function
    Public Sub ClearThumbnails(ByRef cntlThumbnailContainer As ScrollableControl)

        Dim i As Integer = 0
        Dim c As Control

        While i < cntlThumbnailContainer.Controls.Count
            c = cntlThumbnailContainer.Controls(i)
            If c.GetType Is GetType(Thumbnail) Then
                cntlThumbnailContainer.Controls.Remove(c) ' Remove the current thumbnail
                c.Dispose() ' Dispose of it
            Else
                i += 1
            End If
        End While
    End Sub
    Private Sub GenerateThumbnailsForFolder(ByRef cntlThumbnailContainer As ScrollableControl)

        Dim dlg As FolderBrowserDialog
        Dim iThumbnailCount As Integer = 0
        Dim tnNewThumbnail As Thumbnail

        ' Create a new instance of the folder browser dialog
        dlg = New FolderBrowserDialog

        ' Show the dialog and check that the user pressed OK to close it - not cancel
        If dlg.ShowDialog = DialogResult.OK Then

            Me.Cursor = Cursors.WaitCursor ' Set the Forms cursor to a hour glass

            ' Clear all thumbnails currently on the control
            ClearThumbnails(cntlThumbnailContainer)

            ' Get all the JPEG files in the specified folder and loop through them
            For Each objFile As FileInfo In New DirectoryInfo(dlg.SelectedPath).GetFiles("*.jpg")
                ' Create a new thumbnail to display the current file
                tnNewThumbnail = AddThumbnail(cntlThumbnailContainer, objFile.FullName)
                ' Arrange the thumbnail
                SetThumbnailPosition(tnNewThumbnail, cntlThumbnailContainer, iThumbnailCount)
                iThumbnailCount += 1  ' Increment the count of the thumbnails on the control
            Next

            ' Set the title of the application to reflect the folder name being browsed
            Me.Text = Application.ProductName & " - " & dlg.SelectedPath
            Me.Cursor = Cursors.Arrow ' Return the cursor to an arrow
        End If
        ' Dispose of the dialog
        dlg.Dispose()

    End Sub
    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileBrowse.Click
        GenerateThumbnailsForFolder(DirectCast(pnlThumbnails, ScrollableControl))
    End Sub
    Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
        Me.Close()
    End Sub
    Private Sub ReDrawPhoto(ByVal xPos As Integer, ByVal yPos As Integer)

        Dim dPicStaticXPos, dPicStaticYPos As Double
        Dim dPanelStaticXPos, dPanelStaticYPos As Double
        Dim iNewXPos, iNewYPos As Integer

        If Not picPhoto.Image Is Nothing Then
            ' Get the position on the picture in pixels of
            ' the cordinates specified in the function, we will use this to
            ' ensure these cordinates stay in the same position each time we redraw the photo
            dPicStaticXPos = xPos / (picPhoto.Width / picPhoto.Image.Width)
            dPicStaticYPos = yPos / (picPhoto.Height / picPhoto.Image.Height)
            dPanelStaticXPos = xPos + pnlPhoto.AutoScrollPosition.X
            dPanelStaticYPos = yPos + pnlPhoto.AutoScrollPosition.Y

            ' Set the new image height and width using the zoom factor
            picPhoto.Height = CInt(picPhoto.Image.Height * dZoomFactor)
            picPhoto.Width = CInt(picPhoto.Image.Width * dZoomFactor)
            picPhoto.Refresh()

            ' Set the AutoScrollPosition so that the point clicked on is in the same position on the screen after zooming 
            iNewXPos = CInt((dPicStaticXPos * dZoomFactor) - dPanelStaticXPos)
            iNewYPos = CInt((dPicStaticYPos * dZoomFactor) - dPanelStaticYPos)
            pnlPhoto.AutoScrollPosition = New Drawing.Point(iNewXPos, iNewYPos)
        End If

    End Sub
    Private Sub FitPhotoToPanel()

        ' Assumes that the Panel is approximately square
        ' Calculates the maximum zoom factor that can be used and still
        ' fit the image on the panel used to contain it
        If picPhoto.Image.Height > picPhoto.Image.Width Then
            dZoomFactor = (pnlPhoto.Height - 5) / picPhoto.Image.Height
        Else
            dZoomFactor = (pnlPhoto.Width - 5) / picPhoto.Image.Width
        End If

    End Sub
    Private Sub picPhoto_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
        Handles picPhoto.MouseWheel

        If e.Delta > 0 Then
            dZoomFactor *= (e.Delta / 120) * (1 + dZoomIncrements)
        ElseIf e.Delta < 0 Then
            dZoomFactor *= (e.Delta / -120) * (1 - dZoomIncrements)
        End If
        ReDrawPhoto(e.X, e.Y)
    End Sub

    Private Sub picPhoto_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles picPhoto.Click
        picPhoto.Focus()
    End Sub
    Private Sub ImageZoomIn(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles mnuImageZoomIn.Click
        ' Increases the zoom factor and redraws the image.
        ' Passes the cordinates of the middle of the screen into the ReDrawPhoto function so
        ' That the center of the image at present remains in the center of the screen.
        dZoomFactor *= 1 + dZoomIncrements
        ReDrawPhoto(CInt(pnlPhoto.Width / 2) - pnlPhoto.AutoScrollPosition.X, _
            CInt(pnlPhoto.Height / 2) - pnlPhoto.AutoScrollPosition.Y)
    End Sub

    Private Sub ImageZoomOut(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles mnuImageZoomOut.Click
        dZoomFactor *= 1 - dZoomIncrements
        ReDrawPhoto(CInt(pnlPhoto.Width / 2) - pnlPhoto.AutoScrollPosition.X, _
            CInt(pnlPhoto.Height / 2) - pnlPhoto.AutoScrollPosition.Y)
    End Sub

    Private Sub ImageFitToScreen(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles mnuImageFitToScreen.Click
        FitPhotoToPanel()
        ReDrawPhoto(0, 0)
    End Sub
    Private Sub ImageFlipVeritcal(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles mnuImageFlipVertical.Click
        If Not picPhoto.Image Is Nothing Then
            picPhoto.Image.RotateFlip(RotateFlipType.RotateNoneFlipX)
            ReDrawPhoto(CInt(pnlPhoto.Width / 2), CInt(pnlPhoto.Height / 2))
        End If
    End Sub

    Private Sub ImageFlipHorizontal(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles mnuImageFlipHorizontal.Click
        If Not picPhoto.Image Is Nothing Then
            picPhoto.Image.RotateFlip(RotateFlipType.RotateNoneFlipY)
            ReDrawPhoto(CInt(pnlPhoto.Width / 2), CInt(pnlPhoto.Height / 2))
        End If
    End Sub

    Private Sub ImageRotateLeft(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles mnuRotateLeft.Click
        If Not picPhoto.Image Is Nothing Then
            picPhoto.Image.RotateFlip(RotateFlipType.Rotate270FlipNone)
            ReDrawPhoto(CInt(pnlPhoto.Width / 2), CInt(pnlPhoto.Height / 2))
        End If
    End Sub

    Private Sub ImageRotateRight(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles mnuRotateRight.Click
        If Not picPhoto.Image Is Nothing Then
            picPhoto.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
            ReDrawPhoto(CInt(pnlPhoto.Width / 2), CInt(pnlPhoto.Height / 2))
        End If
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub ΑντιγραφήToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ΑντιγραφήToolStripMenuItem.Click

    End Sub

End Class

Posted Image

Is This A Good Question/Topic? 0
  • +

Replies To: User Control and Form

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,759
  • Joined: 02-June 10

Re: User Control and Form

Posted 24 November 2012 - 01:05 PM

Quote

so when a checkbox state is checked from a button to the form the checked files to be copied in a folder

Sorry, but I can't follow that in English.

When a checkbox is either checked or unchecked it will raise an event of .CheckChanged. You can write a handler method for that event just as you would a method to handle a button.click event.
Was This Post Helpful? 0
  • +
  • -

#3 apateonas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 12

Re: User Control and Form

Posted 24 November 2012 - 01:50 PM

Thank you for the reply.

Let me explain you ... checkbox is on Usercontrol and the button is on form 2 ...
I want when i check a file and press the button, the file to copy in a folder i choose ...

If you can see the picture in previous post ...

Thank you
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,759
  • Joined: 02-June 10

Re: User Control and Form

Posted 24 November 2012 - 03:49 PM

IN the future please actually place the image in the post.

Attached Image


As for what you want, I understand. But I'm not writing your code for you. Nobody here is going to just give you finished code when you haven't shown us any effort on your part to try to write your own best attempt.

You really have not described what the actual difficulty is. I thought you were trying to react to the change in checked state.
Do you not know how to copy a file?
System.IO.File.Copy(string source, string destination)

Do you not know how to have two different forms communicate with each other? Until you can better describe the problem I can't figure out what it is you need help with.

From my FAQ list:

How do I?

Q: ...get Form 'A' to make a change or talk to Form 'B'
NOTE: Don't try to access GUI controls across forms. Its wrong. Nobody will hire you if you do this sort of crap. It violates every guideline for 'black box' programming, Separation of Responsibility, loose binding of components, and event driven programming. Read the tutorials and learn to do it right the first time so you don't develop bad habits that you just have to un-learn later.
A:

This post has been edited by tlhIn`toq: 25 November 2012 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 apateonas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 12

Re: User Control and Form

Posted 25 November 2012 - 08:16 AM

First of I didn't ask you to give me finished code, i just asked for help because im trying to build an application for my store cause i don't have money to buy it. If you don't want to help fine but be more polite to others ....
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5475
  • View blog
  • Posts: 11,759
  • Joined: 02-June 10

Re: User Control and Form

Posted 25 November 2012 - 09:27 AM

View PosttlhIn`toq, on 24 November 2012 - 04:49 PM, said:

You really have not described what the actual difficulty is. I thought you were trying to react to the change in checked state.
Do you not know how to copy a file?
System.IO.File.Copy(string source, string destination)

Do you not know how to have two different forms communicate with each other?

Until you can better describe the problem I can't figure out what it is you need help with.
[/list]


I did not insult you or in any way be impolite to you. I asked you to better clarify what your troubles are, and what part of your project specifically you need help coding. We can't point you in the right direction without a better and more targeted understanding of what you are having trouble with.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1