C# 如何使用for循环单选按钮显示数据库中的图像或信息?在c中#
大家好我在程序中显示图像和全名等信息时遇到问题。我有一个for循环单选按钮,它计算有多少候选人在一个特定的职位上竞选。例如“总统” 以下是我目前的代码:C# 如何使用for循环单选按钮显示数据库中的图像或信息?在c中#,c#,winforms,sql-server-2008,C#,Winforms,Sql Server 2008,大家好我在程序中显示图像和全名等信息时遇到问题。我有一个for循环单选按钮,它计算有多少候选人在一个特定的职位上竞选。例如“总统” 以下是我目前的代码: cmd = new SqlCommand("SELECT COUNT(Position) FROM TableVote WHERE Position='" + "President" + "'", sc); Int32 PresCount = (Int32)cmd.ExecuteScalar();
cmd = new SqlCommand("SELECT COUNT(Position) FROM TableVote WHERE Position='" + "President" + "'", sc);
Int32 PresCount = (Int32)cmd.ExecuteScalar();
TxtPresCount.Text = PresCount.ToString();
//int lol = Convert.ToInt32(TxtPresCount.Text);
var panel = new FlowLayoutPanel();
panel.SuspendLayout();
panel.Size = new Size(600, 150);
panel.Location = new Point(50, 50);
panel.FlowDirection = FlowDirection.LeftToRight;
panel.AutoScroll = true;
panel.WrapContents = false;
this.Controls.Add(panel);
for (int i = 0; i < PresCount; ++i)
{
var radioButton = new RadioButton();
radioButton.Size = new Size(75, 75);
radioButton.CheckAlign = ContentAlignment.MiddleCenter;
radioButton.ImageAlign = ContentAlignment.MiddleCenter;
panel.Controls.Add(radioButton);
//radioButton.Image = Image.FromFile();
radioButton.ImageAlign = ContentAlignment.MiddleCenter;
radioButton.FlatStyle = FlatStyle.Flat;
}
panel.ResumeLayout();
这是我的数据库:
public partial class Form1 : Form {
public Form1(){
InitializeComponent();
//do this if you want to register the Load event handler using code
Load += Form1_Load;
}
FlowLayoutPanel panel = new FlowLayoutPanel();
private void InitPanel(){
panel.Size = new Size(600, 150);
panel.Location = new Point(50, 50);
panel.FlowDirection = FlowDirection.LeftToRight;
panel.AutoScroll = true;
panel.WrapContents = false;
Controls.Add(panel);
}
//Load event handler
private void Form1_Load(object sender, EventArgs e){
InitPanel();
panel.SuspendLayout();
string cmdText = "SELECT (FirstName + ' ' + MiddleName + ' ' + LastName) as FullName, " +
"imgPath as ImagePath FROM TableVote WHERE Position='President'";
using(SqlCommand com = new SqlCommand(cmdText,sc)){
if(sc.State != ConnectionState.Open) sc.Open();
SqlDataReader reader = com.ExecuteReader();
while(reader.Read()){
AddRadioButton(reader.GetString(0), reader.GetString(1));
}
reader.Close();
sc.Close();
panel.ResumeLayout(true);
}
}
private void AddRadioButton(string fullName, string imagePath){
RadioButton radio = new RadioButton {Text = fullName, Parent = panel};
radio.AutoSize = true;
radio.Image = new Bitmap(Image.FromFile(imagePath),75,75);
radio.TextImageRelation = TextImageRelation.ImageAboveText;
radio.CheckAlign = ContentAlignment.BottomCenter;
}
}
注意:我可以看到您在表中存储了两个涉及图像的信息,我认为您应该选择其中一个,存储
图像路径
很容易,您的表格重量较轻,但如果您的图像路径
不再指向实际图像,则信息可能会丢失。您希望在单选按钮中输入哪些信息?单选按钮只能容纳一些像标签一样的文本,区别在于它允许一些交互来做出选择。例如图像、候选人全名等信息。有人告诉我,它可以使用单选按钮保存图像。Image
。但我不知道具体怎么做。你从哪里获取图像?SQLServer2008。数据类型为Image
。它是否存储在表TableVote
中?您应该显示包含总统候选人
所有信息的表格,包括全名
和图像
,我没有看到您发布的任何代码显示如何获取全名
和图像
基于我的前几个代码,如何将该代码合并到您的代码中?或者我应该删除我的代码并替换为你的代码?@LyndonBrozTonelete现在你可以使用我的代码了,初始化面板的代码保持不变。@LyndonBrozTonelete由你决定,以的形式加载就可以了。注意,AddRadioButton
是一个方法,你必须把它放在表单类中。对不起,我有点搞不清楚我要把它放在哪里。我不会再使用我的循环了吗?@LyndonBrozTonelete不,你的循环需要知道候选人的数量,我发布的代码就足够了,把它放在你的表单加载中,如果有任何异常,请告诉我。。。
public partial class Form1 : Form {
public Form1(){
InitializeComponent();
//do this if you want to register the Load event handler using code
Load += Form1_Load;
}
FlowLayoutPanel panel = new FlowLayoutPanel();
private void InitPanel(){
panel.Size = new Size(600, 150);
panel.Location = new Point(50, 50);
panel.FlowDirection = FlowDirection.LeftToRight;
panel.AutoScroll = true;
panel.WrapContents = false;
Controls.Add(panel);
}
//Load event handler
private void Form1_Load(object sender, EventArgs e){
InitPanel();
panel.SuspendLayout();
string cmdText = "SELECT (FirstName + ' ' + MiddleName + ' ' + LastName) as FullName, " +
"imgPath as ImagePath FROM TableVote WHERE Position='President'";
using(SqlCommand com = new SqlCommand(cmdText,sc)){
if(sc.State != ConnectionState.Open) sc.Open();
SqlDataReader reader = com.ExecuteReader();
while(reader.Read()){
AddRadioButton(reader.GetString(0), reader.GetString(1));
}
reader.Close();
sc.Close();
panel.ResumeLayout(true);
}
}
private void AddRadioButton(string fullName, string imagePath){
RadioButton radio = new RadioButton {Text = fullName, Parent = panel};
radio.AutoSize = true;
radio.Image = new Bitmap(Image.FromFile(imagePath),75,75);
radio.TextImageRelation = TextImageRelation.ImageAboveText;
radio.CheckAlign = ContentAlignment.BottomCenter;
}
}