Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drawing A Control On A Form 2

Status
Not open for further replies.

MrVB50au

Programmer
Mar 16, 2003
326
0
0
AU
Hi There!

Please could anyone help me.

I have been trying to draw a control in RUN MODE onto a form eg: Form1. I created two variables named oldX and oldY to store coordinates of the mouse cursors' X and Y when Form1_MouseDown is activated. Then got the control array eg: Text1(objInx) to load on the form at that specific location but in Form1_DragOver the Text1(objinx).Width = X and the Text1(objinx).Height =Y doesn't seem to want to size when mouse button is held down.

Can anyone please help me with this?

I'll be really greatfull if you could,

Andrew.
 
Just an example,
needs 1 form
1 command button with a index of 0

Option Explicit

Dim oldX As Single
Dim oldY As Single

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Load Command1(1)
oldX = X
oldY = Y
Command1(1).Top = oldY
Command1(1).Left = oldX
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If oldX = 0 Then Exit Sub
Command1(1).Width = X - oldX
Command1(1).Height = Y - oldY
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
oldY = 0
oldX = 0
Command1(1).Visible = True
End Sub
 
Thank you MattSTech

This really gave me a good start and here's what I have so far as a result.

-----------------------------------------------------------
Code:
Option Explicit

Dim oldX As Single
Dim oldY As Single

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    oldX = X
    oldY = Y
    Shape1.Left = oldX
    Shape1.Top = oldY
    Shape1.Width = oldX
    Shape1.Height = oldY
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If oldX = 0 Then Exit Sub
    Shape1.Width = X - oldX
    Shape1.Height = Y - oldY
    Shape1.Visible = True
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    oldY = 0
    oldX = 0
    Load Command1(1)
    Command1(1).Top = Shape1.Top
    Command1(1).Left = Shape1.Left
    Command1(1).Width = Shape1.Width
    Command1(1).Height = Shape1.Height
    Command1(1).Visible = True
    Shape1.Visible = False
End Sub
 
Here's the updated version of it.

Code:
Option Explicit

Dim oldX As Single
Dim oldY As Single
Dim objInx As Integer

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    oldX = X
    oldY = Y
    MousePointer = 2
    Shape1.Left = oldX
    Shape1.Top = oldY
    Shape1.Width = oldX
    Shape1.Height = oldY
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If oldX = 0 Then Exit Sub
    Shape1.Width = X - oldX
    Shape1.Height = Y - oldY
    Shape1.Visible = True
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    oldY = 0
    oldX = 0
    MousePointer = 0
    objInx = Command1.Count
    Load Command1(objInx)
    With Command1(objInx)
        .Top = Shape1.Top
        .Left = Shape1.Left
        .Width = Shape1.Width
        .Height = Shape1.Height
        .Visible = True
    End With
    Shape1.Visible = False
End Sub

There is a problem tho',.. I found that if I got the MouseDown event ativated and instead of moving the mouse from left top to bottom right, I decide to move the mouse from left top to -left and - top I get an error.

Is there anyway I can correct this, so, no matter where I move the mouse and while I still have the mouse button held down, the width and height of the shape1 control will move with the mouse and minus coordinates (if less than left and top) can still draw a rectangle?
 
Try changing your mouse move event to this:


Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If oldX = 0 Then Exit Sub

Shape1.Width = Abs(X - oldX)
Shape1.Height = Abs(Y - oldY)

If oldX > X Then
Shape1.Left = oldX - Abs(X - oldX)
End If
If oldY > Y Then
Shape1.Top = oldY - Abs(Y - oldY)
End If
Shape1.Visible = True
End Sub

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Goodness DrJavaJoe

I've got still so so much to learn about programming.

Thank you so so much for this, your knowledge as well as everyone elses is valued beyond words.

You all are a real help to people like me.

Thanks again both of you DrJavaJoe and MattSTech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top