John
John

Reputation: 343

c# Image and text on a button, centered in the button?

I have a button on a C# Windows Form form, and want to show an image and some text, side by side, and centered on the button. I tried aligning the image to the left and the text to the right, and I'm getting this (the periods are spaces):

|[IMAGE}.................Text|

But I want this:

|........[IMAGE] Text........|

My code looks like this:

btnChangeStatus.Text = "Change status to SUPPRESSED";
btnChangeStatus.TextAlign = ContentAlignment.MiddleRight;

btnChangeStatus.Image=Image.FromFile(@"J:\nomail.gif");
btnChangeStatus.ImageAlign = ContentAlignment.MiddleLeft;

I've searched here, and found lots of stuff for Java or HTML, but nothing for C#. Any ideas?

Thanks!

Upvotes: 3

Views: 4951

Answers (1)

René Vogt
René Vogt

Reputation: 43946

Set TextImageRelation to TextImageRelation.ImageBeforeText:

btnChangeStatus.TextImageRelation = TextImageRelation.ImageBeforeText;
btnChangeStatus.TextAlign = ContentAlignment.MiddleCenter;        
btnChangeStatus.ImageAlign = ContentAlignment.MiddleCenter;

Specifies that the image is displayed horizontally before the text of a control.


UPDATE: You are right, though it sounds like this should do what you want, it's still a little to the left.
I tried around a little and using

btnChangeStatus.TextImageRelation = TextImageRelation.ImageBeforeText;
btnChangeStatus.TextAlign = ContentAlignment.MiddleRight; // <- right here
btnChangeStatus.ImageAlign = ContentAlignment.MiddleCenter;

leads to the desired result, but I can't tell why the button behaves like that.

Upvotes: 6

Related Questions