AutoCAD .Net 获取用户输入——整型数值

本文介绍了AutoCAD.Net中Editor类的各种获取用户输入的方法,包括整型、浮点型、距离、角度、坐标点及字符串等类型的输入,并详细解释了如何设置用户输入的控制项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值