0

大家好!我有一个程序可以计算数据库中注册的职位列中的所有总统。现在我数了数并将其放入我的文本框中。我想根据我的文本框中的值制作一个动态图片框。我该怎么做?

这是我现在的代码:

    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();
        }
    }
4

2 回答 2

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

我希望它有所帮助^^

于 2013-10-03T03:34:19.723 回答
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;

您可以随心所欲地构建该路径,但希望您能明白这一点。

于 2013-10-03T00:54:16.073 回答