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

本秂侑毒 提交于 2021-02-08 06:46:38

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/46368199/c-sharp-image-and-text-on-a-button-centered-in-the-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!