【VB.NET】带关闭按钮的选项卡

'来源:http://www.vbforums.com/showthread.php?622242-TabControl-with-Close-button-on-TabPages-(with-Design-Time-support)

Option Strict On
Imports System.ComponentModel



<ToolboxBitmap(GetType(System.Windows.Forms.TabControl))>
Public Class TabControlEx
    Inherits System.Windows.Forms.TabControl

    Private Declare Auto Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    Protected CloseButtonCollection As New Dictionary(Of Button, TabPage)
    Private _ShowCloseButtonOnCurrentTab As Boolean = True
    Private _ShowCloseButtonOnTabs As Boolean = False


    <Browsable(True), DefaultValue(True), Category("Behavior"), Description("Indicates whether a close button should be shown on each TabPage")>
    Public Property ShowCloseButtonOnCurrentTab() As Boolean
        Get
            Return _ShowCloseButtonOnCurrentTab
        End Get
        Set(ByVal value As Boolean)
            _ShowCloseButtonOnCurrentTab = value
            For Each btn In CloseButtonCollection.Keys
                btn.Visible = _ShowCloseButtonOnCurrentTab
            Next
            RePositionCloseButtons()
        End Set
    End Property


    <Browsable(True), DefaultValue(True), Category("Behavior"), Description("Indicates whether a close button should be shown on each TabPage")>
    Public Property ShowCloseButtonOnTabs() As Boolean
        Get
            Return _ShowCloseButtonOnTabs
        End Get
        Set(ByVal value As Boolean)
            _ShowCloseButtonOnTabs = value

            RePositionCloseButtons()
        End Set
    End Property

    Protected Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()


        For Each item In CloseButtonCollection
            item.Value.Text = item.Value.Text.Trim & Space(4)
        Next

        RePositionCloseButtons()
    End Sub

    Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
        MyBase.OnControlAdded(e)
        Dim tp As TabPage = DirectCast(e.Control, TabPage)
        Dim rect As Rectangle = Me.GetTabRect(Me.TabPages.IndexOf(tp))
        Dim btn As Button = AddCloseButton(tp)
        btn.Size = New Size(rect.Height - 1, rect.Height - 1)
        btn.Location = New Point(rect.X + rect.Width - rect.Height - 1, rect.Y + 1)
        SetParent(btn.Handle, Me.Handle)
        AddHandler btn.Click, AddressOf OnCloseButtonClick
        CloseButtonCollection.Add(btn, tp)
    End Sub

    Protected Overrides Sub OnControlRemoved(ByVal e As System.Windows.Forms.ControlEventArgs)
        Dim btn As Button = CloseButtonOfTabPage(DirectCast(e.Control, TabPage))
        RemoveHandler btn.Click, AddressOf OnCloseButtonClick
        CloseButtonCollection.Remove(btn)
        SetParent(btn.Handle, Nothing)
        btn.Dispose()
        MyBase.OnControlRemoved(e)
    End Sub

    Protected Overrides Sub OnLayout(ByVal levent As System.Windows.Forms.LayoutEventArgs)
        MyBase.OnLayout(levent)
        RePositionCloseButtons()
    End Sub

    Public Event CloseButtonClick As CancelEventHandler
    Protected Overridable Sub OnCloseButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        If Not DesignMode Then
            Dim btn As Button = DirectCast(sender, Button)
            Dim tp As TabPage = CloseButtonCollection(btn)
            Dim ee As New CancelEventArgs
            RaiseEvent CloseButtonClick(sender, ee)

            If Not ee.Cancel Then
                If MsgBox("是否关闭 " + tp.Text.Trim + "标签?") = MsgBoxResult.Ok Then
                    Me.TabPages.Remove(tp)
                    RePositionCloseButtons()
                End If
            End If
            End If
    End Sub

    Protected Overridable Function AddCloseButton(ByVal tp As TabPage) As Button
        Dim closeButton As New Button
        With closeButton
            '' TODO: Give a good visual appearance to the Close button, maybe by assigning images etc.
            ''       Here I have not used images to keep things simple.
            .Text = "X"
            .FlatStyle = FlatStyle.Flat
            .BackColor = Color.FromKnownColor(KnownColor.ButtonFace)
            .ForeColor = Color.White
            .Font = New Font("Microsoft Sans Serif", 6, FontStyle.Bold)
        End With
        Return closeButton
    End Function

    Public Sub RePositionCloseButtons()
        For Each item In CloseButtonCollection
            RePositionCloseButtons(item.Value)
        Next
    End Sub

    Public Sub RePositionCloseButtons(ByVal tp As TabPage)
        Dim btn As Button = CloseButtonOfTabPage(tp)
        If btn IsNot Nothing Then
            Dim tpIndex As Integer = Me.TabPages.IndexOf(tp)
            If tpIndex >= 0 Then
                Dim rect As Rectangle = Me.GetTabRect(tpIndex)
                If Me.SelectedTab Is tp Then
                    btn.BackColor = Color.Red
                    btn.Size = New Size(rect.Height - 1, rect.Height - 1)
                    btn.Location = New Point(rect.X + rect.Width - rect.Height, rect.Y + 1)
                    btn.Visible = ShowCloseButtonOnCurrentTab
                Else
                    btn.BackColor = Color.FromKnownColor(KnownColor.ButtonFace)
                    btn.Size = New Size(rect.Height - 3, rect.Height - 3)
                    btn.Location = New Point(rect.X + rect.Width - rect.Height - 1, rect.Y + 1)
                    btn.Visible = Me.ShowCloseButtonOnTabs
                End If

                btn.BringToFront()
            End If
        End If
    End Sub

    Protected Function CloseButtonOfTabPage(ByVal tp As TabPage) As Button
        Return (From item In CloseButtonCollection Where item.Value Is tp Select item.Key).FirstOrDefault
    End Function
End Class

 

【VB.NET】TabControl有关事件

事件: Deselecting               e.TabPage.Text=TabPage1
标签: SelectedTab               TabPage1
——————–
事件: Deselected               e.TabPage.Text=TabPage1
标签: SelectedTab               TabPage1
——————–
事件: Selecting               e.TabPage.Text=TabPage2
标签: SelectedTab               TabPage2
——————–
事件: Selected               e.TabPage.Text=TabPage2
标签: SelectedTab               TabPage2
——————–
事件: SelectedIndexChanged               TabPage2
标签: SelectedTab               TabPage2

【VB.NET】自定义TextBox类,添加显示下划线属性

实现功能:
a.ShowLine 参数:设置下划线是否可见
b.LineColor参数:设置下划线颜色
c.LineWidth参数:设置下划线粗细
d.LineOffset参数:设置下划线偏移量

Imports System.ComponentModel

<ToolboxBitmap(GetType(System.Windows.Forms.TextBox))>
Public Class TextBoxEx
    Inherits System.Windows.Forms.TextBox

    Protected _lineColor As Color = Color.Black
    Protected _lineWidth As Integer = 1
    Protected _lineOffset As Integer = 3
    Protected _showLine As Boolean = False
    Protected WithEvents Line As New Control()

    <DefaultValue(GetType(Color), "Black")>
    <Description("下划线颜色")>
    Public Property LineColor As Color
        Get
            Return _lineColor
        End Get
        Set(value As Color)
            If _lineColor <> value Then
                _lineColor = value
                RaiseEvent LineColorChanged(Me, Nothing)
            End If
        End Set
    End Property

    <DefaultValue(1)>
    <Description("下划线宽度")>
    Public Property LineWidth As Integer
        Get
            Return _lineWidth
        End Get
        Set(value As Integer)
            If _lineWidth <> value Then
                _lineWidth = value
                RaiseEvent LineWidthChanged(Me, Nothing)
            End If
        End Set
    End Property

    <DefaultValue(3)>
    <Description("下划线偏移量")>
    Public Property LineOffset As Integer
        Get
            Return _lineOffset
        End Get
        Set(value As Integer)
            If _lineOffset <> value Then
                _lineOffset = value
                RaiseEvent LineOffsetChanged(Me, Nothing)
            End If
        End Set
    End Property

    <DefaultValue(False)>
    <Description("指示控件是否显示下划线")>
    Public Property ShowLine As Boolean
        Get
            Return _showLine
        End Get
        Set(value As Boolean)
            _showLine = value
            RaiseEvent ShowLineChanged(Me, Nothing)
        End Set
    End Property

    ''' <summary>
    ''' 在 TextBoxEx.LineColor 属性值发生更改时发生
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event LineColorChanged(ByVal sender As Object, e As EventArgs)

    ''' <summary>
    ''' 在 TextBoxEx.LineWidth 属性值发生更改时发生
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event LineWidthChanged(ByVal sender As Object, e As EventArgs)

    ''' <summary>
    ''' 在 TextBoxEx.ShowLine 属性值发生更改时发生
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event ShowLineChanged(ByVal sender As Object, e As EventArgs)
    ''' <summary>
    ''' 在 TextBoxEx.LineOffset 属性值发生更改时发生
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Public Event LineOffsetChanged(ByVal sender As Object, e As EventArgs)

    Public Sub onLineWidthChanged(ByVal sender As Object, e As EventArgs) Handles Me.LineWidthChanged, Me.LineOffsetChanged, Me.LineColorChanged
        If Me.ShowLine Then
            rePosLine()
        End If
    End Sub

    Public Sub onShowLineChanged() Handles Me.ShowLineChanged
        If Me.ShowLine Then
            BorderStyle = BorderStyle.None
            BackColor = Color.FromKnownColor(KnownColor.Control)
            rePosLine()
        Else
            BorderStyle = BorderStyle.Fixed3D
            BackColor = Color.FromKnownColor(KnownColor.Window)
            If Not IsNothing(Line) Then
                Line.Visible = False
            End If
        End If
    End Sub

    Protected Overrides Sub OnParentChanged(e As EventArgs)
        MyBase.OnParentChanged(e)
        If Me.ShowLine Then
            rePosLine()
        End If
    End Sub

    Protected Overrides Sub OnLocationChanged(e As EventArgs)
        MyBase.OnLocationChanged(e)
        If Me.ShowLine Then
            rePosLine()
        End If
    End Sub

    Protected Sub rePosLine()
        If Not IsNothing(Me.Parent) Then
            If Not Me.Parent.Controls.Contains(Line) Then
                Me.Parent.Controls.Add(Line)
            End If
            Line.Width = Me.Width
            Line.Height = Me.LineWidth
            Line.BackColor = Me.LineColor
            Line.Location = New Point(Me.Location.X, Me.Location.Y + Me.Height + Me.LineOffset)
            Line.Visible = True
            Line.BringToFront()
        End If
    End Sub

    Protected Overrides Sub Dispose(disposing As Boolean)
        MyBase.Dispose(disposing)
        If disposing Then
            If Not IsNothing(Line) Then
                Line.Dispose()
            End If
        End If
    End Sub

    Protected Overrides Sub OnLayout(levent As LayoutEventArgs)
        MyBase.OnLayout(levent)
        If Me.ShowLine Then
            rePosLine()
        End If
    End Sub
End Class

 

宽带修复记

昨天晚上可恶的老鼠在很隐蔽的地方把光仟线给咬断了。害我以为光猫出了问题,检查来检查去,还给复位了。致电电信后,下午时分他们才姗姗来迟。

(不知是不是技术不行,光仟线接了好几次),终于好了。师父说光衰-24dBm还不错。

但是!但是!但是!(重要的事情说三遍)

由于之前光猫复位了,需要重新进行设置注册,填写LOID和密码,所以还上不了网。电信员工掏出手机,进入后台系统,等待又等待,页面就是刷不出来。只好让他们先去维修其他地方,然后把ID和密码通过短信发给我。

过了一个多小时后,还不发来,只好打电话提醒下了。获得ID和密码后,在浏览器页面输入192.168.1.1,进入光猫,单击【设备注册】。

奇怪的是,提交后每当到获取管理IP时,总提示密码错误,打电话确认密码无误后,重试了几次还是不行(其实也可以在【网络】>【远程管理】>【OLT认证】里查看和设置)。本来想放弃的,后来想,干脆先看看进入光猫设置里看看好了。没想到设备早已注册成功,只是之前提示的信息有误而已。

 

后续部分,参考破解光猫WIFI篇。

 

补充。宽带 电话设置要注意这些