【问题标题】:loading various images for boolean buttons?为布尔按钮加载各种图像?
【发布时间】:2013-06-11 06:03:50
【问题描述】:

我在运行时创建按钮,我想为每个值 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);
      }
    }
  }
}

【问题讨论】:

  • 1) 在资源管理器类中预加载它们(缓存) 2) 不确定你有什么问题,但如果它是单选按钮,那么它们必须在不同的容器中才能同时工作,也许考虑改用CheckBox'es? 4)取决于问题,动态生成控件的想法还不错,但是如果您使用的是位字段,那么创建指定控件可以编辑 8-, 16-, 24-, 32- 位值会更好主意。配置类的设置winforms有PropertyGrid,非常强大的控制。

标签: c# .net winforms user-interface


【解决方案1】:

您应该在项目中将checked imageunchecked image 保存为Resources。只需查看Solution Explorer,您会看到在您的项目节点下的Properties 节点下有一个Resources 节点。双击该节点,您应该知道如何将图像添加到该Resources。向其中添加图像后,您可以轻松访问这些图像,如下面的代码所示。我想您的选中图像已添加到 Resources,名称为 CheckedImage,而您未选中的图像添加到 Resources,名称为 UncheckedImage

//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;
}

你的代码冗余太多,效率不高。

【讨论】:

  • 我知道是这样,但我很难跳出框框思考,如果有任何建议,请不胜感激
  • @justanidiot 你还想要什么?建议是关于什么的?据我了解,您想用自己的Checked and Unchecked images 创建自己的RadioButton,默认情况下这些图像是圆圈。
【解决方案2】:
  1. 我认为在这种情况下您不需要图片。只需更改按钮的背景颜色即可。

  2. 创建按钮的示例代码:

    Dim pnl as new Panel
    
    Dim btn as new Button
    btn.Name = "btn1"
    btn.Location = new Point(..., ...)
    
    pnl.Controls.Add(btn)
    

对不起,我不懂 C#。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-13
    • 2013-01-06
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多