none
VB.Net 自定按鈕控制項 RRS feed

  • 問題

  • 各位大大好

    小弟用繼承的方式自訂了一個按鈕的控制項

    按鈕的事件如MouseClick、MouseEnter、MouseLeave都寫在Form中

    想請教小弟該怎麼把這些方法寫到
    Public Class Label_button中,使的我物件拉進來就有這些作用了

    Public Class Label_button
    	Inherits Label	'繼承Label
    
    	Public bool As Boolean
    
    	Private Sub Label_button_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
    
    	End Sub
    
    	Private Sub Label_button_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
    
    	End Sub
    
    	Private Sub Label_button_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    		If bool = True Then
    			'上  
    			e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 4), New Point(0, 0), New Point(sender.Width - 1, 0))
    			'左
    			e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 4), New Point(0, 0), New Point(0, sender.Height - 1))
    			'右
    			e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 2), New Point(sender.Width - 1, 0), New Point(sender.Width - 1, sender.Height - 1))
    			'下
    			e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 2), New Point(0, sender.Height - 1), New Point(sender.Width - 1, sender.Height - 1))
    		Else
    			'上 
    			e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 4), New Point(0, 0), New Point(sender.Width - 1, 0))
    			'左
    			e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 4), New Point(0, 0), New Point(0, sender.Height - 1))
    			'右
    			e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 2), New Point(sender.Width - 1, 0), New Point(sender.Width - 1, sender.Height - 1))
    			'下
    			e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 2), New Point(0, sender.Height - 1), New Point(sender.Width - 1, sender.Height - 1))
    		End If
    		
    	End Sub
    End Class

     

    Form

    Public Class Form1
    	Dim bool1 As Boolean = False
    
    	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    		Label_button1.BackColor = Color.FromArgb(196, 222, 247)	
    	End Sub
    	
    	Private Sub Label_button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label_button1.MouseClick
    
    		If bool1 = True Then
    			Label_button1.bool = True
    			bool1 = False
    			Label_button1.BackColor = Color.FromArgb(0, 136, 254)
    			Label_button1.ForeColor = Color.FromArgb(255, 255, 255)
    			Label_button1.Refresh()
    		Else
    			Label_button1.bool = False
    			bool1 = True
    			Label_button1.BackColor = Color.FromArgb(0, 136, 254)
    			Label_button1.ForeColor = Color.FromArgb(20, 20, 20)
    			Label_button1.Refresh()
    		End If
    	End Sub
    
    	Private Sub Label_button1_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label_button1.MouseEnter
    		Label_button1.BackColor = Color.FromArgb(0, 136, 254)
    	End Sub
    
    	Private Sub Label_button1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label_button1.MouseLeave
    		If Label_button1.bool = True Then
    			Label_button1.BackColor = Color.FromArgb(0, 136, 254)
    		Else
    			Label_button1.BackColor = Color.FromArgb(196, 222, 247)
    		End If
    
    	End Sub
    
    End Class


    2012年2月3日 上午 07:55

解答

  • 就把form上的code移進去就好了呀!

    Public Class Label_button
        Inherits Label  '繼承Label
    
        Public bool As Boolean
        Dim bool1 As Boolean = False
        Private Sub Label_button_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
            If bool1 = True Then
                bool = True
                bool1 = False
                BackColor = Color.FromArgb(0, 136, 254)
                ForeColor = Color.FromArgb(255, 255, 255)
                Refresh()
            Else
                bool = False
                bool1 = True
                BackColor = Color.FromArgb(0, 136, 254)
                ForeColor = Color.FromArgb(20, 20, 20)
                Refresh()
            End If
        End Sub
    
        Private Sub Label_button_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
            BackColor = Color.FromArgb(0, 136, 254)
        End Sub
    
        Private Sub Label_button_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
            If bool = True Then
                BackColor = Color.FromArgb(0, 136, 254)
            Else
                BackColor = Color.FromArgb(196, 222, 247)
            End If
        End Sub
    
        Private Sub Label_button_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            If bool = True Then
                '上  
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 4), New Point(0, 0), New Point(sender.Width - 1, 0))
                '左
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 4), New Point(0, 0), New Point(0, sender.Height - 1))
                '右
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 2), New Point(sender.Width - 1, 0), New Point(sender.Width - 1, sender.Height - 1))
                '下
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 2), New Point(0, sender.Height - 1), New Point(sender.Width - 1, sender.Height - 1))
            Else
                '上 
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 4), New Point(0, 0), New Point(sender.Width - 1, 0))
                '左
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 4), New Point(0, 0), New Point(0, sender.Height - 1))
                '右
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 2), New Point(sender.Width - 1, 0), New Point(sender.Width - 1, sender.Height - 1))
                '下
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 2), New Point(0, sender.Height - 1), New Point(sender.Width - 1, sender.Height - 1))
            End If
    
        End Sub
    End Class
    
    



    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/
    • 已標示為解答 Super Saiyan 2012年2月4日 上午 05:22
    2012年2月3日 上午 09:23

所有回覆

  • 不太確定你所表達的意思

    你所謂的"拉進來", 是指VB 中內定的標準元件嗎?

    若是使用VB內定的元件,那不可能說"拉進來"就直接可以使用你自訂的事件 ,

    除非你的元件也是自訂的

    以下是自訂的按鈕元件的參考,事件也是自訂的

      " AddHandler btn.Click, AddressOf OnCMDbtnClick " ~~~參考這一段

    希望對你有幫助

     Try
                strbtn = bin_load("QuickBtn.sec")
                tip = New ToolTip(Me.components)
                tip.Active = True
                tip.AutoPopDelay = 15000
                tip.ReshowDelay = 500
                Dim i As Integer = -1
                For Each a As String() In strbtn
                    btn = New Btn_Class
                    btn.Quick_CMD_btn(a(0), a(1))
                    i += 1
                    btn.Location = New Point(85 + i * 49, 2)
                    spc_cmd.Panel1.Controls.Add(btn)
                    AddHandler btn.Click, AddressOf OnCMDbtnClick
                    tip.SetToolTip(btn, a(2))
                Next
                strbtn.Clear()
                strbtn = Nothing
     Catch ex As Exception
    
     End Try
    

    2012年2月3日 上午 09:11
  • 就把form上的code移進去就好了呀!

    Public Class Label_button
        Inherits Label  '繼承Label
    
        Public bool As Boolean
        Dim bool1 As Boolean = False
        Private Sub Label_button_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
            If bool1 = True Then
                bool = True
                bool1 = False
                BackColor = Color.FromArgb(0, 136, 254)
                ForeColor = Color.FromArgb(255, 255, 255)
                Refresh()
            Else
                bool = False
                bool1 = True
                BackColor = Color.FromArgb(0, 136, 254)
                ForeColor = Color.FromArgb(20, 20, 20)
                Refresh()
            End If
        End Sub
    
        Private Sub Label_button_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseEnter
            BackColor = Color.FromArgb(0, 136, 254)
        End Sub
    
        Private Sub Label_button_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.MouseLeave
            If bool = True Then
                BackColor = Color.FromArgb(0, 136, 254)
            Else
                BackColor = Color.FromArgb(196, 222, 247)
            End If
        End Sub
    
        Private Sub Label_button_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            If bool = True Then
                '上  
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 4), New Point(0, 0), New Point(sender.Width - 1, 0))
                '左
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 4), New Point(0, 0), New Point(0, sender.Height - 1))
                '右
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 2), New Point(sender.Width - 1, 0), New Point(sender.Width - 1, sender.Height - 1))
                '下
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 2), New Point(0, sender.Height - 1), New Point(sender.Width - 1, sender.Height - 1))
            Else
                '上 
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 4), New Point(0, 0), New Point(sender.Width - 1, 0))
                '左
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(255, 255, 255), 4), New Point(0, 0), New Point(0, sender.Height - 1))
                '右
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 2), New Point(sender.Width - 1, 0), New Point(sender.Width - 1, sender.Height - 1))
                '下
                e.Graphics.DrawLine(New Pen(System.Drawing.Color.FromArgb(10, 10, 10), 2), New Point(0, sender.Height - 1), New Point(sender.Width - 1, sender.Height - 1))
            End If
    
        End Sub
    End Class
    
    



    亂馬客blog: http://www.dotblogs.com.tw/rainmaker/
    • 已標示為解答 Super Saiyan 2012年2月4日 上午 05:22
    2012年2月3日 上午 09:23
  • 你是要開發「伺服器控制項」嗎?

    如果是,可以參考以下的內容,

    請問如何自己製作一個ASP.NET的元件,不是UserControl

    應該有很大的幫助。


    blog.kkbruce.net
    Plurk
    Google+專頁

    春有百花秋有月,
    夏有涼風冬有雪,
    若無閒事在心頭,
    便是人間好時節。
    2012年2月3日 上午 09:38