开发者

How to ensure Winforms controls stay right-aligned regardless of font setting?

I have a windows forms application written in c#. I create checkboxes, textboxes and buttons in a panel. I have some problems when I run it in the rectangle monitor.

If I make the computer font size normal (control panel -> Appearance and Themes), there is no problem. Textbox and button alignment is top right and it works.

But when I change the computer font to larger from control panel textboxes and buttons go left. Alignments don't work. By the way I created all in run time. I use this code:

tmin[i].Anchor = AnchorStyles.Right | AnchorStyles.Top;
tmax[i].Anchor = AnchorStyles.Right | AnchorStyles.Top;
gb[i].Anchor = AnchorStyles.Right | AnchorStyles.Top;

tmin and tmax are arrays of textboxes. gb is array of button. what can I do about this?

EDIT

For example initiliazation of tmax textbox array is :

 height = 1;
 paddi开发者_如何学JAVAng = 10;

 for (int i = 3; i < ff.documentColumnCount; i++)
 {                
   tmax[i] = new TextBox();
   tmax[i].Name = max + (i - 2).ToString();
   tmax[i].TabIndex = i;
   tmax[i].Size = new System.Drawing.Size(65, 20);
   tmax[i].Location = new System.Drawing.Point(326, 36 + i * 20);
   tmax[i].TextChanged += new System.EventHandler(tmax17_TextChanged);
   tmax[i].Bounds = new Rectangle(326, 20 + padding + height, 65, 22);
   tmax[i].Anchor = AnchorStyles.Right | AnchorStyles.Top;
   maxTextBoxes.Add(tmax[i]);
   panel5.Controls.Add(tmax[i]);
   height += 22;
}


Don't use anchors. Using Dock property instead, and set Min size as needed.


Instead of anchoring the control to the right edge of the form (which is what AnchorStyles.Right means), set the TextAlign property to HorizontalAlignment.Right, so that the text within the control is right-aligned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜