well i have a lil API .. which is based on command buttons and picturboxes right now .... i mean .. i click buttons and it places ( copy pastes ) associated picture box to given coordinates .. but requirement of my deisgn is very vast . i mean i can not place a command button for every scenario ( in what order and design .. would those picture boxes be placed ) ... isnt this possible.. that at run time .. i drag and drop my picture box ( and VB alligns it to nearest grid .. or u can say coordinates ) .. once again .. i need a copy paste of my picture boxes .. also ..at run time user can be able to draw a line (which means for the interconnection between those pictureboxes ) .... infact my design is kinda electronics related .. so u can assume that its kinda connections ( which i m refferin as picture boxes ) .. and lines will work as connecting wires .... i hope i do make sense ...aint i
You can use the MouseDown/MouseUp events to drag any control within a form.
Also, you can search the forum for 'snapping' objects to edges of other objects.
And you can use Line to draw lines based on the mouse coordinates (also use MouseMove and MouseDown events.
so here i m tryin this thing .... well .. now i m lookin for code to give behind ... jus as at run time .. i drag and drop my picture box , a ( i have three picture boxes ... a ,b and c ) ... as i drop it on form it should check this
that which standard coordinate is the nearest one ..
x ranges from 0 to 7
y ranges from 0 to 11
(1575+x*945,105+y*525) "these are the standard coordinates "
and vb should see that droping coordinates are closer to which one of these and should move the instance of picture box to nearest standard coordinates
lemme know .. if i m makin sense ...
seems pretty difficult task .. or i aint makin any sense ... caz it is virtually impossible that u guys dont know any things .. u r the GURUS ... k i have come up with an idea . y shouldnt apply distance formula to every drop zone to calculate the nearest standard point .... is there any method in vb ... which return the coordinates of drop zone ( point where i have droppped my picture box after dragging ) .. and also ..coordinates should be with respect to my form .. not with respect to my screen ... i hope this will work .. waiting ...
I'm sorry, but the way you have written your posts is giving me a headache. I can't understand what you want. Do you just want to be able to drag and drop a picturebox with the mouse?
i m sorry abt that .. will try to be more clear ... offcourse penagate . i want a drag drop from a mouse .. and see now .. as i m dragging any picturebox and drop it somewhere on the form ( assume this as dropping point ) ... VB should compare this droppoing point (coordinates ) with the set of coordinates i have given below
x ranges from 0 to 7
y ranges from 0 to 11
(1575+x*945,105+y*525)
and should move the picture box to nearest point ( from the 96 points i have given above ) ...
u can consider these 96 points as my grid .. as vb alligns everything to nearest grid .. i want allignment to these points ....
Oh I see now. Thanks for the clarifcation. I have actually posted an example of a drag-drop with grid alignment on the forums before. I will search for it.
yeah yeah yeah .... ... thts what i want right now .... later i ll be lookin for what u can call as ... "marked" .. i mean vb should mark those points as marked . where i have dropped a picturebox and should eliminate them from next comparison ( see i cant have two pictureboxes at one point ).. and also "undo" at run time ... removing specific picturebox from its point .. if i don want it there ....
Bah, I couldn't find my example and couldn't be bothered looking too much for it.
Basically what you need to do is integer divide your coordinates by a certain figure and then multiply them again by that same figure in order to get the "grid" effect.
Here is how to drag+drop a picture box and have it snap to a grid. Make sure both your PictureBox and your Form have .ScaleMode set to vbPixels.
VB Code:
Private Sub Picture1_MouseMove( _
Button As Integer, _
Shift As Integer, _
X As Single, _
Y As Single _
)
Static ssngMouseX As Single
Static ssngMouseY As Single
If (Button = vbLeftButton) Then
X = ((X - ssngMouseX) \ 25) * 25
Y = ((Y - ssngMouseY) \ 25) * 25
Picture1.Move (Picture1.Left + X), _
(Picture1.Top + Y)
Else
ssngMouseX = X
ssngMouseY = Y
End If
End Sub
As for using predefined points, that would be extremely slow, because you need to loop through an array of stored points and compare against each point, every time you move the mouse.
well penagate this code is workin very fine ... but i want an instance of picture box to be dragged and dropped ... i have created a control array of picture boxes but it isnt loading them .. anyway .. along with that by this new grid scheme in how many columns and rows are you dividing your form ... should i redesign my form with ur given grids ... caz u see i have to place the pictureboxes in those points .... slowness is not the issue ... am i makin sense ?
I.. err... how many columns/rows you get depends on the width/height of the form... If you want specific numbers of columns/rows then you need to work out the width/height of each column, and use those numbers in place of where I have used 25.
Originally Posted by Saint333
slowness is not the issue
On the contrary, it will be if you use predefined points. Trust me, it is not pleasant to have slow code in a _MouseMove event.
ok boss .. i ll do as u say .. forget those predefined points .. now as i had 12 rows and 8 columns in my own grid (number of points as i have given earlier in my post .. ) .... so i think .. i should divide x by 8 and y by 12 .... but this will divide the whole of form in rows and columns ... i want a lil vacant space at right where i could place my command buttons and other controls ... also .. how can i fix my form widht and height ( i don want it to vary at run time and ruin all my design
k .. i have fixed my form's length and width .. its (630 X 399) ... and i want to have some vacant space at right ( or where ever possible left , top .. anywhere ) ... and i want portion of my form to be availble as work space .. portion ( from point(105,7) to point(546,392)) .. i mean only this portion should be divided into rows and columns and mouse movement should work only in this portion of my form ... am i makin sense ?
i agree ... placed a big picture box (named it as "work") there which would work as my workspace .. now how would i clamp this pictue box there permanently .. i mean while working or testing .. it moves by clicking .. . .and also when i run and drag and drop my previous picture boxes ( a ,b and c) to this work space they get dissappeared i mean . they get under this and beomes invisible along with that .. i mentioned . i don neend to move my orignial picture boxes to workspace .. need their instances to be created at run time ....
done . .and working ..but still .... i have to work with instances of picture boxes ... and thts too inside the workspace (work) .. i only need instances to be placed at run time .......... see . i have i have three pictureboxes ( a , b and c ) .. right now placed outside workspace ( for kinda reference that user may know that what kinda connections "pictureboxes " ) he can have for further manipulation .... at run time .. i want their instance to be created ( as user click any of them ) .. and than that instance should be dragged and dropped inside that work space ( all that gridding stuff will be inside this workspace picture box ) .......
and i m cant rate ur post .. donno y . msg prompts .. " You must spread some Reputation around before giving it to penagate again." ....
To be able to dynamically create pictureboxes you could use a control array, instead of a,b,c separate controls. However, that requires a slight modification to the event handler code.
Assume, that your control array is named picBox, say
ahaan... k i have tried to create a control array of picBox . by setting its index to value "0" ( this is how .. i think control array of picture boxes is created :$ ) ... and jus for testing i have renamed my picturebox "a" to "picBox" ..
after this i deleted all other coding in my code and jus copy pasted what u gave me in previous post ...
but as soon as i paste
vb changes the color of this much portion to red
Private Sub picBox_MouseMove( _
Index As Integer
Button As Integer, _
Shift As Integer, _
X As Single, _
Y As Single _
)
and throws error like ... compile error .. expected list separator ... :$ :$ :$
ohh comon sir .. don be sorry .. i mean ... i have been eating ur brain since noon with all my stupid questions .. don u think i should be sorry first and many times ... anyway ... its again ... i mean its compiling .. running aswell .. but .. isnt creating instances .. rather .. it also isnt following mouse curser ( as it was doing ) ... rather seems like movin a car without wheels ... kinda sticks to where it is ... and tries to keep remain close to .. i mean u should see ur self .. ..
Here you go. This example lets you add and remove pictureboxes at runtime, change the size of the grid, and it even draws the grid lines for you (I thought that was quite cool myself )
I changed the behaviour of the snap-to-grid effect so that it only applies when you release the mouse button, which is a more standard behaviour.
penagate . its awesome .. i mean .. thts exactly what i was lookin for ... excellent work .. full points .. if i wouldnt have run out of reputations before .. i must have given u buch of them .... and luckily ( or you intentionally considerin the fact that i m total goof ) didnt lock the code either ... thanks a bunch . now lemme spend some time with this to alter and mess with it .... so that i may develop something according to my design ... chaos