Repeat Prompt

mmchugh
Participant
Participant

Repeat Prompt

mmchugh
Participant
Participant

Hi All,

 

     I'm new to coding and I am creating a command where I would like to prompt the user for a few variables at the start of the command.  I would like the command not to just throw an error message and stop if an invalid response is given to one of the prompts.  Ideally, the prompt would simply be displayed again with a message like: "YOUR RESPONSE WAS INVALID, PLEASE TRY AGAIN." 

     Does anyone know how to do this? Or what it's called in coding so I can learn about it?  I really just don't know where to look.  Thanks for any help.

0 Likes
Reply
Accepted solutions (1)
815 Views
5 Replies
Replies (5)

norman.yuan
Mentor
Mentor

Firstly, I assume you do know how to use PromptXxxxxOptions class in conjunction with Editor.GetXxxxx(), where the various PromptXxxxxOptions class already provides some ways to allow/disallow certain user input. For example, with PromptKeywordOptions, user would only allowed to input according to the listed keyword, or hit Enter for default keyword, or hit Esc for cancelling. Then Edtior.GetXxxx() method returns PromptResult class that flags the returned result is OK, or not (cancelled, errored, ... ). If you have your specific validating rule that requires more logic than PromptXxxxxOptions can handle, you can do it after PromptResult being returned as OK; and you may do the work in a loop, something like (assume you expect a 5-char long text input):

 

string answer = null;

while(true)

{

    var opt=new PromptStringOptions(...);

    ...

   var res = ed.GetString(opt);

   if (res.Status = PromptStatus.OK)

   {

      //Do your own validation here

      if (res.StringResult.Leght==5)

      {

         answer=res.StringResult;

         break;

      }

      else

      {

         ed.WriteMessage("\nInvalid: must enter 5 characters!"); //loop back

      }

   }

   else

   {

      break; //User hits Esc to cancel the input

   }

}

 

if (!string.IsNullOrEmpty(aswer))

{

   //There is valid user input, go on with your business

}

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

mmchugh
Participant
Participant
Hi Norman,

Thank you for the fast response. Yes. I've found sufficient documentation on prompting and storing results as variables, while allowing/disallowing certain inputs. My question is, after the else statement where you throw the error "Invalid: must enter 5 characters!", will it loop right back to the original prompt without ending the command? How do you do that? Thanks again.


0 Likes

norman.yuan
Mentor
Mentor
Accepted solution

If you read my sample code carefully enough, you would have seen that line of code that prompt user for invalid input, and the execution would loop back (i.e. the while(true){...} loop only end either user hits Esc, or enters 5-char text, thus the two "break;" statements you can see in the while(){...} block):

 

while(true)

{

    var opt=new PromptStringOptions(...);

    ...

   var res = ed.GetString(opt);

   if (res.Status = PromptStatus.OK)

   {

      //Do your own validation here

      if (res.StringResult.Leght==5)

      {

         answer=res.StringResult;

         break;

      }

      else

      {

         ed.WriteMessage("\nInvalid: must enter 5 characters!"); //loop back

      }

   }

   else

   {

      break; //User hits Esc to cancel the input

   }

}

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes

_gile
Mentor
Mentor

Hi,

Here's a simple example:

        [CommandMethod("TEST")]
        public static void Test()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            string result;
            while (true)
            {
                var pr = ed.GetString("\nEnter a 5 characters string: ");

                // if the user cancels, exit the method.
                if (pr.Status != PromptStatus.OK)
                {
                    return;
                }
                // if the user entered a valid input, break the while loop.
                if (pr.StringResult.Length == 5)
                {
                    result = pr.StringResult;
                    break;
                }
                // else, prompt a messahe and loop
                ed.WriteMessage("\nThe string must have 5 caracters.");
            }
            Application.ShowAlertDialog(result);
        }

Hi,

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes

mmchugh
Participant
Participant
Ahhh. I see. It comes down to the while() statement. Thank you good sir.


0 Likes