Reputation: 187
I am an old time user of the Array of controls in VB back in the days. It is gone now, so I am looking for a replacement.
I have created an User Control in C# for Silverlight. This control exposes a bool property IsChecked.
<ImageButton Name"imgbutton1" IsChecked="True"/>
<ImageButton Name"imgbutton2" IsChecked="False"/>
<ImageButton Name"imgbutton3" IsChecked="False"/>
In a Child Window, I populate a series of these controls and use them as an equivalent of RadioButtons, i.e., when I click one, any other that has an IsChecked = true will be set to false, for which I thought of doing something like:
List<ImageButton> imgButtons= new List<ImageButton>();
imgButtons.Add(imgbutton1);
imgButtons.Add(imgbutton2);
imgButtons.Add(imgbutton3);
Then, to clear all of their IsChecked but the one I need (pointed by some 'index' variable), I would do something like:
foreach (ImageButton imgbutton in imgButtons) imgbutton.IsChecked = false;
imgbuttons[index].IsChecked = true;
The problem I have is that List<ImageButton>
is not compiling. I am very flaky at collections and can't figure out what I am missing. I can do for standard controls but doesn't allow me to get a user control in there.
Thanks!
PS: I have thought of just customizing a RadioButton control but don't have Blend and I am using this image control for other types of controls. However, if you think there is a better way to implement this, please let me know.
EDIT: Compiler says "The type or namespace name 'ImageButton' could not be found (are you missing a using directive or an assembly reference?)'
Upvotes: 0
Views: 3859
Reputation: 360
This may help.
The code for user control:
//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;
}
}
now in the form totally dynamically can create an array of that user control like this
A global variable
private UserControl1[] userControl11=new WindowsFormsApplication1.UserControl1[3];
and now the array of user control
you can write it on a button
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]);
}
;
Upvotes: -1
Reputation: 4076
Just thought of something else, I'm not sure why you are keeping a List<> of your ImageButtons if they are in the display (in that childwindow). If they are all within the same container your could use something like :
// 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;
Upvotes: 1
Reputation: 4076
Ensure that you have a reference to your "ImageButton" control in your code (in the "using" part).
To do that automatically you can just rightclick on your "ImageButton" in your code and click "Resolve", it will automatically add the Reference
Upvotes: 3
Reputation: 22719
You didn't tell us what the compiler error was when you said "List is not compiling".
My guess is that you need to include the namespace of the List collection in your file.
using System.Collections.Generic;
Upvotes: 2