我是VB中控件数组的旧时用户。它已经消失了,所以我正在寻找替代品。
我在C#中为Silverlight创建了一个用户控件。此控件公开bool属性IsChecked。
<ImageButton Name"imgbutton1" IsChecked="True"/>
<ImageButton Name"imgbutton2" IsChecked="False"/>
<ImageButton Name"imgbutton3" IsChecked="False"/>
在子窗口中,我填充了一系列这些控件,并将它们用作RadioButtons的等效项,即,当我点击一个时,任何其他具有IsChecked = true的将被设置为false,我想到了做类似的事情:
List<ImageButton> imgButtons= new List<ImageButton>();
imgButtons.Add(imgbutton1);
imgButtons.Add(imgbutton2);
imgButtons.Add(imgbutton3);
然后,要清除所有IsChecked但我需要的那个(由某个'index'变量指向),我会做类似的事情:
foreach (ImageButton imgbutton in imgButtons) imgbutton.IsChecked = false;
imgbuttons[index].IsChecked = true;
我遇到的问题是List<ImageButton>
没有编译。我对收藏品非常不稳定,无法弄清楚我错过了什么。我可以为标准控件做,但不允许我在那里获得用户控件。
谢谢!
PS:我想过只是自定义一个RadioButton控件,但没有Blend,我正在将这个图像控件用于其他类型的控件。但是,如果您认为有更好的方法来实现这一点,请告诉我。
编辑:编译器说“找不到类型或命名空间名称'ImageButton'(您是否缺少using指令或汇编引用?)'
答案 0 :(得分:3)
确保在代码中引用了“ImageButton”控件(在“using”部分中)。
要自动执行此操作,您只需右键单击代码中的“ImageButton”并单击“解决”,它将自动添加参考
答案 1 :(得分:2)
当您说“List is not compiling”时,您没有告诉我们编译器错误是什么。
我的猜测是你需要在文件中包含List集合的命名空间。
using System.Collections.Generic;
答案 2 :(得分:1)
想到别的东西,我不确定你为什么要保留一个List&lt;&gt;您的ImageButtons是否在显示中(在该子窗口中)。如果它们都在同一个容器中,你可以使用类似的东西:
// here "grd" is your Grid container, it could be another type of container though
foreach (ImageButton imgBtn in grd.Children.OfType<ImageButton>())
imgBtn.IsChecked = false;
答案 3 :(得分:-1)
这可能有所帮助。
用户控制代码:
//usercontrol1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace WindowsFormsApplication1
{
public partial class UserControl1 : UserControl
{
Color formcolor;
public UserControl1()
{
InitializeComponent();
}
public void setvals(string a1,string a2,string a3,string a4)
{
t1.Text=a1;
t2.Text=a2;
t3.Text=a3;
t4.Text=a4;
}
public Color formColor
{
get
{
return formcolor;
}
set
{
formcolor = value;
this.BackColor = formcolor;
}
}
}
}
usercontrol.designer.cs
namespace WindowsFormsApplication1
{
partial class UserControl1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.t1 = new System.Windows.Forms.TextBox();
this.t2 = new System.Windows.Forms.TextBox();
this.t3 = new System.Windows.Forms.TextBox();
this.t4 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// t1
//
this.t1.Location = new System.Drawing.Point(20, 16);
this.t1.Name = "t1";
this.t1.Size = new System.Drawing.Size(100, 20);
this.t1.TabIndex = 0;
//
// t2
//
this.t2.Location = new System.Drawing.Point(20, 42);
this.t2.Name = "t2";
this.t2.Size = new System.Drawing.Size(100, 20);
this.t2.TabIndex = 1;
//
// t3
//
this.t3.Location = new System.Drawing.Point(20, 68);
this.t3.Name = "t3";
this.t3.Size = new System.Drawing.Size(100, 20);
this.t3.TabIndex = 2;
//
// t4
//
this.t4.Location = new System.Drawing.Point(20, 94);
this.t4.Name = "t4";
this.t4.Size = new System.Drawing.Size(100, 20);
this.t4.TabIndex = 3;
//
// UserControl1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.t4);
this.Controls.Add(this.t3);
this.Controls.Add(this.t2);
this.Controls.Add(this.t1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(278, 133);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox t1;
private System.Windows.Forms.TextBox t2;
private System.Windows.Forms.TextBox t3;
private System.Windows.Forms.TextBox t4;
}
}
现在以完全动态的形式可以像这样创建一个用户控件的数组
全局变量
private UserControl1[] userControl11=new WindowsFormsApplication1.UserControl1[3];
现在是用户控件数组
你可以在按钮上写下
this.SuspendLayout();
Random r=new Random(DateTime.Now.Millisecond);
for (int i = 0; i < 3; i++)
{
userControl11[i] = new UserControl1();
this.userControl11[i].formColor = Color.FromArgb(r.Next(255),r.Next(255),r.Next(255));
this.userControl11[i].Location = new System.Drawing.Point(133 , 133*(i+1));
this.userControl11[i].Name = "userControl11";
this.userControl11[i].Size = new System.Drawing.Size(278, 133);
this.userControl11[i].TabIndex = 0;
this.Controls.Add(this.userControl11[i]);
}
;