質問

I created a UserControl that has a FlowLayoutPanel on it. I populate this panel in runtime with a CollectionBase of PictureBoxes. I then have a FlowLayoutPanel on my WindowsForm that I populate in runtime using a CollectionBase of my UserControls.

Now, I want to be able to access a custom function within my UserControl in order to add another PictureBox to an instanciated (existing) UserControl's FlowLayoutPanel. How do I access the function to do this?

The function is public within the UserControl but I am unable to get through the CollectionBase in order to use it.

Here is my code for the 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];
        }
    }
}

I use this Class to implement the UserConrol on my WindowsForm:

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

In this code I can create a new PictureBox in the Control's FlowLayoutPanel when instantiating the Control:

            newUser.AddEquip(EquipName, EquipImage);  //THIS ADDS THE PICTUREBOX CORRECTLY

But when I try to access it through the 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
            }
        }

I also tried to pull the control out like this:

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

I have looked everywhere for help on this but have not found anything related. Please Help!

Thank you.

役に立ちましたか?

解決

I figured it out with some outside help.

Basically I need to typecast the control as UserDisplay. I don't know why it didn't work when I pulled it out using:

UserDisplay newUserDisplay = HostPanel.Controls[i] as UserDisplay

but I used this code instead:

(HostPanel.Controls[i] as UserDisplay).AddEquip(EquipName, EquipImage);

I did make sure that the control was the correct type before I declared it so this way:

if (HostPanel.Controls[i] is UserDisplay)

Thanks for the help.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top