Author Topic: Exit While Loop when waiting for user input  (Read 1977 times)

0 Members and 1 Guest are viewing this topic.

Eycki

  • Guest
Exit While Loop when waiting for user input
« on: April 16, 2012, 05:29:50 AM »
OK, this might sound very basic, but I haven't found the right answer yet.

I have a checkbox which activate a while loop when checked.  In this while loop it ask in every iteration for a selection of blocks.
When I uncheck the checkbox, it has to stop immediately.  The problem is that its already in the next loop waiting for user input (blocks to be selected).
So I can uncheck but it will still ask for one more selection until it exit the While loop.  I'm searching for something to break my getselection method.   
I know I could just do a SendStringToExecute but I prefer to use proper VB.NET code. Or maybe there or other suggestions to make my program work?

MexicanCustard

  • Swamp Rat
  • Posts: 705
Re: Exit While Loop when waiting for user input
« Reply #1 on: April 16, 2012, 07:55:31 AM »
Can you post some code?  I'm thinking the answer depends on a few specifics.  How are you presenting the checkbox to the user? Whats the code inside the while loop thats selecting blocks look like?
Revit 2019, AMEP 2019 64bit Win 10

n.yuan

  • Bull Frog
  • Posts: 348
Re: Exit While Loop when waiting for user input
« Reply #2 on: April 16, 2012, 09:46:46 AM »
If your user has already been asked to select something in AutoCAD editor in the loop, why don't use a standard AutoCAD approach, such as a keyword (eXit) or pressing Esc, to let user cancel the selection/loop instead of asking user to go to your form to uncheck a check box?

TheMaster

  • Guest
Re: Exit While Loop when waiting for user input
« Reply #3 on: April 16, 2012, 11:50:16 AM »
OK, this might sound very basic, but I haven't found the right answer yet.

I have a checkbox which activate a while loop when checked.  In this while loop it ask in every iteration for a selection of blocks.
When I uncheck the checkbox, it has to stop immediately.  The problem is that its already in the next loop waiting for user input (blocks to be selected).
So I can uncheck but it will still ask for one more selection until it exit the While loop.  I'm searching for something to break my getselection method.   
I know I could just do a SendStringToExecute but I prefer to use proper VB.NET code. Or maybe there or other suggestions to make my program work?

You'll need to use SendStringToExecute() to get out of the prompt, or not use a checkbox (just keep prompting until the user responds with Enter/Esc).


fixo

  • Guest
Re: Exit While Loop when waiting for user input
« Reply #4 on: April 16, 2012, 01:57:37 PM »
Try this code, tested on 2010
Code: [Select]
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Show()
        If CheckBox1.Checked Then '<-- initially set checked to true
            Me.TextBox1.Text = ""
            Dim bSuccess As Boolean = False
            Do
                bSuccess = testSelect()
            Loop While bSuccess
        End If
    End Sub

    Public Function testSelect() As Boolean
        Dim bln As Boolean = False
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
        Dim ed As Editor = doc.Editor

        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using tr As Transaction = db.TransactionManager.StartTransaction
            Dim peo As PromptEntityOptions = New PromptEntityOptions("")

            peo.SetRejectMessage(vbLf + "Select a block only: ")
            peo.Message = vbLf + "Select block: "
            Dim pres As PromptEntityResult = ed.GetEntity(peo)
            If pres.Status <> PromptStatus.Cancel Then
                Dim ent As Entity = tr.GetObject(pres.ObjectId, OpenMode.ForRead)
                If ent IsNot Nothing Then
                    Me.TextBox1.Text = ent.ObjectId.ToString
                    bln = True
                Else
                    bln = False
                End If
            End If
            tr.Commit()
        End Using
        Return bln
    End Function