我正在尝试创建一个程序,该程序从数据库中获取值并将其放入单选按钮。但是,单选按钮的数量可能会随着数据库中每个项目的变化而变化,因此我以编程方式创建了单选按钮。
但是,我在尝试使整个单选按钮列表居中时遇到困难。理想情况下,我希望选项列表在最大化窗口的中间水平居中-有什么提示吗?
System.Windows.Forms.RadioButton[] radioButtons =
new System.Windows.Forms.RadioButton[answersItems];
for (int i = 0; i < answersItems; ++i)
{
radioButtons[i] = new RadioButton();
radioButtons[i].Font = new Font("Calibri", 20);
radioButtons[i].Location = new System.Drawing.Point((1600/2) - (radioButtons[i].Text./2), question.Location.Y + question.Height + 38 + i * 38);
radioButtons[i].AutoSize = true;
this.Controls.Add(radioButtons[i]);
}
// Get all the RadioButtons on the form.
var allRadioButtons = this.Controls.OfType<RadioButton>().ToArray();
// Get the width of the widest RadioButton.
var greatestWidth = allRadioButtons.Max(rb => rb.Width);
// Calculate the X position to centre the widest RadioButton.
var commonLeft = (this.ClientSize.Width - greatestWidth) / 2;
// Align all RadioButtons to the calculated X position.
Array.ForEach(allRadioButtons, rb => rb.Left = commonLeft);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句