Results 1 to 3 of 3

Thread: Custom Control not working

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2013
    Posts
    10

    Custom Control not working

    I have a form that requires several Comboboxes and TextBoxes when I tried it on one box it works:

    Code:
    Public Class frmStart
    
        'Code for Combobox Borders
        Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
            ' Add any initialization after the InitializeComponent() call.
            AddHandler Me.Paint, AddressOf DrawComboBlueBorder
        End Sub
        Private Sub DrawComboBlueBorder(ByVal sender As Object, ByVal e As PaintEventArgs)
            Dim rect As New Rectangle(Me.cboCallType.Location.X - 1,
                                      Me.cboCallType.Location.Y - 1,
                                      Me.cboCallType.Width + 2,
                                      Me.cboCallType.Height + 2)
    
            Dim pen As New Pen(Color.FromArgb(0, 172, 238), 2)
            Dim g As Graphics = e.Graphics
            g.DrawRectangle(pen, rect)
        End Sub
    
    End Class
    So I attempted to move it to a Class Library (Which I have never done before) Here is what I have, When I import the control in to my Tools, and use it, I get no border

    Code:
    Imports System.Windows.Forms
    Imports System.Drawing
    Imports System.EventArgs
    
    Public Class BorderComboBox
        Inherits ComboBox
    
        Public Sub New()
            AddHandler Me.Paint, AddressOf DrawComboBlueBorder
        End Sub
        Private Sub DrawComboBlueBorder(ByVal sender As Object, ByVal e As PaintEventArgs)
            Dim rect As New Rectangle(Me.Location.X - 1,
                                      Me.Location.Y - 1,
                                      Me.Width + 2,
                                      Me.Height + 2)
    
            Dim pen As New Pen(Color.Red, 2)
            Dim g As Graphics = e.Graphics
            g.DrawRectangle(pen, rect)
        End Sub
    End Class
    Like I said, I am just getting in to more advanced stuff, Thanks in advance for any help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Custom Control not working

    There's a REALLY big difference between your two code snippets. In the first, you are handling the Paint event of the form and drawing ON THE FORM. In the second, you're handling the Paint event of the control and drawing on the control. That is completely different.

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Custom Control not working

    Further, when you're drawing on a control it tends to be clipped to its bounds. That means anything you draw outside of its area won't actually be drawn. Since your border tries to be on the outside of the combo box, it won't be visible.

    So you'll have to draw the border inside the bounds, which means the border will overlap some of the combo box. This is easily solved by making a different custom control (it could really be a UserControl). The cheapest possible way is to make a Panel with a Blue background, then place a ComboBox inside of it and write code to size the ComboBox slightly smaller than the Panel. If you do that, then set the ComboBox's Anchor property so it resizes as the Panel resizes, it ought to have the same effect.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width