-
Oct 6th, 2013, 12:41 PM
#1
Thread Starter
Junior Member
Last edited by kubau2; Oct 7th, 2013 at 08:40 AM.
-
Oct 6th, 2013, 01:47 PM
#2
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.
-
Oct 6th, 2013, 02:06 PM
#3
Thread Starter
Junior Member
Re: Change generated PictureBox locaton on mouse click
Originally Posted by si_the_geek
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.
-
Oct 6th, 2013, 02:20 PM
#4
Re: Change generated PictureBox locaton on mouse click
When you press the button, which picturebox do you want it to work with?
-
Oct 6th, 2013, 02:31 PM
#5
Thread Starter
Junior Member
Re: Change generated PictureBox locaton on mouse click
Originally Posted by si_the_geek
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.
-
Oct 6th, 2013, 04:24 PM
#6
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).
Originally Posted by kubau2
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
-
Oct 7th, 2013, 12:22 AM
#7
Thread Starter
Junior Member
Re: Change generated PictureBox locaton on mouse click
Originally Posted by si_the_geek
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
-
Oct 7th, 2013, 06:57 AM
#8
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.
-
Oct 7th, 2013, 08:39 AM
#9
Thread Starter
Junior Member
Re: Change generated PictureBox locaton on mouse click
Originally Posted by si_the_geek
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.
-
Oct 7th, 2013, 09:10 AM
#10
Re: Change generated PictureBox locaton on mouse click
Originally Posted by kubau2
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").
-
Oct 7th, 2013, 10:30 AM
#11
Thread Starter
Junior Member
Re: Change generated PictureBox locaton on mouse click
Originally Posted by si_the_geek
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"
-
Oct 7th, 2013, 10:45 AM
#12
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).
-
Oct 7th, 2013, 12:07 PM
#13
New Member
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 :-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|