vb.net - 滚动父控件时如何获取可见的子控件?
问题描述
将标签添加到面板时的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 0 To 50
Panel1.Controls.Add(New Label With {.BorderStyle = BorderStyle.FixedSingle, .AutoSize = False, .Visible = True, .Text = i.ToString, .Width = 50, .Dock = DockStyle.Left})
Next
End Sub
现在,如何在面板滚动时获取可见标签,也许在Panel1.Scroll
事件中?
Private Sub Panel1_Scroll(sender As Object, e As ScrollEventArgs) Handles Panel1.Scroll
If e.ScrollOrientation = ScrollOrientation.HorizontalScroll Then
'HOW ?????
End If
End Sub
解决方案
滚动时可以获得可见的标签。您必须查看 of 中的哪个ClientRectangle
标签Panel1
。这是 的可视区域Panel1
。
在您的问题中,不清楚您想对可见标签做什么。假设您想在Textbox1
滚动时写入可查看标签的名称Panel1
。您可以使用以下代码执行此操作:
Private Sub Panel1_Scroll(sender As Object, e As ScrollEventArgs) Handles Panel1.Scroll
Dim myBounds as Rectangle = Panel1.ClientRectangle
TextBox4.Text = ""
For Each control in Panel1.Controls
Dim label as Label = CType(control,Label)
if label.Location.X > myBounds.X And label.Location.X<myBounds.Width Or label.Location.X+label.Width>myBounds.X AND label.Location.X+label.Width<myBounds.Width Then
TextBox4.Text += CType( control,Label).Text + vbCrLf
End If
Next
End Sub
请注意,上述方法列出了所有部分可见的标签。如果您只想列出所有完全可见的标签,只需更改代码:
Private Sub Panel1_Scroll(sender As Object, e As ScrollEventArgs) Handles Panel1.Scroll
Dim myBounds as Rectangle = Panel1.ClientRectangle
TextBox4.Text = ""
For Each control in Panel1.Controls
Dim label as Label = CType(control,Label)
if label.Location.X > myBounds.X And label.Location.X<myBounds.Width AND label.Location.X+label.Width>myBounds.X AND label.Location.X+label.Width<myBounds.Width Then
TextBox4.Text += CType( control,Label).Text + vbCrLf
End If
Next
End Sub
推荐阅读
- python - 通过烧瓶脚本或第三方网站向一位或多位客户付款
- html - 我可以让广告拦截器将元素视为广告吗
- laravel - 找不到类“Pbmedia\LaravelFFMpeg\FFMpeg”
- javascript - 在 NextJS 中使用 SWR 跨页面改变数据
- c# - C# .NET Core 将 HTML 表单变量传递给控制器
- python - ValueError:x 和 y 不能大于 2-D,但具有 (2, 1, 1) 和 (2,) 形状
- java - Spring 和 Prometheus 问题
- amazon-web-services - 如何使用 API Gateway 作为 CloudFront 源配置
- arrays - 机器人框架 - 找不到名称为“拆分字符串”的关键字
- mysql - 握手时 NodeJs 中的 Mysql-ssl 连接需要什么