0

在这里,我正在做一个以图像形式呈现问题的项目。当项目加载时,“开始考试”按钮将出现在屏幕上。按下按钮后,它应该为来自指定路径的每个图像创建一个picturebox、一个textbox和一个buttontextbox然后用户必须在动态创建的答案中输入答案。button在为每个图像单击动态提交后,textbox必须将值存储在listbox. 我不知道如何从中获取值textbox。谁能帮我解决这个问题?

这是我的代码:

PictureBox[] pics = new PictureBox[100];
TextBox[] txts = new TextBox[100];
Button[] butns = new Button[100];
FlowLayoutPanel[] flws = new FlowLayoutPanel[100];

private void button1_Click( Object sender , EventArgs e)
{
    for (int i = 0; i < listBox1.Items.Count; i++)
    {
        flws[i] = new FlowLayoutPanel();
        flws[i].Name = "flw" + i;
        flws[i].Location = new Point(3,brh);
        flws[i].Size = new Size(317,122);
        flws[i].BackColor = Color.DarkCyan;
        flws[i].BorderStyle = BorderStyle.Fixed3D;
        pics[i] = new PictureBox();
        pics[i].Location = new Point(953, 95 + brh);
        pics[i].Name = "pic" + i;
        pics[i].Size = new Size(300, 75);
        pics[i].ImageLocation = "C:/" + listBox1.Items[i];
        flws[i].Controls.Add(pics[i]);
        txts[i] = new TextBox();
        txts[i].Name = "txt" + i;
        txts[i].Location = new Point(953, 186 + brh);
        flws[i].Controls.Add(txts[i]);
        butns[i] = new Button();
        butns[i].Click += new EventHandler(butns_Click);
        butns[i].Text = "submit";
        butns[i].Name = "but" + i;
        butns[i].Location = new Point(1100, 186 + brh);
        flws[i].Controls.Add(butns[i]);
        flowLayoutPanel1.Controls.Add(flws[i]);
        brh += 130;
    }
}

private void butns_Click(object sender, EventArgs e)
{
    Button butns = sender as Button;
    TextBox txts = sender as TextBox; 
    listBox2.Items.Add("text values " + txts.Text.ToString());
}
4

2 回答 2

0

I would create a usercontrol to combine the controls.

Search for "custom usercontrol c#"

Regards.

于 2013-07-31T07:51:13.397 回答
0

尝试这个...

private void butns_Click(object sender, EventArgs e)
{
    Button butns = sender as Button;
    string btnName = butns.Name;
    string Id = btnName.Substring(3);
    string txtName = "txt" + Id;
    listBox2.Items.Add("text values " + GetValue(txtName));
}

private string GetValue(string name)
{
    TextBox txt = new TextBox();
    txt.Name = name;
    foreach (Control ctl in this.Controls)
    {
        if (ctl is FlowLayoutPanel)
        {
            foreach (Control i in ctl.Controls)
            {
                if (((TextBox)i).Name == txt.Name)
                {
                    txt = (TextBox)i;
                    return txt.Text;
                }
            }
        }
    }
    return txt.Text;
}
于 2013-07-31T07:59:26.850 回答