Search code examples
c#imagewinformsbuttonalignment

Stacking different elements in a Windows.Forms.Button


I am developing a windows forms application in c#. I am creating a large amount of buttons in loops, and I wish for both an image (icon), and text to be displayed on a button. I have experimented with alignment, but I require the image to be on the very top of the Button, and the text to be below the image. My current code is:

button1.Image = im;
button1.ImageAlign = ContentAlignment.TopCenter;
button1.Text = "CS: GO";
button1.TextAlign = ContentAlignment.MiddleCenter;

This produces this image, which is clearly not what I want:

Image

I cannot resize the Button, as the text is user defined, and subject to change in length.


Solution

  • Try the following:

    button1.TextAlign = ContentAlignment.BottomCenter;
    

    if this doesn't fit your needs there's another Property:

    button1.TextImageRelation = TextImageRelation.ImageAboveText;
    

    if I am right this will override some of your alignments.