Code does not execute before closing/hiding the userform

Code does not execute before closing/hiding the userform

sunchuanpu
Enthusiast Enthusiast
1,609 Views
6 Replies
Message 1 of 7

Code does not execute before closing/hiding the userform

sunchuanpu
Enthusiast
Enthusiast

Hello,

I have met this issue in multiple projects of mine so I think I should better come to ask. 

 

I have a Userform containing user input and start button, once the button clicked I hide this Userform, run the code, and then show the Userform again for another run.

 

The problem is, the code does not seem to run before the "UserForm.Show" at the end of my code. The changes made by the code only display in the drawing after I close the Userform, or when I click the start button on the Userform to make the code run again. How can I update the drawing before showing the Userform?

 

 

- UserForm.Show stands at the end of the button_click sub while it is placed right after ErrorHandler of " On Error GoTo"

- I tried to put a msgbox right before the UserForm.Show, the msgbox displays, while the code still does not run.

0 Likes
Accepted solutions (1)
1,610 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

Show your code

0 Likes
Message 3 of 7

sunchuanpu
Enthusiast
Enthusiast

I was trying to give you a simple sample which only draws one line. However, when I test it, the problem occurs under AutoCAD 2007 but not 2010 (I only have these two versions right now), so I guess maybe the AutoCAD version is a factor of this problem?

I found the following code I wrote before, and AutoCAD2010 still has the problem(does not display result until Userform closed). The Userform contains a start button and three textboxes. This code doesn't even have an error handler. 

 

Private Sub Button_Start_Click()
UserForm_BreakLine.Hide

Dim pt1 As Variant
Dim pt2 As Variant
Dim ppt As Variant
Dim angle As Double
Dim distance As Double
Dim line As AcadLine
Dim exl1 As AcadLine
Dim exl2 As AcadLine
Dim l1 As AcadLine
Dim l2 As AcadLine
Dim brl1 As AcadLine
Dim brl2 As AcadLine
Dim brl0 As AcadLine

'register entered values
Dim exlength As Double
Dim breakwidth As Double
Dim breaklength As Double
exlength = TextBox_EXLength.value
breakwidth = TextBox_BreakWidth.value
breaklength = TextBox_BreakLength.value

Dim pi As Double
pi = 4 * Atn(1)
'-------------------------------------------------------------
pt1 = ThisDrawing.Utility.GetPoint(, "Pick first point")
pt2 = ThisDrawing.Utility.GetPoint(pt1, "Pick second point")

Set line = ThisDrawing.ModelSpace.AddLine(pt1, pt2)

angle = ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)
distance = line.Length

ppt = ThisDrawing.Utility.PolarPoint(pt1, angle + pi, exlength)
Set exl1 = ThisDrawing.ModelSpace.AddLine(pt1, ppt)
ppt = ThisDrawing.Utility.PolarPoint(pt2, angle, exlength)
Set exl2 = ThisDrawing.ModelSpace.AddLine(pt2, ppt)
ppt = ThisDrawing.Utility.PolarPoint(pt1, angle, (distance - breaklength) / 2)
Set l1 = ThisDrawing.ModelSpace.AddLine(pt1, ppt)
ppt = ThisDrawing.Utility.PolarPoint(pt2, angle + pi, (distance - breaklength) / 2)
Set l2 = ThisDrawing.ModelSpace.AddLine(pt2, ppt)
ppt = ThisDrawing.Utility.PolarPoint(l1.EndPoint, angle - pi / 2, breakwidth / 2)
Set brl1 = ThisDrawing.ModelSpace.AddLine(l1.EndPoint, ppt)
ppt = ThisDrawing.Utility.PolarPoint(l2.EndPoint, angle + pi / 2, breakwidth / 2)
Set brl2 = ThisDrawing.ModelSpace.AddLine(l2.EndPoint, ppt)
Set brl0 = ThisDrawing.ModelSpace.AddLine(brl1.EndPoint, brl2.EndPoint)

line.Delete
UserForm_BreakLine.Show
End Sub

 

 

0 Likes
Message 4 of 7

sieclaprzemyslaw
Contributor
Contributor
Accepted solution

Hi,

I think you have to regen your drawing to see the changes before you show your user form again.

Please try that:

 

(...)
line.Delete
ThisDrawing.Regen acAllViewports
UserForm_BreakLine.Show
End Sub

 

 

Message 5 of 7

sunchuanpu
Enthusiast
Enthusiast
This solves the problem, thank you very much.
0 Likes
Message 6 of 7

norman.yuan
Mentor
Mentor

While REGEN solves your issue, it could be very "expensive" operation, if the drawing has large number of entities: regen may take noticeable time.

 

In your code, the quick way to update screen on individual entities, especially those just created, calling its Update() method does the work:

pt1 = ThisDrawing.Utility.GetPoint(, "Pick first point")
pt2 = ThisDrawing.Utility.GetPoint(pt1, "Pick second point")

Set line = ThisDrawing.ModelSpace.AddLine(pt1, pt2)

angle = ThisDrawing.Utility.AngleFromXAxis(pt1, pt2)
distance = line.Length

ppt = ThisDrawing.Utility.PolarPoint(pt1, angle + pi, exlength)
Set exl1 = ThisDrawing.ModelSpace.AddLine(pt1, ppt)
exl1.Update ppt = ThisDrawing.Utility.PolarPoint(pt2, angle, exlength) Set exl2 = ThisDrawing.ModelSpace.AddLine(pt2, ppt)
exl2.Update ppt = ThisDrawing.Utility.PolarPoint(pt1, angle, (distance - breaklength) / 2) Set l1 = ThisDrawing.ModelSpace.AddLine(pt1, ppt)
l1.Update ppt = ThisDrawing.Utility.PolarPoint(pt2, angle + pi, (distance - breaklength) / 2) Set l2 = ThisDrawing.ModelSpace.AddLine(pt2, ppt)
l2.Update ppt = ThisDrawing.Utility.PolarPoint(l1.EndPoint, angle - pi / 2, breakwidth / 2) Set brl1 = ThisDrawing.ModelSpace.AddLine(l1.EndPoint, ppt)
brl1.Update ppt = ThisDrawing.Utility.PolarPoint(l2.EndPoint, angle + pi / 2, breakwidth / 2) Set brl2 = ThisDrawing.ModelSpace.AddLine(l2.EndPoint, ppt)
brl2.Update Set brl0 = ThisDrawing.ModelSpace.AddLine(brl1.EndPoint, brl2.EndPoint) brl0.Update line.Delete UserForm_BreakLine.Show

Since "line" is just a temporary line for calculation, there is no need to call its Update(). As matter of fact, it is simple math to calculate distance between 2 points (picked by user), there is no need at all to create a line and then erase it later.

Norman Yuan

Drive CAD With Code

EESignature

Message 7 of 7

sunchuanpu
Enthusiast
Enthusiast
Thank you very much for your suggestions.
0 Likes