0

実行時にボタンを作成しています。true と false の値ごとに画像を追加したかったので、onOff ボタンがある場合、true は緑の写真、false は赤になります。これを他のプロパティでも機能させたいと考えています。

私の質問は次のとおりです。1.この目的に最も適した写真をロードするにはどうすればよいですか? 2. ボタンを正しい方法で作成するには? 3. 奇妙に見える場合は、Enum ボタンに同じ関数を使用していました。4. 私は多くのことを間違っていると確信しています。アドバイスをお願いします。

foreach(p in properties){
if(p is bool){createBoolButton(p);}
//so on

string path;
string path2;
private Control createBoolButton(IProperty p) {
  countControls2 = 1;
  locationY = 10;
  int gbHeight = 2;
  radioButtonY = 10;
  IType pType = p.Type;
  var myP = new MyProperty(p, this);
  if (myP.Value != null) {
  }
  Panel gb = new Panel();
  gb.Location = new Point(locationY, nextLocationX);
  nextLocationX += rbWidth + 10;
  gb.Name = "groupBox" + p.Id;
  gb.Text = p.Id;
  gb.Tag = p;
  bool[] x = { true, false };
  foreach (var t in x) {
    RadioButton rb = new RadioButton();
    rb.Appearance = Appearance.Button;
    rb.Width = rbWidth;
    rb.Height = rbHeight;
    rb.Name = t.ToString();
    rb.Text = t.ToString();
    rb.Tag = t;
    countControls++;
    rb.Location = new Point(radioButtonY, radioButtonX);
    if (myP.Value != null && myP.Value.ToString().SafeEquals(rb.Text)) {
      rb.Checked = true;
    }
    radioButtonY += rbHeight;
    gb.Controls.Add(rb);
    rb.CheckedChanged += rb_CheckedChanged;
  }
  gb.Width = rbHeight * gbHeight + 20;
  gb.Height = rbWidth + 10;
  Controls.Add(gb);
  countControls2++;
  return gb;
}
  private void getimagesPath(EnumValue[] TypesArray) {

  foreach (var enumType in TypesArray) {
    string path = @"C:\Folder\" + enumType.Name + ".png";
    string path2 = @"C:\Folder\" + enumType.Name + "_checked.png";
    FileInfo fi = new FileInfo(path);
    FileInfo fi2 = new FileInfo(path2);
    if (!imagePaths.ContainsKey(enumType.Name) && !imagePaths.ContainsKey(enumType.Name + "_checked")) {
      if (fi.Exists && fi2.Exists) {
        imagePaths.Add(enumType.Name, path);
        imagePaths.Add(enumType.Name + "_checked", path2);
      }
    }
    else {
      if (!imagePaths.ContainsKey(enumType.Name)) {
        imagePaths.Add(enumType.Name, DEFAULT_IMAGE_PATH);
      }
    }
  }
}
4

2 に答える 2

0

checked imageandunchecked imageResourcesプロジェクトに保存する必要があります。を見るだけで、プロジェクト ノードの下にノードの下にノードSolution Explorerがあることがわかります。そのノードをダブルクリックすると、そのノードに画像を追加する方法がわかります。画像を追加すると、以下のコードに示すように、それらの画像に簡単にアクセスできます。チェックされた画像がnameで追加され、チェックされていない画像が name で追加されたとします。ResourcesPropertiesResourcesResourcesCheckedImageResourcesUncheckedImage

//Here is your RadioButton CheckedChanged event handler to change to image accordingly.
private void rb_CheckedChanged(object sender, EventArgs e){
   RadioButton button = sender as RadioButton;
   button.Image = button.Checked ? Properties.Resources.CheckedImage : Properties.Resources.UncheckedImage;
}

あなたのコードは冗長すぎて効率的ではありません。

于 2013-06-11T08:08:17.377 に答える