Drawing text on a panel

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • computerjunkie
    New Member
    • Jan 2009
    • 1

    Drawing text on a panel

    I am trying to manually draw a rectangle with text inside of a panel. I have the code to do this but for some reason nothing is being drawn on the panel. It draws just fine on the main form but when I try to draw on the panel, nothing shows up.

    Code:
     
    private void panel1_Paint(object sender, PaintEventArgs e)
            {
                     Rectangle rec = new Rectangle(ITEM_PADDING, currentYPosition, (m_maxItemWidth - ITEM_PADDING), Height);
    
                     e.Graphics.DrawRectangle(blackPen, rec);
    
            }

    Any help is greatly appreciated. Thank you.
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    It should work, just check that your coordinates are ok and that blackPen is non-null. Try with some fixed coordinates first:

    Code:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.panel1.Paint += new PaintEventHandler(panel1_Paint);
        }
    
        void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(SystemPens.ControlDark, new Rectangle(0, 0, 20, 20));
        }
    }

    Comment

    Working...