根据文本框中的值制作图片框
本文关键字:文本 | 更新日期: 2023-09-27 18:33:51
大家好!我有一个程序,可以计算在我的数据库中注册的职位列中的所有总统。现在我数了数并将其放入我的文本框中。我想根据文本框中的值制作动态图片框。我该怎么做?
这是我现在的代码:
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();
}
}
首先,你需要一个图像。
var image = new Image.FromFile(@"pathToImage");
然后设置图片框的图像。
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;
你可以随心所欲地建立这条道路,但希望你能明白这一点。
我现在知道答案了。希望它对你有用..
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();
};
我希望它对^^有所帮助