简体   繁体   中英

retrieving data from database with dynamic controls

I have made a voting system for our school. I already made an Add, Update, Delete, and retrieving data from database. I have a problem making a dynamic control automatically with the exact amount of data i have in database. For example, I have added pictures to the two Candidates in the position of President, then, I want it to dynamically create a PictureBox in my new form and retrieve the pictures in a 2 pictureboxes and radiobutton for their names under it. Is it possible for me to do it in array? I'm new to programming so bear with me please.

I'm a little bit confused. Can you make an example of it, if possible please.. :)

This should get you going.

Add this code to your form. And you can use this code for any buttons or anything you want. But you should maybe read up on the FlowLayoutPanel or GroupBox to get this to work in reality.

 Point _imagePos = new Point(10,10);
    int _imageCounter = 1;
    private void NewPictureBox(string pathToImg, string imageName)
    {
        var img = new PictureBox 
            { 
                Name = "imageBox" + _imageCounter, 
                ImageLocation = pathToImg, 
                Left = _imagePos.X, 
                Top = _imagePos.Y,
                SizeMode = PictureBoxSizeMode.StretchImage,
                Height = 50,
                Width = 50
            };
        var txt = new TextBox
            {
                Text = imageName,
                Left = _imagePos.X,
                Top = img.Bottom + 10
            };
        this.Controls.Add(img);
        this.Controls.Add(txt);
        _imageCounter++;
        _imagePos.Y += 10 + img.Height + txt.Height;           
    }
    private void Form1_Load(object sender, EventArgs e)
    {

        NewPictureBox(@"C:\test\QuestionMark.jpg", "image1");
        NewPictureBox(@"C:\test\QuestionMark.jpg", "image2");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM