Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi, I've made a custom control for a film and entertainment system in a windows form. The control is button which is the basic custom control with just two different colours put together and are transparaent. I put the control in my windows form and it work fine but when I add text to the button the text doesnt display? I thought at first it had something to do with the colours I used, but that hasn't affected it? I changed the text in the properties for the button, do I need to change the text in the code, instead of just the properties?

Heres the code for the control:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Button
{
    public partial class CustomControl : Control
    {
        public CustomControl()
        {
            InitializeComponent();
        }
        
        
    private Color m_color1 = Color.Green; // first color
    private Color m_color2 = Color.Red;  // second color
    private int m_color1Transparent = 100; // transparency degree 
                                             // (applies to the 1st color)
    private int m_color2Transparent = 100; // transparency degree 
                                          //  (applies to the 2nd color)

    public Color Color1
    {
        get { return m_color1; }
        set { m_color1 = value; Invalidate(); }
    }
    public Color Color2
    {
        get { return m_color2; }
        set { m_color2 = value; Invalidate(); }
    }
    public int Transparent1
    {
        get { return m_color1Transparent; }
        set { m_color1Transparent = value; Invalidate(); }
    }
    public int Transparent2
    {
        get { return m_color2Transparent; }
        set { m_color2Transparent = value; Invalidate(); }
    }

  

    protected override void OnPaint(PaintEventArgs pe)
    {
        // Calling the base class OnPaint
        base.OnPaint(pe);
        // Create two semi-transparent colors
        Color c1 = Color.FromArgb
            (m_color1Transparent , m_color1);
        Color c2 = Color.FromArgb
            (m_color2Transparent , m_color2);
        Brush b = new System.Drawing.Drawing2D.LinearGradientBrush
            (ClientRectangle, c1, c2, 10);
        pe.Graphics.FillRectangle (b, ClientRectangle);
        b.Dispose();
    }
private void f1_paint(object sender, PaintEventArgs e)
    {
        // Get Graphics Object
        Graphics g = e.Graphics;

        // Method under System.Drawing.Graphics
        g.DrawString("Download Film", new Font("Verdana", 20),
        new SolidBrush(Color.Tomato), 40, 40);
    }


}
}
Posted
Updated 1-Apr-11 6:05am
v3
Comments
[no name] 1-Apr-11 11:30am    
Without seeing what you have done it will be difficult to offer advice
programmer1234 1-Apr-11 11:34am    
I've added my code to the question.

When you start from scratch by inheriting Control (or UserControl) you need to do everything yourself, including painting the text. You are not painting text in your paint handler, so no text appears. Add a line including g.DrawString in your paint handler.

Correctly re-implementing a button is non-trivial and you might want to consider just painting the button yourself instead of creating a whole new control, though that messes with transparency.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Apr-11 11:55am    
It was easy :-) My 5 (we were writing the Answer at the same time, I think).

Congratulations with April 1st!
See my first of April post here: http://www.codeproject.com/Answers/175616/WARNING-Black-Line-of-Death-in-windows-phone-7.aspx

:-)
--SA
programmer1234 1-Apr-11 12:04pm    
Ok I added the g.Drawstring, but the text still isnt displaying? I've updated my code in the question so you can have a look.
BobJanova 1-Apr-11 13:35pm    
Uh, put it in the OnPaint method with everything else ...
Changing the property should be sufficient.

Without seeing your code, it is difficult to be sure. It may just need the NotifyParentProperty(true) attribute[^] applying, or it could be more complex.

[Edit - after seeing the code]
As has been said you haven't painted the text. You need to add something like:
C#
..................
..................
pe.Graphics.FillRectangle(b, ClientRectangle);
b.Dispose();
pe.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), Rectangle.Inflate(this.ClientRectangle, -1, -1));


I'll leave it as an exercise for you to arrange the text position.
[/Edit]
 
Share this answer
 
v2
Comments
programmer1234 1-Apr-11 11:33am    
I've added my code to the question.
Sergey Alexandrovich Kryukov 1-Apr-11 11:53am    
Hi Henry. It was so easy :-) Please see my Answer.

Congratulations with April 1st!
See my April first post here: http://www.codeproject.com/Answers/175616/WARNING-Black-Line-of-Death-in-windows-phone-7.aspx

:-)
--SA
programmer1234 1-Apr-11 12:23pm    
Yes that worked, Thanks.
Can you keep a secret? Of course you can. :-)

In your code, there is not such line which would render any text. Not at all. Base class (you call base.OnPaint) does not do it, too. As simple as that. Don't worry: I won't tell anybody…

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900