C# 在Windows窗体c中,动态创建的Radiobutton是否具有固定的文本大小?
我尝试动态创建一个radiobutton并将其添加到groupbox/form,但与radiobutton关联的整个文本没有显示出来。从设计器添加单选按钮时,将显示整个文本。对于动态添加单选按钮,我是否遗漏了任何内容,或者是否有任何方法可以做到这一点? 请在下面查找示例代码:C# 在Windows窗体c中,动态创建的Radiobutton是否具有固定的文本大小?,c#,radio-button,C#,Radio Button,我尝试动态创建一个radiobutton并将其添加到groupbox/form,但与radiobutton关联的整个文本没有显示出来。从设计器添加单选按钮时,将显示整个文本。对于动态添加单选按钮,我是否遗漏了任何内容,或者是否有任何方法可以做到这一点? 请在下面查找示例代码: public partial class Form1 : Form { private void SelectMicrophone_Load(object sende
public partial class Form1 : Form
{
private void SelectMicrophone_Load(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton r1 = new System.Windows.Forms.RadioButton(); //created a radiobutton
r1.Name = "Microphone(RealTex";
r1.Text = "Microphone(RealTex";
r1.Location = new System.Drawing.Point(15, 15);
this.groupBox1.Controls.Add(r1);
在设计器中设置文本属性时,它会将单选按钮调整为新的大小,以覆盖文本的宽度。默认情况下,我认为宽度是90,上面的文本的宽度调整为124。因此,当您在运行时创建对象时,可能只是将宽度保持在90。但是,在将其添加到控件集合之前,您可以只设置r1.Width=124
RadioButton r1 = new RadioButton();
r1.Text = "This is short text";
//Measure the Text property and get the width and add 20 to accomodate the circle
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 15);
this.Controls.Add(r1);
//Just another RB with even longer text.
r1 = new RadioButton();
r1.Text = "This is even longer text that we want to show";
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 35);
this.Controls.Add(r1);
请记住,您可能不知道每次的长度,因此您可以将宽度设置为所需的最大大小,或者使用TextRender的.MeasureText方法获取文本的大小,然后只需将20添加到该大小,以覆盖同样出现的单选按钮圆的图形,并将X属性的结果设置为您的宽度将radiobutton添加到集合之前
RadioButton r1 = new RadioButton();
r1.Text = "This is short text";
//Measure the Text property and get the width and add 20 to accomodate the circle
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 15);
this.Controls.Add(r1);
//Just another RB with even longer text.
r1 = new RadioButton();
r1.Text = "This is even longer text that we want to show";
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 35);
this.Controls.Add(r1);