2011-11-14 135 views
0

我试过这段代码,但它是抛出错误。如何从文本区域清除所有以前的文本?

Private Sub ClearText 
    Dim ctl As Control 

    ' Clear all the TextBoxes on the form. 
    For Each ctl In Controls 
     If TypeOf ctl Is TextArea Then ctl.Text = "" 
    Next 
End Sub 

任何人都可以提出一些逻辑,感谢

+0

**你得到了什么错误?** – SLaks

+0

我认为Control类没有任何叫做Text的属性。你必须将'ctl'强制转换为'TextArea'才能访问它的'Text' – Roman

回答

0

我想你需要投CTL键入文本区域,然后才能设置属性Text

我认为这会工作

Private Sub ClearText 
    Dim ctl As Control 

    ' Clear all the TextBoxes on the form. 
    For Each ctl In Controls 
        If TypeOf ctl Is TextArea Then CType(ctl, TextArea).Text = "" 
    Next 
End Sub 

您也可以使用DirectCast,它可能会提供比CType更好的性能。