■■■ 两个小问题,一、选取文本后的光标位置;二、属性能带多个参数吗?■■■

victorycy 2003-08-21 10:47:19

一、我想在文本框选取部分字符串,但是闪烁光标要求在选取的字串之前。就象Access中的组合框中的效果那样的。怎么做?

类似下面的语句,光标会在选取的字符串的末尾。

Private Sub Command1_Click()

With Text1
.SelStart = 2
.SelLength = 3
.SetFocus
End With

End Sub


二、我在类模块中定义了一个属性,想问一下,能同时带多个参数吗?如果可以的话,类实例中赋值语句的语法是怎样的?

Public Property Let rs(ByRef rsA As Recordset, strA As String, strB As String) '这样的定义编译时能通过。

这里是赋值处理代码

End Property

如果上面的属性定义是合理的,那么,赋值时怎么写?

dim A as 该类
set A = new 该类

A.rs=??? '这里怎么写赋值语句?




谢谢回贴。
...全文
62 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
victorycy 2003-08-25
  • 打赏
  • 举报
回复
up
victorycy 2003-08-23
  • 打赏
  • 举报
回复
看来大家都不这么使用属性。

那么,谁能说说微软设计多参数的属性的用意何在?为什么赋值这么难呢?
James0001 2003-08-23
  • 打赏
  • 举报
回复
2. obj.prop(a, b, c) = d
victorycyz 2003-08-22
  • 打赏
  • 举报
回复
fujiachun(傅加淳):

你说得没错。我就是觉得奇怪,为什么可以定义,不可以赋值呢?

如果我知道了赋值的语法,不是比用其它方法来得更规范简洁吗?
富察咪咪 2003-08-22
  • 打赏
  • 举报
回复
干吗非要在属性里写多个参数,放到方法里不行吗?最好少定义属性,在方法里传递参数,用Byref 返回值。
victorycy 2003-08-22
  • 打赏
  • 举报
回复
不行的,我也是这样,定义一个自定义类型的,运行时出错,错误信息:“必选参数”。光标指在属性名称这里。
qingming81 2003-08-22
  • 打赏
  • 举报
回复
Property Let Demo = p(0) 应为

demo = p(0)
qingming81 2003-08-22
  • 打赏
  • 举报
回复
不会吧?我虽然没有用过。参数与参数之间肯定有关系。比如就你上面的:

type ppoint
w as long
X as long
Y as long
z as long
end type

dim p(100) as ppoint
Property Let Demo = p(0) '------还会有其它吗?
victorycy 2003-08-22
  • 打赏
  • 举报
回复
不好意思,看了之后,还是没明白怎么写赋值语句。那个Figure 9.8,我也在MSDN找到了,看过了,不知道怎么做。

Figure 9.8:

Demo(a, b, c) = d
------- |
| \_______
\_________ |
↓ |
------- ↓
Property Let Demo(w, x, y, z)
'code omitted.
End Property
victorycy 2003-08-21
  • 打赏
  • 举报
回复
网络太慢,射天狼插队了。不过还是要谢谢你回贴。

(刚才想贴上这句话,出现错误:连续的回复不能超过3次。呵呵,CSDN不会计数了。只好再换回帐号来回贴)
victorycyz 2003-08-21
  • 打赏
  • 举报
回复
对了,这样就行了。谢谢。

我举Access的组合框的例子,是因为这个组合框在用键盘输入时,会自动匹配下拉列表中的项目,并把尚未输入完成的部分用选取字串的形式显示出来,光标仍留在输入时的位置。我觉得这样的处理比较好。

第二个问题再等待解答。

victorycyz 2003-08-21
  • 打赏
  • 举报
回复
对了,这样就行了。谢谢。

我举Access的组合框的例子,是因为这个组合框在用键盘输入时,会自动匹配下拉列表中的项目,并把尚未输入完成的部分用选取字串的形式显示出来,光标仍留在输入时的位置。我觉得这样的处理比较好。

第二个问题再等待解答。

victorycyz 2003-08-21
  • 打赏
  • 举报
回复
对了,这样就行了。谢谢。

我举Access的组合框的例子,是因为这个组合框在用键盘输入时,会自动匹配下拉列表中的项目,并把尚未输入完成的部分用选取字串的形式显示出来,光标仍留在输入时的位置。我觉得这样的处理比较好。

第二个问题再等待解答。

射天狼 2003-08-21
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
Text1.SelStart = 0
Text1.SetFocus
End Sub
qingming81 2003-08-21
  • 打赏
  • 举报
回复
一、ACCESS组合框中并不是那样的,它也是和正常的文本框一样,即当选定内容时,和你鼠标移动(或用shift和光标左右键的移动)方向一致:从左向右,光标在后;从右向左,光标在左。下面只是模似shift+光标左键移动,效果是你想要的。当然,如果移动的数字太多,则应该用循环来赋值。
Private Sub Text1_Click()
With Text1
.SelStart = 5
End With
SendKeys "+{left}"
SendKeys "+{left}"
SendKeys "+{left}"
End Sub

二、大概可以吧。没有研究过。不知其他高手是否有研究?
qingming81 2003-08-21
  • 打赏
  • 举报
回复
一、ACCESS组合框中并不是那样的,它也是和正常的文本框一样,即当选定内容时,和你鼠标移动(或用shift和光标左右键的移动)方向一致:从左向右,光标在后;从右向左,光标在左。下面只是模似shift+光标左键移动,效果是你想要的。当然,如果移动的数字太多,则应该用循环来赋值。
Private Sub Text1_Click()
With Text1
.SelStart = 5
End With
SendKeys "+{left}"
SendKeys "+{left}"
SendKeys "+{left}"
End Sub

二、大概可以吧。没有研究过。不知其他高手是否有研究?
qingming81 2003-08-21
  • 打赏
  • 举报
回复
A.rs=应该是你所带参数相关的数组(如type定义的结构)。如下面的
Public Property Get Things(ByVal X As Integer, _
ByVal Y As Integer) As Variant
' (Code for retrieval from array omitted.)
End Property

这里 A.rs = 所定义的一个点(坐标为X、Y)
qingming81 2003-08-21
  • 打赏
  • 举报
回复

The most common use for property procedures with multiple arguments is to create property arrays.

Read-Only Properties
To create a read-only property, simply omit the Property Let or (for object properties) the Property Set.

Object Properties
If you're creating a read-write object property, you use a Property Get and a Property Set, as here:

Private mwdgWidget As Widget

Public Property Get Widget() As Widget
' The Set statement must be used to return an
' object reference.
Set Widget = mwdgWidget
End Property

Public Property Set Widget(ByVal NewWidget As Widget)
Set mwdgWidget = NewWidget
End Property

Variant Properties
Read-write properties of the Variant data type are the most complicated. They use all three property procedure types, as shown here:

Private mvntAnything As Variant

Public Property Get Anything() As Variant
' The Set statement is used only when the Anything
' property contains an object reference.
If IsObject(mvntAnything) Then
Set Anything = mvntAnything
Else
Anything = mvntAnything
End If
End Property

Public Property Let Anything(ByVal NewValue As Variant)
' (Validation code omitted.)
mvntAnything = NewWidget
End Property

Public Property Set Anything(ByVal NewValue As Variant)
' (Validation code omitted.)
Set mvntAnything = NewWidget
End Property

The Property Set and Property Let are straightforward, as they're always called in the correct circumstances. However, the Property Get must handle both of the following cases:

strSomeString = objvar1.Anything
Set objvar2 = objvar1.Anything

In the first case, the Anything property contains a string, which is being assigned to a String variable. In the second, Anything contains an object reference, which is being assigned to an object variable.

The Property Get can be coded to handle these cases, by using the IsObject function to test the private Variant before returning the value.

Of course, if the first line of code is called when Anything contains an object reference, an error will occur, but that's not Property Get's problem — that's a problem with using Variant properties.

Write-Once Properties
There are many possible combinations of property procedures. All of them are valid, but some are relatively uncommon, like write-only properties (only a Property Let, no Property Get). And some depend on factors other than the kinds of property procedures you combine.

For example, when you organize the objects in your program by creating an object model, as described in "Object Models" later in this chapter, you may want an object to be able to refer back to the object that contains it. You can do this by implementing a Parent property.

You need to set this Parent property when the object is created, but thereafter you may want to prevent it from being changed — accidentally or on purpose. The following example shows how the Account object might implement a Parent property that points to the Department object that contains the account.

' Private data storage for Parent property.
Private mdeptParent As Department

Property Get Parent() As Department
' Use the Set statement for object references.
Set Parent = mdeptParent
End Property

' The property value can only be set once.
Public Property Set Parent(ByVal NewParent _
As Department)
If deptParent Is Nothing Then
' Assign the initial value.
Set mdeptParent = NewParent
Else
Err.Raise Number:=vbObjectError + 32144, _
Description:="Parent property is read-only"
End If
End Property

When you access the parent of an Account object, for example by coding strX = acctNew.Parent.Name to get the department name, the Property Get is invoked to return the reference to the parent object.

The Property Set in this example is coded so that the Parent property can be set only once. For example, when the Department object creates a new account, it might execute the code Set acctNew.Parent = Me to set the property. Thereafter the property is read-only.

For More Information Because forms in Visual Basic are classes, you can add custom properties to forms. See "Customizing Form Classes" earlier in this chapter.


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
qingming81 2003-08-21
  • 打赏
  • 举报
回复
在MSDN上查到了内容,是可以的。看下面的
Public Property Get Things(ByVal X As Integer, _
ByVal Y As Integer) As Variant
' (Code for retrieval from array omitted.)
End Property



Visual Basic Concepts

Putting Property Procedures to Work for You


Visual Basic provides three kinds of property procedures, as described in the following table.

Procedure Purpose
Property Get Returns the value of a property.
Property Let Sets the value of a property.
Property Set Sets the value of an object property (that is, a property that contains a reference to an object).


As you can see from the table, each of these property procedures has a particular role to play in defining a property. The typical property will be made up of a pair of property procedures: A Property Get to retrieve the property value, and a Property Let or Property Set to assign a new value.

These roles can overlap in some cases. The reason there are two kinds of property procedures for assigning a value is that Visual Basic has a special syntax for assigning object references to object variables:

Dim wdg As Widget
Set wdg = New Widget

The rule is simple: Visual Basic calls Property Set if the Set statement is used, and Property Let if it is not.

Tip To keep Property Let and Property Set straight, hearken back to the Basics of yore, when instead of x = 4 you had to type Let x = 4 (syntax supported by Visual Basic to this very day). Visual Basic always calls the property procedure that corresponds to the type of assignment — Property Let for Let x = 4, and Property Set for Set c1 = New Class1 (that is, object properties).

For More Information "Working with Objects" in "Programming Fundamentals" explains the use of the Set statement with object variables.

Read-Write Properties
The following code fragment shows a typical read-write property:

' Private storage for property value.
Private mintNumberOfTeeth As Integer

Public Property Get NumberOfTeeth() As Integer
NumberOfTeeth = mintNumberOfTeeth
End Property

Public Property Let NumberOfTeeth(ByVal NewValue _
As Integer)
' (Code to validate property value omitted.)
mintNumberOfTeeth = NewValue
End Property

The name of the private variable that stores the property value is made up of a scope prefix (m) that identifies it as a module-level variable; a type prefix (int); and a name (NumberOfTeeth). Using the same name as the property serves as a reminder that the variable and the property are related.

As you've no doubt noticed, here and in earlier examples, the names of the property procedures that make up a read-write property must be the same.

Note Property procedures are public by default, so if you omit the Public keyword, they will still be public. If for some reason you want a property to be private (that is, accessible only from within the object), you must declare it with the Private keyword. It's good practice to use the Public keyword, even though it isn't required, because it makes your intentions clear.

Property Procedures at Work and Play
It's instructive to step through some property procedure code. Open a new Standard Exe project and add a class module, using the Project menu. Copy the code for the NumberOfTeeth property, shown above, into Class1.

Switch to Form1, and add the following code to the Load event:

Private Sub Form_Load()
Dim c1 As Class1
Set c1 = New Class1
' Assign a new property value.
c1.NumberOfTeeth = 42
' Display the property value.
MsgBox c1.NumberOfTeeth
End Sub

Press F8 to step through the code one line at a time. Notice that when the property value is assigned, you step into the Property Let, and when it's retrieved, you step into the Property Get. You may find it useful to duplicate this exercise with other combinations of property procedures.

Arguments of Paired Property Procedures Must Match
The property procedure examples you've seen so far have been simple, as they will be for most properties. However, property procedures can have multiple arguments — and even optional arguments. Multiple arguments are useful for properties that act like arrays, as discussed below.

When you use multiple arguments, the arguments of a pair of property procedures must match. The following table demonstrates the requirements for arguments in property procedure declarations.

Procedure Declaration syntax
Property Get Property Get propertyname(1,..., n) As type
Property Let Property Let propertyname(1,..., n, n+1)
Property Set Property Set propertyname(1,..., n, n+1)


The first argument through the second-to-last argument (1,..., n) must share the same names and data types in all Property procedures with the same name. As with other procedure types, all of the required parameters in this list must precede the first optional parameter.

You've probably noticed that a Property Get procedure declaration takes one less argument than the related Property Let or Property Set. The data type of the Property Get procedure must be the same as the data type of the last argument (n+1) in the related Property Let or Property Set.

For example, consider this Property Let declaration, for a property that acts like a two-dimensional array:

Public Property Let Things(ByVal X As Integer, _
ByVal Y As Integer, ByVal Thing As Variant)
' (Code to assign array element omitted.)
End Property

The Property Get declaration must use arguments with the same name and data type as the arguments in the Property Let procedure:

Public Property Get Things(ByVal X As Integer, _
ByVal Y As Integer) As Variant
' (Code for retrieval from array omitted.)
End Property

The data type of the final argument in a Property Set declaration must be either an object type or a Variant.

Matching Up the Arguments
The reason for these argument matching rules is illustrated in Figure 9.8, which shows how Visual Basic matches up the parts of the assignment statement with the arguments of a Property Let.

Figure 9.8 Calling a Property Let procedure
victorycyz 2003-08-21
  • 打赏
  • 举报
回复
up
加载更多回复(2)

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧