清除表单中的所有 RichTextBox (VB.NET)

发布于 2024-08-10 19:55:36 字数 126 浏览 6 评论 0原文

我有一个包含 25 个 RichTextBox 的表单。当用户按下按钮时我需要清除它们。

我认为这与:Me.Controls.Clear有关,但我不确定。

感谢您的任何帮助。 :)

I have a form with 25 RichTextBoxes. I need to clear them all when a user presses a button.

I think it's something to do with: Me.Controls.Clear but I'm not sure.

Thanks for any help. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不忘初心 2024-08-17 19:55:36

Me.Controls.Clear 将从 Controls 集合中删除所有控件。您需要迭代 Controls 中的所有控件,如果控件的类型为 RichTextBox,则对该控件调用一些明确的方法。

Me.Controls.Clear will remove all controls from the Controls collection. You need to iterate over all controls in Controls and if control is of type RichTextBox then call some clear method on that control.

小…楫夜泊 2024-08-17 19:55:36

你可以这样做。它会清除表单上的所有文本框。如果你想保存任何富文本框,你可以检查ctl.name

Dim ctl As Control
Dim rt As RichTextBox

For Each ctl In Me.Controls
  If TypeOf (ctl) Is RichTextBox Then
    rt = ctl
    rt.Clear()
  End If
Next ctl

You can do it this way. It clears all the textboxes on the form. If you want to save any richtextboxes, you can check ctl.name.

Dim ctl As Control
Dim rt As RichTextBox

For Each ctl In Me.Controls
  If TypeOf (ctl) Is RichTextBox Then
    rt = ctl
    rt.Clear()
  End If
Next ctl
江湖彼岸 2024-08-17 19:55:36

我认为你可以使用 linq 来选择所有文本框...类似这样的东西(未经测试且使用 ac# 语法),

IEnumerable<RichTextBox> txtBoxes = from txt in form1.Controls
                                where txt is RichTextBox
                                select (RichTextBox) txt;

然后你可以执行 foreach 循环清除它。

foreach(RichTextBoxt in txtBoxes)
{
    // t.clear() ... clear(t) ... t.Text=String.empty ... o whatever you want
}

I think you could use linq to select all the textboxes... something like this (not tested and with a c# syntax)

IEnumerable<RichTextBox> txtBoxes = from txt in form1.Controls
                                where txt is RichTextBox
                                select (RichTextBox) txt;

then you can do a foreach loop clearing it.

foreach(RichTextBoxt in txtBoxes)
{
    // t.clear() ... clear(t) ... t.Text=String.empty ... o whatever you want
}
笑咖 2024-08-17 19:55:36

显然,任何 from ctrl in form.Controls... 方法都会跳过面板/其他容器中的任何(丰富)文本框。这也是 MarkJ 在评论 Jonathan 的回答时所说的。

下面是一个例程,用于显式递归所有控件并清除 (1) 没有子项且 (2) 是(丰富)文本框的任何控件。

Private Sub ClearControl(ByVal ctrl As Control)

    If ctrl.Controls.Count > 0 Then
        For Each subCtrl As Control In ctrl.Controls
            ClearControl(subCtrl)
        Next
    End If

    If TypeOf ctrl Is RichTextBox Then
        DirectCast(ctrl, RichTextBox).Clear()
    End If

    REM You can clear other types of controls in here as well
    If TypeOf ctrl Is TextBox Then
       DirectCast(ctrl, TextBox).Clear()
    End If

End Sub

将表单作为根控件传递以开始递归以清除所有所需的子控件:
ClearControl(Me)

Apparently any from ctrl in form.Controls... approach skips any (rich)textboxes that live within a panel/other container. This is also what MarkJ said in comment to Jonathan's answer.

Here's a routine to explicitly recurse over all controls and clear any control that (1) has no children, and (2) is a (rich)textbox.

Private Sub ClearControl(ByVal ctrl As Control)

    If ctrl.Controls.Count > 0 Then
        For Each subCtrl As Control In ctrl.Controls
            ClearControl(subCtrl)
        Next
    End If

    If TypeOf ctrl Is RichTextBox Then
        DirectCast(ctrl, RichTextBox).Clear()
    End If

    REM You can clear other types of controls in here as well
    If TypeOf ctrl Is TextBox Then
       DirectCast(ctrl, TextBox).Clear()
    End If

End Sub

Pass the form as the root control to start the recursion to clear all desired subcontrols:
ClearControl(Me).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文