hardwired 发表于 2008-3-5 06:28:37

功能这个!!

嗨,
我有下面的代码,我想知道如何使它成为一个从10个不同的按钮调用它的函数-唯一改变的是块名所在的文本框引用(pick ),但它将被放入每个按钮的click sub。我只是想避免每个按钮都包含这些代码代码0]
.....我以前从未做过函数,当我试图为此设置一个函数时,我试图通过简单地输入函数名从每个按钮调用它,但是它标记了一个错误..
**** Hidden Message *****

Bryco 发表于 2008-3-5 09:59:58

嗨,
正如我所说,我对功能并不热,因为我以前从未创建过一个,只是不时地复制一些表单源,所以请耐心等待,并让我知道如果我编码不正确:
Public Function GETFIXINGBLOCK(FixBlock As AcadObject, PickPoint As Variant)
On Error Resume Next
PICK:
ThisDrawing.Utility.GetEntity FixBlock, PickPoint, "Please select a fixing.."
If Err0 Then
    Err.Clear
    Exit Function
Else 'Else for error..
    If FixBlock.ObjectName = "AcDbBlockReference" Then 'If block..
      If FixBlock.Name Like "A$C*" Then 'If the block name is "A$C......." (A copied and pasted block)..
            MsgBox "This isn't a true fixing block - It appears to be a copied / pasted block.." & vbCr & "Please pick another instance of it", vbExclamation, "Fixings Chart Creator.."
            GoTo PICK
      Else
            ' Other code to go here later for getting the block attribute values but for now, just the block name..
            GoTo PICKED
      End If
    ElseIf FixBlock.ObjectName"AcDbBlockReference" Then 'If NOT block..
      ThisDrawing.Utility.Prompt "You may only select a block: "
      GoTo PICK
    End If 'End If for objectname..
End If 'End if for error..
End Sub
....按钮点击的调用如下所示:
Private Sub blockpick1BTN_Click()
    FixingsChartFRM.Hide 'Hide the form..
    GETFIXINGBLOCK
PICKED:
    fx1descTXT.text = FixBlock.Name & ": " & FixBlock.Handle 'Put the block name in the text box..
    FixingsChartFRM.Show 'Show the form..
End Sub

hardwired 发表于 2008-3-5 12:20:56

据我所知,你不需要函数,你需要一个带有参数的 Sub,其中参数是 TextBox。我用了你的第一个代码:
Sub ForButton(ByRef TB As TextBox)
FixingsChartFRM.Hide 'Hide the form..
On Error Resume Next
PICK:
Dim FixBlock As AcadEntity
Dim PickPoint As Variant
ThisDrawing.Utility.GetEntity FixBlock, PickPoint, "Please select a fixing.."
If Err0 Then
    Err.Clear
    Exit Sub
Else 'Else for error..
    If FixBlock.ObjectName = "AcDbBlockReference" Then 'If block..
      If FixBlock.Name Like "A$C*" Then 'If the block name is "A$C......." (A copied and pasted block)..
            MsgBox "This isn't a true fixing block - It appears to be a copied / pasted block.." & vbCr & "Please pick another instance of it", vbExclamation, "Fixings Chart Creator.."
            GoTo PICK
      Else
            'ENTER ATTRIBUTE CODE HERE GETTING "FIXDESC" STRING..
      End If
    ElseIf FixBlock.ObjectName"AcDbBlockReference" Then 'If NOT block..
      ThisDrawing.Utility.Prompt "You may only select a block: "
      GoTo PICK
    End If 'End If for objectname..
End If 'End if for error..
PICKED:
TB.Text = FixBlock.Name & ": " & FixBlock.Handle 'Put the block name in the text box..
FixingsChartFRM.Show 'Show the form..
End Sub

你可以把这个代码放在每个按钮上,只改变TB参数

Joro-- 发表于 2008-3-5 12:57:46

所以,我必须将代码复制到每个按钮单击事件,只需将TB更改为TB1或TB2等...?

Bryco 发表于 2008-3-5 19:30:38

对,现在找到你了,它就像一个梦——不像我昨晚做的那个,那太奇怪了,哈哈
谢谢乔罗

hardwired 发表于 2008-3-6 04:28:34

我很高兴这就是你需要的!
页: [1]
查看完整版本: 功能这个!!