Skip to content

Center Align the Text of Tile Bar of Mdi Parent Form

Ankush

Asked Feb 4, 2016, 5:00 AM EST1 answer
Share
LinkedInX
I have created a windows application in which there one of the form is Mdi Parent form. I want to center align the text of this form i.e. Mdi Parent form. I have the code to center align the text of title bar of form. It is working perfectly with Mdi Child forms but not with Mdi parent form. Create the method and just call the method in Form Load event.
 
C#
private void UpdateTextPosition()
        {
         Graphics g = this.CreateGraphics();
                     Double startingPoint = (this.Width / 2) - (g.MeasureString(this.Text.Trim(), this.Font).Width / 2);
                     Double widthOfASpace = g.MeasureString(" ", this.Font).Width;
                     String tmp = " ";
                     Double tmpWidth = 0;

                     while ((tmpWidth + widthOfASpace) < startingPoint)
                     {
                                  tmp += " ";
                                  tmpWidth += widthOfASpace;
                     }
                     this.Text = tmp + this.Text.Trim();
        }


If you debug the program with break point, you can see that the above method is working i.e. the work mentioned in method is done, but the problem is that the text of title bar is not visible. Just try to debug the code and you will know it.
 
Please help !... Thanks in Advance.... 
 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    I had written this.WindowState = FormWindowState.Maximized; I disabled/commented it and run the code. it works fine. but then if you maximize the form then it doesn't work, then I tried to call the same method in MdiParent_SizeChanged, MDIParent1_ClientSizeChanged, but it doesn't work. any help posibble, please.....
    0