none
graphics.drawstring is not clearing default text before drawing a new text. RRS feed

  • Question

  • HI,

       I have button control in winform. I want to keep the text color of button to white when the button is disabled so, i have used Pain event. Inside the paint event I am calling graphics.drawstring. The default text and newly drawn string both are shown. I want default text to be cleared before drawing a new string. Below is my code please let me know where i am wrong. 

    dynamic mybutton = (Button)sender;
                dynamic drawBrush = new SolidBrush(mybutton.ForeColor);
               
              
                dynamic sf = new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                };
                e.Graphics.DrawString(this.mybutton.Text, mybutton.Font, drawBrush, this.mybutton.ClientRectangle, sf);
             
                drawBrush.Dispose();
                sf.Dispose();

    This code is working fine for English characters but for Japanese characters i am facing the issue.

    Thanks

    Tuesday, November 12, 2019 12:44 PM

All replies

  • Hi deepthinikhil,

    In your code example, there is no statement about the "button is disabled".

    Maybe you can use the properties of Button.Text and Button.ForeColor to set the text and color of button, instead of using the

    method Graphics.Drawstring.

    Best Regards,

    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 13, 2019 8:13 AM