CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

控制用户输入 (.NET)

2023-1-1 15:30| 发布者: admin| 查看: 385| 评论: 0|来自: AutoCAD

从用户收集输入时,您希望确保限制他们可以输入的信息类型,以便获得所需的响应。各种提示选项对象不仅用于定义在命令提示符下显示的提示,还用于限制用户可以提供的输入。使用某些输入法,您不仅可以根据所用方法的类型获取返回值,还可以获取关键字。

例如,可以使用该方法让用户指定点或使用关键字进行响应。这就是 LINE、CIRCLE 和 PLINE 等命令的工作方式。GetPoint

获取整数值或关键字

下面的示例提示用户输入非零整数正值或关键字。

VB.NET

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
 
<CommandMethod("GetIntegerOrKeywordFromUser")> _
Public Sub GetIntegerOrKeywordFromUser()
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument

    Dim pIntOpts As PromptIntegerOptions = New PromptIntegerOptions("")
    pIntOpts.Message = vbCrLf & "Enter 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
    Dim pIntRes As PromptIntegerResult = acDoc.Editor.GetInteger(pIntOpts)

    If pIntRes.Status = PromptStatus.Keyword Then
        Application.ShowAlertDialog("Entered keyword: " & _
                                    pIntRes.StringResult)
    Else
        Application.ShowAlertDialog("Entered value: " & _
                                    pIntRes.Value.ToString())
    End If
End Sub

C#

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());
    }
}

VBA/ActiveX Code Reference

Sub GetIntegerOrKeywordFromUser()
 
    ' The first parameter of InitializeUserInput (6)
    ' restricts input to positive and non-negative
    ' values. The second parameter is the list of
    ' valid keywords.
    ThisDrawing.Utility.InitializeUserInput 6, "Big Small Regular"
 
    ' Set the prompt string variable
    Dim promptStr As String
    promptStr = vbCrLf & "Enter the size or [Big/Small/Regular] <Regular>:"
 
    ' At the GetInteger prompt, entering a keyword or pressing
    ' ENTER without entering a value results in an error. To allow
    ' your application to continue and check for the error
    ' description, you must set the error handler to resume on error.
    On Error Resume Next
 
    ' Get the value entered by the user
    Dim returnInteger As Integer
    returnInteger = ThisDrawing.Utility.GetInteger(promptStr)
 
    ' Check for an error. If the error number matches the
    ' one shown below, then use GetInput to get the returned
    ' string; otherwise, use the value of returnInteger.
 
    If Err.Number = -2145320928 Then
        Dim returnString As String
        Debug.Print Err.Description
        returnString = ThisDrawing.Utility.GetInput()
        If returnString = "" Then        'ENTER returns null string
           returnString = "Regular"      'Set to default
        End If
        Err.Clear
    Else                                 'Otherwise,
        returnString = returnInteger     'Use the value entered
    End If
 
    ' Display the result
    MsgBox returnString, , "InitializeUserInput Example"
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 14:58

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部