我创建了一个UserControl,上面有一个FlowLayoutPanel。我在运行时使用PictureBoxes的CollectionBase填充此面板。然后我在我的WindowsForm上有一个FlowLayoutPanel,我在运行时使用UserControls的CollectionBase填充。
现在,我希望能够访问UserControl中的自定义函数,以便将另一个PictureBox添加到instanciated(现有)UserControl的FlowLayoutPanel。如何访问该功能?
该功能在UserControl中是公共的,但是我无法通过CollectionBase来使用它。
以下是UserControl的代码:
public partial class UserDisplay : UserControl
{
public UserDisplay()
{
InitializeComponent();
myEquipArray = new ImageArray(flpnlTools);
}
public void AddEquip(string EquipName, Image EquipImage)
{
myEquipArray.AddNewEquip(EquipName, EquipImage);
}
//Accessor
public ControlCollection Equips
{
get
{
return flpnlEquips.Controls;
}
}
}
public class ImageArray : System.Collections.CollectionBase
{
private readonly System.Windows.Forms.FlowLayoutPanel HostPanel;
public ImageArray(FlowLayoutPanel hostPanel)
{
HostPanel = hostPanel;
}
public System.Windows.Forms.PictureBox AddNewEquip(string EquipName, Image EquipImage)
{
PictureBox pbA = new PictureBox();
this.List.Add(pbA);
HostPanel.Controls.Add(pbA);
pbA.Tag = EquipName;
pbA.Image = EquipImage;
return pbA;
}
public void RemoveEquip(string EquipName)
{
if (this.Count > 0)
{
int i = 0;
while (EquipName != HostPanel.Controls[i].Tag.ToString())
{
i++;
}
HostPanel.Controls.Remove(this[EquipName]);
this.List.RemoveAt(i);
}
}
public PictureBox this[string EquipName]
{
get
{
int i = 0;
while (EquipName != HostPanel.Controls[i].Tag.ToString())
{
i++;
}
return (PictureBox)this.List[i];
}
}
}
我使用此类在我的WindowsForm上实现UserConrol:
public class UserDsp : System.Collections.CollectionBase
{
private readonly System.Windows.Forms.FlowLayoutPanel HostPanel;
public DisplayControl.UserDisplay AddNewUser(Int64 UserID, string UserName, Image UserImage, string EquipName, Image EquipImage)
{
DisplayControl.UserDisplay newUser = new DisplayControl.UserDisplay();
this.List.Add(newUser);
HostPanel.Controls.Add(newUser);
newUser.Tag = UserID;
newUser.UserName = UserName;
newUser.UserImage = UserImage;
newUser.AddEquip(EquipName, EquipImage); //THIS ADDS THE PICTUREBOX CORRECTLY
return newUser;
}
public void AddNewEquip(Int64 UserID, Int64 EquipID, string EquipName, Image EquipImage)
{
if (this.Count > 0)
{
int i = 0;
while (UserID != Convert.ToInt64(HostPanel.Controls[i].Tag))
{
i++;
}
// Found the Control with UserID
HostPanel.Controls[i].AddEquip(EquipName, EquipImage); //THIS DOES NOT WORK
}
}
public UserDsp(System.Windows.Forms.FlowLayoutPanel hostPanel)
{
HostPanel = hostPanel;
}
public void RemoveUser(Int64 UserID)
{
if (this.Count > 0)
{
int i = 0;
while (UserID != Convert.ToInt64(HostPanel.Controls[i].Tag))
{
i++;
}
HostPanel.Controls.Remove(this[UserID]);
this.List.RemoveAt(i);
}
UsersLogdIn.Remove(UserID);
}
public DisplayControl.UserDisplay this[Int64 UserID]
{
get
{
int i = 0;
while (UserID != Convert.ToInt64(HostPanel.Controls[i].Tag))
{
i++;
}
return (DisplayControl.UserDisplay)this.List[i];
}
}
public List<long> usersLogdIn
{
get
{
return UsersLogdIn;
}
}
}
}
在这段代码中,我可以在实例化Control时在Control的FlowLayoutPanel中创建一个新的PictureBox:
newUser.AddEquip(EquipName, EquipImage); //THIS ADDS THE PICTUREBOX CORRECTLY
但是当我尝试通过CollectionBase访问它时:
public void AddNewEquip(Int64 UserID, Int64 EquipID, string EquipName, Image EquipImage)
{
if (this.Count > 0)
{
int i = 0;
while (UserID != Convert.ToInt64(HostPanel.Controls[i].Tag))
{
i++;
}
// Found the Control with UserID
HostPanel.Controls[i].AddEquip(EquipName, EquipImage); //THIS DOES NOT WORK
}
}
我也尝试将控件拉出来:
public void AddNewEquip(Int64 UserID, Int64 EquipID, string EquipName, Image EquipImage)
{
if (this.Count > 0)
{
int i = 0;
while (UserID != Convert.ToInt64(HostPanel.Controls[i].Tag))
{
i++;
}
// Found the Control with UserID
UserDisplay newUserDisplay = HostPanel.Controls[i] as UserDisplay //THIS DOES NOT WORK
}
}
我到处都寻求帮助,但没有发现任何相关内容。请帮助!
谢谢。
答案 0 :(得分:0)
我在一些外界的帮助下想出来了。
基本上我需要将控件强制转换为UserDisplay。我不知道为什么当我使用以下方法将其取出时它不起作用:
UserDisplay newUserDisplay = HostPanel.Controls[i] as UserDisplay
但我改用了这段代码:
(HostPanel.Controls[i] as UserDisplay).AddEquip(EquipName, EquipImage);
在我以这种方式声明之前,我确实确保控件是正确的类型:
if (HostPanel.Controls[i] is UserDisplay)
感谢您的帮助。