求这段代码的问题,我想实现图片循环轮换。
现在里面有2条记录,第一条的cj_names是"耐克",第二条的cj_names是"阿迪"。
现在这段代码的问题是:
1、图片不循环,显示两张之后就停止了。
2、label的名字第一张停止的显示 “阿”,第二张停止的显示“迪”。应该是第一张停止显示“耐克”,第二张显示“阿迪”。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string sql_str = "select * from cj_main";
SqlConnection conn = new SqlConnection(conn_str);
SqlCommand command = new SqlCommand(sql_str, conn);
conn.Open();
SqlDataReader rs = command.ExecuteReader();
}
string conn_str = "Server=WIN_TEST;Database=choujiang;uid=sa;pwd=dsd_dsdn";
string cj_names;
string cj_pic_path;
int i = 0;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
timer1.Enabled = true;
label1.Visible = false;
label2.Visible = false;
label3.Visible = true;
}
if (e.KeyCode == Keys.Enter)
{
timer1.Enabled = false;
label1.Visible = true;
label3.Visible = false;
}
if (e.KeyData == Keys.Escape) Application.Exit();
{
}
} private void timer1_Tick(object sender, EventArgs e)
{
string sql_str = "select * from cj_main";
SqlConnection conn = new SqlConnection(conn_str);
SqlCommand command = new SqlCommand(sql_str, conn);
conn.Open();
SqlDataReader rs = command.ExecuteReader();
List<Image> lst = new List<Image>();
List<string>names = new List<string>();
while(rs.Read())
{
cj_names = rs[1].ToString();
cj_pic_path = rs[3].ToString();
lst.Add(Image.FromFile(cj_pic_path));
names.Add("恭喜您抽中了:"+cj_names);
if (i >= lst.Count)
{
i = 0;
}
else
{
this.pictureBox1.Image = lst[i];
this.label1.Text = cj_names[i].ToString();
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
i = i + 1;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
}
}
现在里面有2条记录,第一条的cj_names是"耐克",第二条的cj_names是"阿迪"。
现在这段代码的问题是:
1、图片不循环,显示两张之后就停止了。
2、label的名字第一张停止的显示 “阿”,第二张停止的显示“迪”。应该是第一张停止显示“耐克”,第二张显示“阿迪”。using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string sql_str = "select * from cj_main";
SqlConnection conn = new SqlConnection(conn_str);
SqlCommand command = new SqlCommand(sql_str, conn);
conn.Open();
SqlDataReader rs = command.ExecuteReader();
}
string conn_str = "Server=WIN_TEST;Database=choujiang;uid=sa;pwd=dsd_dsdn";
string cj_names;
string cj_pic_path;
int i = 0;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
timer1.Enabled = true;
label1.Visible = false;
label2.Visible = false;
label3.Visible = true;
}
if (e.KeyCode == Keys.Enter)
{
timer1.Enabled = false;
label1.Visible = true;
label3.Visible = false;
}
if (e.KeyData == Keys.Escape) Application.Exit();
{
}
} private void timer1_Tick(object sender, EventArgs e)
{
string sql_str = "select * from cj_main";
SqlConnection conn = new SqlConnection(conn_str);
SqlCommand command = new SqlCommand(sql_str, conn);
conn.Open();
SqlDataReader rs = command.ExecuteReader();
List<Image> lst = new List<Image>();
List<string>names = new List<string>();
while(rs.Read())
{
cj_names = rs[1].ToString();
cj_pic_path = rs[3].ToString();
lst.Add(Image.FromFile(cj_pic_path));
names.Add("恭喜您抽中了:"+cj_names);
if (i >= lst.Count)
{
i = 0;
}
else
{
this.pictureBox1.Image = lst[i];
this.label1.Text = cj_names[i].ToString();
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
i = i + 1;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
}
}
Timer调用位置?
SqlDataReader不用关闭?
private void Form1_Load(object sender, EventArgs e)
{
InitializeComponent();
string sql_str = "select * from cj_main";
using(SqlConnection conn = new SqlConnection(conn_str))
{
SqlCommand command = new SqlCommand(sql_str, conn);
conn.Open();
SqlDataReader rs = command.ExecuteReader();
List<string>names = new List<string>();
while(rs.Read())
{
cj_names = rs[1].ToString();
cj_pic_path = rs[3].ToString();
lst.Add(Image.FromFile(cj_pic_path));
}
rs.Close();}
}
timer1_Tick中获取随机数
ran.Next(0,lst.Count-1);
rs循环的第一次 是将i置0
rs循环的第二次 才是变图。
而变的图始终都是list[0]
其他都不想说了.