可显示/隐藏左边控件的Splitter

有些软件中的splitter中间有一个按钮,点击后可显示/隐藏左边的控件,如WPS就是这样。在.NET中也自定义了一个。

类代码:
Public Class mySplitter

Public Shared Sub AddShowHideEvent(ByVal sl As Splitter)
Dim lbl As New Label
lbl.Text = "3"
lbl.Font = New System.Drawing.Font("Marlett", 7.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(2, Byte))
lbl.AutoSize = True
lbl.Location = New Point(-3.5, sl.Height / 2 - lbl.Height)
lbl.Cursor = Cursors.Hand
lbl.ForeColor = Color.Red
sl.Controls.Add(lbl)
sl.Width = 3.5

AddHandler lbl.Click, AddressOf lbl_Click
AddHandler sl.Resize, AddressOf splitter_Resize

Dim tt As New ToolTip
tt.SetToolTip(lbl, "隐藏左边的的控件")
lbl.Tag = tt
End Sub


Private Shared Sub lbl_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label = CType(sender, Label)
Dim targetctrl As Control
Dim sl As Splitter = CType(lbl.Parent, Splitter)
Dim pctrl As Control = sl.Parent

For Each temp As Control In pctrl.Controls
If Not temp.Equals(sl) Then
If sl.Dock = DockStyle.Left Then
If temp.Dock = DockStyle.Left AndAlso _
(Not temp.Visible OrElse temp.Left < sl.Left) Then
targetctrl = temp
Exit For
End If
ElseIf sl.Dock = DockStyle.Top Then
If temp.Dock = DockStyle.Top AndAlso _
(Not temp.Visible OrElse temp.Top < sl.Top) Then
targetctrl = temp
Exit For
End If
End If
End If
Next

If Not targetctrl Is Nothing Then
Dim tt As ToolTip = CType(lbl.Tag, ToolTip)
If lbl.Text = "3" Then
lbl.Text = 4
tt.SetToolTip(lbl, "显示左边的的控件")
targetctrl.Visible = False
Else
lbl.Text = 3
tt.SetToolTip(lbl, "隐藏左边的的控件")
targetctrl.Visible = True
End If
End If
End Sub

Private Shared Sub splitter_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sl As Splitter = CType(sender, Splitter)
Dim lbl As Label = sl.Controls(0)
lbl.Location = New Point(-3.5, sl.Height / 2 - lbl.Height)
End Sub
End Class

使用时,在窗体的初始化代码中加入如下所示的红色代码即可:
Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化
  mySplitter.AddShowHideEvent(Me.Splitter1)
End Sub
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值