Code continues to run before setting textbox.text completes

PLM World Member Genius PLM World Member Genius
PLM World Member Genius

This is related to VB6.

We have a few computers that every once in a while experience an issue where a specific SE API command is slow to complete and other code executes and expects that the previous code completed when it hadn't.

 

The specific example is below.  We get the height of a textbox.  Then we set the value for the textbox.  Sometimes this causes the text to wrap and the textbox height to increase.  So we shrink the textsize by factor of .9 until the textbox doesn't wrap and the height is the same as it was originally.

 

Set objTextbox = objBackground.TextBoxes(lngTempIndex)
dblHeight = objTextbox.Height  'original size or textbox
objTextbox.Text = strValue

Do While objTextbox.Height > dblHeight	
      objTextbox.Edit.Textsize = objTextbox.Edit.Textsize * 0.9
      objTextbox.Height = dblHeight  'it will auto increase if text width is too long
Loop

 Can anyone give some insight as to how to ensure that the "objTextbox.Text = strValue" completes before the comparison of textbox heights is done?

 

Thanks.

Carl Breving

S&C Electric Company

9 REPLIES

Re: Code continues to run before setting textbox.text completes

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Carl,

 

The standard TextBox control does not have an Edit property as you show in your code.

 

objTextbox.Edit.Textsize = ...

 

I can only conclude that you're using a 3rd party textbox control. If that is the case, then I would suspect something going on in the 3rd party control. Without knowing the specifics of the control you're using, there's not much we can do to help.

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Code continues to run before setting textbox.text completes

PLM World Member Genius PLM World Member Genius
PLM World Member Genius

This is something we have used for over 10 years.  we're not using any custom control.  In ST4 using VB6 this is the excerpt from the Object Browser.

 

Property Edit As TextEdit
read-only
Member of SolidEdgeFrameworkSupport.TextBox
Returns the TextEdit object associated with the referenced TextBox object.

 

 

Re: Code continues to run before setting textbox.text completes

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

My bad Carl. I thought you meant your were using the standard VB6 TextBox control. Now that I understand, I'll try to reproduce the issue.

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Code continues to run before setting textbox.text completes

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Quickly looking at this, would setting the TextControlType = SolidEdgeFrameworkSupport.TextControlTypeConstants.igTextFitToContent work for you?

 

SolidEdgeFrameworkSupport.TextControlTypeConstants TextControlType { get; set; }
   Member of SolidEdgeFrameworkSupport.TextBox

Summary:
    Sets and returns the Text Control type for the referenced TextBox object.

Seems like that would make more sense than trying to manage the size of the box manually. If not, I'll keep looking into it for you.

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Code continues to run before setting textbox.text completes

PLM World Member Genius PLM World Member Genius
PLM World Member Genius
Not really. Fit to text would change the width. In this case it is for a Titleblock, which means many of the textboxes can not change size. I also can't use the AspectRatio, because that only squeezes the width of characters which makes them look really skinny which does not look good.

Ultimately my question is, is there a way to control the program so it does not proceed until after: "objTextbox.Text = strValue" actually completes?




NOTICE OF CONFIDENTIALITY:

This message may contain information that is considered confidential and which may be prohibited from disclosure under applicable law or by contractual agreement. The information is intended solely for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or use of the information contained in or attached to this message is strictly prohibited. If you have received this email transmission in error, please notify the sender by replying to this email and then delete it from your system.

Re: Code continues to run before setting textbox.text completes

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

I imagine this is difficult to reproduce since it only happens on a few machines. Conceptually, I can imagine why it's happening and I have a few ideas on how to prevent it. The following examples is written in C# but you should be able to easily follow what I'm trying to accomplish. The two keys to my approach are to leverage Application.ScreenUpdating = false and Application.DoIdle(). I would think between those two, the problem should go away.

 

Application.ScreenUpdating eliminates the need for Solid Edge to send WM_* messages to the window. I'm no MFC guru but I believe those messages are dispatched and could lead to API synchronization issues if executed in a tight loop like you've got.

 

Application.DoIdle() is used to give Solid Edge CPU time to process messages. If I'm right, it should get everything back in sync.

 

using SolidEdgeCommunity.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace TextBoxDemo
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var application = SolidEdgeCommunity.SolidEdgeUtils.Connect();
            var selectSet = application.ActiveSelectSet;
            var textBox = selectSet.Item(1) as SolidEdgeFrameworkSupport.TextBox;
            var originalHeight = textBox.Height;
            var originalTextHeight = textBox.Edit.TextSize;

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            // Disable screen updating.
            // It's much faster and minimizes computation.
            // 00:00:02.3672843 (ScreenUpdating = false)
            // 00:00:10.9002267 (ScreenUpdating = true)
            application.ScreenUpdating = false;
            
            // As a test, iterate 50 times adding to the text each time.
            for (int i = 0; i < 50; i++)
            {
                // Append test text.
                textBox.Text += " TEST";

                // Loop to shrink the text and resize the textbox.
                while (textBox.Height > originalHeight)
                {
                    // Alter text size (smaller).
                    textBox.Edit.TextSize *= .9;

                    // Allow Solid Edge to process message loop.
                    application.DoIdle();

                    // Alter textbox height to original.
                    textBox.Height = originalHeight;

                    // Allow Solid Edge to process message loop.
                    application.DoIdle();
                }
            }

            // Turn screen updating back on.
            application.ScreenUpdating = true;

            stopwatch.Stop();
            var elapsed = stopwatch.Elapsed;

            // Manually update the window.
            var sheetWindow = application.ActiveWindow as SolidEdgeDraft.SheetWindow;
            sheetWindow.Update();

            // Reset test data.
            textBox.Text = "TEST";
            textBox.Edit.TextSize = originalTextHeight;
        }
    }
}

Fingers crossed...

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Code continues to run before setting textbox.text completes

Phenom
Phenom

Jason,

We have a nmacro for copying the drawing border from a source file (we have many possible border styles) onto the background of the drawing.

I have been looking for a way to stop the screen showing all the display changes as this is processed.

What I needed was Application.ScreenUpdating = false

 

Thank you !

 

The only other thing I need to correct now is how to stop the source file from displaying when I open it - is this possible ?

 

I have tried adding Application.ScreenUpdating = false  before I open it but that has no effect.

 
HP Z420 16GB RAM
Quadro K4000
ST7 MP10 on Windows 7 Pro 64.

Re: Code continues to run before setting textbox.text completes

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

If there is a way to open a file silently, I sure can't find it. Sorry.

Jason Newell
Applications Architect
www.jasonnewell.net

Re: Code continues to run before setting textbox.text completes

Solution Partner Phenom Solution Partner Phenom
Solution Partner Phenom

Carl,

 

You might take a look at the Is there an alternative to GetObject post. My response demonstrates how to open a file without a GUI. Note that it will spawn a second instance of Edge.exe behind the scenes but if that doesn't matter to you, then it should get the job done.

Jason Newell
Applications Architect
www.jasonnewell.net