如何使用循环单选按钮显示数据库中的图像或信息?在 C# 中 [英] How to display images or information from database with a for looped radiobutton? in c#

查看:22
本文介绍了如何使用循环单选按钮显示数据库中的图像或信息?在 C# 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我在我的程序中显示我的图像和信息(例如全名)时出现问题.我有一个 for 循环单选按钮,它计算有多少候选人在特定位置上运行.例如总统".

Hello Everyone! I have a problem displaying my images and information such as full name in my program. I have a for looped radiobutton and it counts as to how many candidates are running in a specific position. Example is "President".

这是我目前的代码:

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

对于上面的代码,它统计了数据库中存储的所有总统.但我的问题是,如何放置存储在数据库中的图像/名称?例如,我有 5 位候选人竞选总统职位,我希望所有数据都在 5 个单选按钮中.我如何将信息放入单选按钮?请帮忙.:(

as for the codes above, it counts all the President that is stored in the database. But my problem is, how do i put an image/Name that is stored in the database? Example i have 5 candidates running for the position of President, I want all the data to be in 5 radiobutton. How do i put the information into the radiobutton? Please help. :(

我使用下面的代码添加信息:

I add information using this code below :

    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);
        cmd.Parameters.AddWithValue("@position", _position);
        cmd.Parameters.AddWithValue("@firstname", _firstname);
        cmd.Parameters.AddWithValue("@middlename", _middlename);
        cmd.Parameters.AddWithValue("@lastname", _lastname);
        cmd.Parameters.AddWithValue("@yearlevel", _yearlevel);
        cmd.Parameters.AddWithValue("@course", _course);
        cmd.Parameters.AddWithValue("@schoolyear", _schoolyear);
        cmd.Parameters.AddWithValue("@imgpath", _imgpath);

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

这是我的数据库:

推荐答案

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

注意:我可以看到您在表格中存储了 2 个涉及图像的信息,我认为您应该选择其中的 1 个,存储 Image Path 很容易,轻巧-表的重量,但如果您的 Image path 不再指向实际图像,则信息可能会丢失.

NOTE: I can see that you store 2 info involving images in your table, I think you should choose 1 of them, storing Image Path is easy, light-weight for your table but the info may be lost if your Image path hasn't pointed to the actual image anymore.

这篇关于如何使用循环单选按钮显示数据库中的图像或信息?在 C# 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆