2017-01-13 1 views
1

ラジオボタンを動的に作成してグループボックス/フォームに追加しようとしましたが、ラジオボタンに関連付けられたテキスト全体が表示されません。ラジオボタンがデザイナーから追加されると、テキスト全体が表示されます。動的にラジオボタンを追加するには何か不足していますか、それを行う方法はありますか? は、以下のサンプルコードを見つけてください:WindowsフォームC#で動的に作成されるラジオボタンのテキストサイズが固定されますか?

 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); 

答えて

0

を使用すると、デザイナでテキストプロパティを設定すると、それはテキストの幅をカバーするために新しいサイズのラジオボタンを調整します。デフォルトでは、幅は90で、その上のテキストは124の幅にリサイズされていると思います。したがって、実行時にオブジェクトを作成すると、幅を90に保つだけです。しかし、r1.Width =コントロールコレクションに追加する前に124。

あなたが必要とする最大サイズに幅を設定するか、TextRenderの.MeasureTextメソッドを使用してテキストのサイズを取得してから20を追加することができるように、これも表示されるラジオボタンサークルのグラフィックをカバーし、コレクションにラジオボタンを追加する前にXプロパティの結果を幅に設定します。

 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); 
関連する問題