我是靠谱客的博主 成就菠萝,最近开发中收集的这篇文章主要介绍可显示/隐藏左边控件的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

最后

以上就是成就菠萝为你收集整理的可显示/隐藏左边控件的Splitter的全部内容,希望文章能够帮你解决可显示/隐藏左边控件的Splitter所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部