-1

I want to create buttons with two texts: one left (text label itself) and one right (hotkey) aligned, and possibly an image. The screenshot below is from a similar application (not mine!)

enter image description here

It would be nice to support multiple lines of text, just like buttons F1-F6 in the image, but I can live without that. I don't need the "always-pressed state" of F8 button.

I'm using C# and windows forms.

EDIT: Apparently this is very simple in WPF. Never did anything in WPF, but will surely take a look.

Victor Gomes
  • 184
  • 7
  • 15
  • What have you try so far? – Oscar Aug 12 '13 at 16:57
  • 2
    Dude, that screenshot is really horrible. I never understood why these applications have such horrible color combinations. I strongly suggest you reconsider your color scheme. BTW you can do this easily in WPF. – Federico Berasategui Aug 12 '13 at 16:58
  • Can you consider using a library? – glautrou Aug 12 '13 at 16:58
  • 1
    @HighCore: Unfortunately everybody cannot migrate all the projects from Winforms to WPF ;) – glautrou Aug 12 '13 at 16:59
  • Wouldn't using accelerator keys (which makes the shortcut underlined) suffice? Or if accelerator keys aren't an option (and you need the F* keys), manually positioning the text? The second option won't look good, but neither does the screenshot... – Pawel J. Wal Aug 12 '13 at 17:00
  • 1
    @glautrou even in winforms, this kind of end-user-torture-machine has no reason to be. – Federico Berasategui Aug 12 '13 at 17:01
  • I totally agree that the color scheme is horrible... this is not my application, just some random one that show what I want with the buttons! :) – Victor Gomes Aug 12 '13 at 17:03
  • @HighCore, I never understood it either. I like my apps to look and feel like, I dunno, all the other windows apps. At the *very* least in the theme... I chose my windows theme because I like it! – Moo-Juice Aug 12 '13 at 17:03
  • Accelerator keys are indeed an option, but I would like to use the F* keys as hotkeys. – Victor Gomes Aug 12 '13 at 17:05
  • Why the downvotes? It's because the color scheme?? As i said in the comments, t's not my application! lol – Victor Gomes Aug 12 '13 at 17:07
  • As @HighCore said you can do this easily with WPF, just by creating your own Style targeting Button. – oimitro Aug 12 '13 at 17:09
  • @VictorGomes you might get downvotes here quickly because you're not meeting the minimum requirements for a question (post the relevant code of what you already tried). I did not downvote, anyways. – Federico Berasategui Aug 12 '13 at 17:13
  • Please provide source code to get appropriate help from the community – legrandviking Aug 12 '13 at 17:15
  • I don't have any source code... at least relevant to the question... up until now I was using a regular button, with a label like "F* XXX"... now when I thought of putting the hotkey aligned to the right (as is the default, in menu items for instance) I had no idea how to do that... searched and found nothing... – Victor Gomes Aug 12 '13 at 17:21
  • Anyways, I have already searched and looks like I'll be able to do what I want with WPF... I'll try and post my results later... thanks for the suggestion @HighCore – Victor Gomes Aug 12 '13 at 17:26

1 Answers1

6

Here is the solution you may be satisfied with:

public class XButton : Button
{
    public XButton()
    {
        UseVisualStyleBackColor = false;
        TextImageRelation = TextImageRelation.ImageAboveText;
    }
    public override string Text
    {
        get { return ""; }
        set { base.Text = value;}
    }
    public string LeftText { get; set; }
    public string RightText { get; set; }
    protected override void OnPaint(PaintEventArgs pevent)
    {            
        base.OnPaint(pevent);
        Rectangle rect = ClientRectangle;
        rect.Inflate(-5, -5);
        using (StringFormat sf = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Far })
        {
            using (Brush brush = new SolidBrush(ForeColor))
            {
                pevent.Graphics.DrawString(LeftText, Font, brush, rect, sf);
                sf.Alignment = StringAlignment.Far;
                pevent.Graphics.DrawString(RightText, Font, brush, rect, sf);
            }
        }
    }
}
//Use it
xButton1.Image = yourImage;
xButton1.LeftText = "How interesting winforms is";
xButton2.RightText = "F12";
//You can add more properties to this XButton class to control how it looks

Screen shot

enter image description here

King King
  • 61,710
  • 16
  • 105
  • 130