Control User Input (.NET)

When collecting input from the user, you want to make sure you limit the type of information they can enter so you can get the desired response. The various prompt option objects are used to not only define the prompt displayed at the Command prompt, but also restrict the input that the user can provide. With some of the input methods, not only can you get a return value based on the type of method used but also get a keyword.

For example, you can use the GetPoint method to have the user specify a point or respond with a keyword. This is how commands like LINE, CIRCLE, and PLINE work.

Get an integer value or a keyword

The following example prompts the user for a positive non-zero integer value or a keyword.

C# Example

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
 
[CommandMethod("GetIntegerOrKeywordFromUser")]
public static void GetIntegerOrKeywordFromUser()
{
    Document acDoc = Application.DocumentManager.MdiActiveDocument;

    PromptIntegerOptions pIntOpts = new PromptIntegerOptions("");
    pIntOpts.Message = "\nEnter the size or ";

    // Restrict input to positive and non-negative values
    pIntOpts.AllowZero = false;
    pIntOpts.AllowNegative = false;

    // Define the valid keywords and allow Enter
    pIntOpts.Keywords.Add("Big");
    pIntOpts.Keywords.Add("Small");
    pIntOpts.Keywords.Add("Regular");
    pIntOpts.Keywords.Default = "Regular";
    pIntOpts.AllowNone = true;

    // Get the value entered by the user
    PromptIntegerResult pIntRes = acDoc.Editor.GetInteger(pIntOpts);

    if (pIntRes.Status == PromptStatus.Keyword)
    {
        Application.ShowAlertDialog("Entered keyword: " +
                                    pIntRes.StringResult);
    }
    else
    {
        Application.ShowAlertDialog("Entered value: " +
                                    pIntRes.Value.ToString());
    }
}