Results 1 to 13 of 13

Thread: [RESOLVED]Change generated PictureBox locaton on mouse click

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    22

    Question [RESOLVED]Change generated PictureBox locaton on mouse click

    Hello,
    I'm trying to change the location of a generated PictureBox using code.
    It work's fine using mouse:
    Code:
     Private Sub myPictureBox_MouseDown(sender As Object, e As MouseEventArgs)
    
    
            Dim PB As PictureBox = CType(sender, PictureBox)
    
            Select Case PB.Name.ToString
                Case "PictureBox" + TextBox7.Text
                    X = Control.MousePosition.X - PB.Location.X
                    Y = Control.MousePosition.Y - PB.Location.Y
                    TextBox3.Text = PB.Width
                    TextBox2.Text = PB.Height
                  
            End Select
    
        End Sub
    but whenever I try to use a button I get this error:
    Name:  8srt.jpg
Views: 1339
Size:  226.5 KB

    I know it may be easy for You to to fix it, but I don't know how to this
    Last edited by kubau2; Oct 7th, 2013 at 08:40 AM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Change generated PictureBox locaton on mouse click

    sender is the object which triggered the code, so in the first case it is the picturebox, and in the other it is the button.

    If you want to always use a specific picturebox you can change the Dim line as follows:
    Code:
            Dim PB As PictureBox = myPictureBox  'set this to the name of the control you want
    (or various alternative methods).

    If you want to somehow use different pictureboxes, you need to decide how to determine which one to use.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    22

    Re: Change generated PictureBox locaton on mouse click

    Quote Originally Posted by si_the_geek View Post
    sender is the object which triggered the code, so in the first case it is the picturebox, and in the other it is the button.

    If you want to always use a specific picturebox you can change the Dim line as follows:
    Code:
            Dim PB As PictureBox = myPictureBox  'set this to the name of the control you want
    (or various alternative methods).

    If you want to somehow use different pictureboxes, you need to decide how to determine which one to use.


    I've given them names while creating, so I can refer to them, but only when the sender is a picturebox (and that's the only way I know how to refer to them-> I need to use this 2nd code) Is there some way to convert/spoof button to a picturebox?

    Here's the code how I create new picturebox, maybe this will help a bit:

    Code:
    
            Dim MyPictureBox As New PictureBox
    
            Try
    
                MyPictureBox.Image = Image.FromFile(TextBox5.Text)
    
            Catch ex As Exception
    
            End Try
            MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
         
            MyPictureBox.Location = New System.Drawing.Point(0, 0)
            MyPictureBox.Size = New System.Drawing.Size(Image_Aligner.TextBox3.Text / 2, Image_Aligner.TextBox4.Text / 2)
            MyPictureBox.BorderStyle = BorderStyle.Fixed3D
            MyPictureBox.Name = "PictureBox" & TextBox7.Text   ' <!-- Each picturebox has it's name, that's how I can select between them 
            MyPictureBox.BackColor = Color.Transparent
            MyPictureBox.BorderStyle = BorderStyle.None
            AddHandler MyPictureBox.Click, AddressOf MyPictureBox_Click
            AddHandler MyPictureBox.MouseMove, AddressOf MyPictureBox_MouseMove
            AddHandler MyPictureBox.MouseDown, AddressOf myPictureBox_MouseDown
             Panel1.Controls.Add(MyPictureBox)
                  
            Timer3.Enabled = False
    Last edited by kubau2; Oct 6th, 2013 at 02:12 PM.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Change generated PictureBox locaton on mouse click

    When you press the button, which picturebox do you want it to work with?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    22

    Re: Change generated PictureBox locaton on mouse click

    Quote Originally Posted by si_the_geek View Post
    When you press the button, which picturebox do you want it to work with?
    I want each of them to work using one code. So for example normally I should write like this:

    Code:
      Dim PB As PictureBox = CType(sender, PictureBox)
    
            Select Case PB.Name.ToString
                Case "PictureBox0"  ' <-- That's how picturebox is selected, in this case it would always be number 0
                   'Here's the command
            Case "PictureBox1"  ' <-- In this case it would always be number 1
                   'Here's the command
                  
            End Select
    So I've assigned each picturebox added unique id using this code:

    Code:
     Select Case PB.Name.ToString
                Case "PictureBox" + TextBox7.Text  ' <-- That's how pictureboxes are selected. The id number is given in the textbox7
                  'Here's my command
                  
            End Select
    And to make "PB" recognizable for VB, I have to define those two lines:

    Code:
     Dim PB As PictureBox = CType(sender, PictureBox)  ' <--And here's the problem, as it is under a Button1_Click, not under  "myPictureBox" Sub. 
     Select Case PB.Name.ToString
    That's what I need to change, but how? I don't exactly understand what CType does, how it works and most importantly, how to use it for my purpose. I always learn from already working code, that's why I would need it first time to run.
    Last edited by kubau2; Oct 6th, 2013 at 02:35 PM.

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Change generated PictureBox locaton on mouse click

    CType converts an object to a particular data type, but can only do so if the data types are compatible. So in the case of this:
    Code:
      Dim PB As PictureBox = CType(sender, PictureBox)
    ...it converts sender from whatever data type it currently is to a PictureBox. In your case it wont work, because sender is a button (which is not compatible for conversion to a picturebox).

    Quote Originally Posted by kubau2 View Post
    I want each of them to work using one code.
    That isn't particularly clear... if you mean you want it to work with all of them at the 'same' time, you can do this:
    Code:
        For Each Ctl As Control In Me.Controls
          If Ctl.Name Like "PictureBox*" Then
             Dim PB As PictureBox = CType(Ctl, PictureBox)
    
            'your Select Case etc here
    
          End If
        Next

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    22

    Re: Change generated PictureBox locaton on mouse click

    Quote Originally Posted by si_the_geek View Post
    CType converts an object to a particular data type, but can only do so if the data types are compatible. So in the case of this:
    Code:
      Dim PB As PictureBox = CType(sender, PictureBox)
    ...it converts sender from whatever data type it currently is to a PictureBox. In your case it wont work, because sender is a button (which is not compatible for conversion to a picturebox).

    That isn't particularly clear... if you mean you want it to work with all of them at the 'same' time, you can do this:
    Code:
        For Each Ctl As Control In Me.Controls
          If Ctl.Name Like "PictureBox*" Then
             Dim PB As PictureBox = CType(Ctl, PictureBox)
    
            'your Select Case etc here
    
          End If
        Next

    Thanks for the code. I'll try today afternoon.
    A bit more explanation : all the pictureboxes should work with one code, but not at the same time. Only one of them should work at the selected time, and selected picturebox id is given in the textbox7.

    Else I would use this code
    For each item as string in listbox2.items
    Here what to do
    Next

    Case "picturebox" + textbox7.text defines only one picturebox at a time

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Change generated PictureBox locaton on mouse click

    In that case you don't need the Select Case, just change the If statement in my example to this:
    Code:
          If Ctl.Name = "picturebox" + textbox7.text Then
    (note that for joining strings you should be using & instead of + )


    There are other ways this could be done (including without a loop), doing it this way means that if the picturebox can't be found for some reason (like you haven't created it yet) then there will be no error.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    22

    Re: Change generated PictureBox locaton on mouse click

    Quote Originally Posted by si_the_geek View Post
    In that case you don't need the Select Case, just change the If statement in my example to this:
    Code:
          If Ctl.Name = "picturebox" + textbox7.text Then
    (note that for joining strings you should be using & instead of + )


    There are other ways this could be done (including without a loop), doing it this way means that if the picturebox can't be found for some reason (like you haven't created it yet) then there will be no error.
    Thank You very much for Your help, finally it works as I wanted


    Code:
    For Each Ctl As Control In Me.Controls
    This command is new for me, I didn't know we can refer to objects using control

    Always connected everything with the "+" sign, rarely with "&" and it worked fine anyway. Does it make any difference in program later?
    And of course to prevent errors I use "try"

    Once again, thank You very much for You time and interest for this thread. Now I can continue my project.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Change generated PictureBox locaton on mouse click

    Quote Originally Posted by kubau2 View Post
    This command is new for me, I didn't know we can refer to objects using control
    Most data types are based (to some degree) on other data types, adding extra functionality.

    The basic data type is Object, and one of the things based on that is Control (which adds properties for location and size etc), and one of the things based on that is Picturebox (which adds properties for picture etc).

    It is safe to use a 'lower' data type (such as Control when it is a Picturebox), but it isn't always wise (because you lose assistance from the VB editor, and need to use CType to get it to the correct data type). In cases like this, it is useful.

    Always connected everything with the "+" sign, rarely with "&" and it worked fine anyway. Does it make any difference in program later?
    + is meant for adding numbers together, and & is meant for joining strings.

    I'm not sure how recent versions of VB deal with it, but in older versions of VB using + to join strings would cause bugs in some circumstances (eg: joining "3" and "1" would sometimes produce "31", but at other times "4").

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2011
    Posts
    22

    Re: Change generated PictureBox locaton on mouse click

    Quote Originally Posted by si_the_geek View Post
    I'm not sure how recent versions of VB deal with it, but in older versions of VB using + to join strings would cause bugs in some circumstances (eg: joining "3" and "1" would sometimes produce "31", but at other times "4").

    Just tested. & and + do the same thing with text. They connect eg. "12" and "14" to "1214" (every time).
    I've started with VB 2008 and it has always been like that, no problems with this.

    The difference is only when converting to value:
    Code:
    TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
    TextBox3.Text = Val(TextBox1.Text) & Val(TextBox2.Text)
    & gives "1214"
    + gives "26"

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: [RESOLVED]Change generated PictureBox locaton on mouse click

    If you use & to join strings you are guaranteed to always join strings together (so always "1214"), but with + it depends on the data types of the items... the help for + gives more info about the circumstances (basically it changes when one of them is a String and the other is a numeric data type).

  13. #13
    New Member
    Join Date
    Mar 2013
    Posts
    8

    Re: [RESOLVED]Change generated PictureBox locaton on mouse click

    Move Controls .Net
    This is a demo project on how to move controls on windows form . I have introduced it using vb.net . To move controls on windows form it requires to deal with x-coordinate and y-coordinate of that controls . X-cordinate tell the control distance from x-axis origin point and y-coordinate tells the distance from y-axis origin.

    MOVE Controls on windows form .NET
    So to move the control on .NET we will increment the x-coordinate and y-coordinate of that control according to how much distance we want to move the controls and also according to which direction you want to move the control

    Design :-

    Name:  move+control.PNG
Views: 1050
Size:  17.7 KB

    Code:
    Code For Move Controls .NET Project
    
    Public Class Form1
    
    //For "->" to move Right
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            TextBox1.Text = Val(TextBox1.Text) + 10
            Dim x_intercept = Val(TextBox1.Text)
    
            If ComboBox1.SelectedIndex = 0 Then
                If Not TextBox1.Text = "" Then
                    PictureBox1.Location = New Point(x_intercept)
                End If
            End If
            If ComboBox1.SelectedIndex = 1 Then
                If Not TextBox1.Text = "" Then
                    Label4.Location = New Point(x_intercept)
                End If
            End If
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = PictureBox1.Location.X
            TextBox2.Text = PictureBox1.Location.Y
        End Sub
    
    //For "Up" to move Up
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            TextBox2.Text = Val(TextBox2.Text) + 10
            Dim y_intercept = Val(TextBox2.Text)
            Dim x_intercept = Val(TextBox1.Text)
    
            If ComboBox1.SelectedIndex = 0 Then
                If Not TextBox2.Text = "" Then
                    PictureBox1.Location = New Point(x_intercept, y_intercept)
                End If
            End If
            If ComboBox1.SelectedIndex = 1 Then
                If Not TextBox2.Text = "" Then
                    Label4.Location = New Point(x_intercept, y_intercept)
                End If
            End If
        End Sub
    
    //For "<-" to move Towards left
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            TextBox1.Text = Val(TextBox1.Text) - 10
            Dim x_intercept = Val(TextBox1.Text)
    
    
            If ComboBox1.SelectedIndex = 0 Then
                If Not TextBox1.Text = "" Then
                    PictureBox1.Location = New Point(x_intercept)
                End If
            End If
            If ComboBox1.SelectedIndex = 1 Then
                If Not TextBox1.Text = "" Then
                    Label4.Location = New Point(x_intercept)
                End If
            End If
        End Sub
    
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            TextBox2.Text = Val(TextBox2.Text) - 10
            Dim y_intercept = Val(TextBox2.Text)
            Dim x_intercept = Val(TextBox1.Text)
    
            If ComboBox1.SelectedIndex = 0 Then
                If Not TextBox2.Text = "" Then
                    PictureBox1.Location = New Point(x_intercept, y_intercept)
                End If
            End If
            If ComboBox1.SelectedIndex = 1 Then
                If Not TextBox2.Text = "" Then
                    Label4.Location = New Point(x_intercept, y_intercept)
                End If
            End If
        End Sub
    
    //For "Down" to move Down
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            If ComboBox1.SelectedIndex = 0 Then
                Label4.Visible = False
                PictureBox1.Visible = True
            End If
            If ComboBox1.SelectedIndex = 1 Then
                Label4.Visible = True
                PictureBox1.Visible = False
            End If
        End Sub
    End Class

Tags for this Thread

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