根据文本框中的值制作图片框

时间:2013-10-03 00:45:22

标签: c# dynamic picturebox

大家好!我在我的数据库中注册的位置栏中有计算所有总统的程序。现在我把它计算在一起并放入我的文本框中。我想根据文本框中的值制作动态图片框。我该怎么做?

现在是我的代码:

    SqlConnection sc = new SqlConnection(@"Data Source=PYTHON-PC\PYTHON;Initial Catalog=DBVote;Persist Security Info=True;User ID=sa;Password=1234");
    SqlCommand cmd;

    public FVotingArea()
    {
        InitializeComponent();
    }

    DataTable dt = new DataTable();
    PictureBox pb = new PictureBox();
    RadioButton rb = new RadioButton();

    private void FVotingArea_Load(object sender, EventArgs e)
    {
        sc.Open();
        try
        {
            cmd = new SqlCommand("SELECT COUNT(Position) FROM TableVote WHERE Position='" + "President" + "'", sc);
            Int32 count = (Int32)cmd.ExecuteScalar();

            TxtCount.Text = count.ToString();

            pb.Parent = this;
            pb.BorderStyle = BorderStyle.FixedSingle;
            pb.BringToFront();
            pb.Location = new System.Drawing.Point(100, 50);
            pb.Size = new System.Drawing.Size(112, 86);
            pb.Name = "PresPicBox";
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }
    }

我存储的图像如下:

    public void Add()
    {
        sc.Open();
        try
        {
        cmd = new SqlCommand("INSERT INTO TableVote (Position, FirstName, MiddleName, LastName, YearLevel, Course, SchoolYear, imgPath, imgImage) VALUES (@position, @firstname, @middlename, @lastname, @yearlevel, @course, @schoolyear, @imgpath, '" + _pb + "')", sc);
        Param();

            int res = cmd.ExecuteNonQuery();
            if(res > 0)
            {
                MessageBox.Show("Data Stored Successfully!");
                FAdminSet._cleardata = cleardata;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            sc.Close();
        }
    }

2 个答案:

答案 0 :(得分:0)

首先你需要一张图片。

var image = new Image.FromFile(@"pathToImage");

然后设置PictureBox的图像。

pb.Image = image;

根据条件来做:

var imagePath = @"c:/";
if (count == 1)
    imagePath += "whatever";
else if (count == 2)
    imagePath += "whatever2";
else
    imagePath += "whatever3";

var image = new Image.FromFile(imageName);
pb.Image = image;

您可以根据需要构建该路径,但希望您能看到这一点。

答案 1 :(得分:0)

我现在知道答案了。希望它对你有用..

    TxtCount.Text = count.ToString();

            for (int i = 0; i < count; ++i)
            {
                PictureBox pb = new PictureBox();
                pb.Location = new Point((80 * i) + 5, 30);
                pb.Size = new Size(75, 75);
                pb.BorderStyle = BorderStyle.Fixed3D;
                pb.ImageLocation = "";
                this.Controls.Add(pb);
                pb.BringToFront();
            };

我希望它能帮助^^