天天看點

AutoCAD .Net 擷取使用者輸入——整型數值

AutoCAD .Net中的Editor類提供了各種擷取使用者輸入的方法,常用的有:

* GetInteger 擷取整型數值

* GetDouble 擷取浮點型數值

* GetDistance 擷取距離值

* GetAngle 擷取角度值

* GetPoint 擷取坐标點

* GetString 擷取字元串

* GetKeywords 擷取關鍵字

GetInteger 的用法

Document doc = Application.DocumentManager.MdiActiveDocument;

PromptIntegerOptions options = new PromptIntegerOptions("\n請輸入整數: ");
PromptIntegerResult result = doc.Editor.GetInteger(options);
switch (result.Status)
{
    case PromptStatus.OK:
        doc.Editor.WriteMessage("\nValue = " + result.Value);
        break;

    case PromptStatus.Cancel:
        doc.Editor.WriteMessage("\n使用者取消了輸入");
        break;

    default:
        break;
}           

PromptIntegerOptions 用于設定使用者輸入的控制項

* AllowNone 是否允許空輸入,預設為false。

* DefaultValue 預設值。

* AllowZero 是否允許輸入0。

* AllowNegative 是否允許輸入負值。

* LowerLimit 控制所允許的最小值。

* UpperLimit 控制所允許的最大值。

* AllowArbitraryInput 是否允許任意輸入,預設為false,在這種情況下,當使用者輸入無效時,比如輸入的是英文字母等,GetInteger并不傳回,而是提示使用者繼續輸入。當AllowArbitraryInput 設定為true,可以接受任何無效輸入,程式需要處理無效輸入的情況。

簡單應用

Document doc = Application.DocumentManager.MdiActiveDocument;

int value = 0;
PromptIntegerOptions options = new PromptIntegerOptions("\n請輸入整數: ");
options.AllowNone = true;
options.DefaultValue = 50;
options.LowerLimit = -100;
options.UpperLimit = 100;
PromptIntegerResult result = doc.Editor.GetInteger(options);
switch (result.Status)
{
    case PromptStatus.OK:
        value = result.Value;
        doc.Editor.WriteMessage("\nValue = " + value);
        break;

        // 空輸入
    case PromptStatus.None:
        value = options.DefaultValue;
        doc.Editor.WriteMessage("\nValue = " + value);
        break;

    case PromptStatus.Cancel:
        doc.Editor.WriteMessage("\n使用者取消了輸入");
        break;

    default:
        break;
}           

允許任意輸入

Document doc = Application.DocumentManager.MdiActiveDocument;

int value = 0;
PromptIntegerOptions options = new PromptIntegerOptions("\n請輸入整數: ");
options.AllowNone = true;
options.DefaultValue = 50;
options.AllowArbitraryInput = true;
PromptIntegerResult result = doc.Editor.GetInteger(options);
switch (result.Status)
{
    case PromptStatus.OK:
        value = result.Value;
        doc.Editor.WriteMessage("\nValue = " + value);
        break;

    // 空輸入
    case PromptStatus.None:
        value = options.DefaultValue;
        doc.Editor.WriteMessage("\nValue = " + value);
        break;

    // 任意輸入
    case PromptStatus.Keyword:
        if (result.StringResult == "HelloWorld")
        {
            doc.Editor.WriteMessage("\nHello World! --- AutoCAD");
        }
        else
        {
            doc.Editor.WriteMessage("\n使用者輸入了無效的值");
        }
        break;

    case PromptStatus.Cancel:
        doc.Editor.WriteMessage("\n使用者取消了輸入");
        break;

    default:
        break;
}           

關鍵字

Document doc = Application.DocumentManager.MdiActiveDocument;
int value = 0;
PromptIntegerOptions options = new PromptIntegerOptions("\n請輸入整數: ");
options.AllowNone = true;
options.DefaultValue = 50;
options.Keywords.Add("Circle", "Circle", "圓(L)", true, true);
options.Keywords.Add("Line", "Line", "直線(L)", true, true);
options.Keywords.Add("Esc", "Esc", "退出(Esc)", true, true);
options.AppendKeywordsToMessage = true;

PromptIntegerResult result = doc.Editor.GetInteger(options);
switch (result.Status)
{
    case PromptStatus.OK:
        value = result.Value;
        doc.Editor.WriteMessage("\nValue = " + value);
        break;

        // 空輸入
    case PromptStatus.None:
        value = options.DefaultValue;
        doc.Editor.WriteMessage("\nValue = " + value);
        break;

        // 關鍵字
    case PromptStatus.Keyword:
        if (result.StringResult == "Circle")
        {
            doc.Editor.WriteMessage("\n使用者輸入了關鍵字: Circle");
        }
        else if (result.StringResult == "Line")
        {
            doc.Editor.WriteMessage("\n使用者輸入了關鍵字: Line");
        }
        else if (result.StringResult == "Esc")
        {
            doc.Editor.WriteMessage("\n使用者退出");
        }
        break;

    case PromptStatus.Cancel:
        doc.Editor.WriteMessage("\n使用者取消了輸入");
        break;

    default:
        break;
}           

繼續閱讀