问题遇到的现象和发生背景
做一个选择教师的功能,一页有4个panel,代表4个教师,每个panel容器里有4个不赋值的label和4个需要赋值的label,后者代表4个教师个人的信息;
在界面初始化时,控件循环生成所有控件panel和其中的label,一页展示4个,点击下一页,前4个panel设置为不可见,后4个设置为可见。但是我点击下一页时,前4个panel确实是不可见了,但是后4个也没出来,界面光秃秃的。
调试了一边,发现控件确实是生成所有的教师panel,但是为什么调用不出来?
问题相关代码,请勿粘贴截图
namespace loginStudent
{
public partial class select_Tea : Form
{
public select_Tea()
{
InitializeComponent();
}
public bool change;
public int count = 0;
public int pages;
public int i = 0;
public int temp = 0;
int times = 0;
public string[,] teainfo = null;
int AllTeacher;
int panelNo = 1, labelNo = 1, checkNo = 1;
string labelName = "label";
int tempPoint = 0;
//bool AlrRuning = false;
private void select_Tea_Load(object sender, EventArgs e)//窗体初始化就加载第一页panel控件
{
change = false;
comboBox1.SelectedIndex = 0;
change = true;
Models.TeaInfo.Academy = comboBox1.Text;
pages = 1;
BLL.Logical b_user = new BLL.Logical();
string[,] TeaInfo = b_user.TeaInfo();
AllTeacher = TeaInfo.Length/4;
times = AllTeacher;
Addpanel();//**生成所有控件**
PagesChanged(pages);//对panel里的label赋值
//for(int i = 5; i<AllTeacher; i++)
//{
// ((Panel)this.Controls.Find(("panel" + i), true)[0]).Visible = false;
//}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (change == true)
{
for (int i = 1; i <= AllTeacher; i++)
{
((Panel)this.Controls.Find("panel" + i, true)[0]).Visible = false;
}
Models.TeaInfo.Academy = comboBox1.Text;
times = teainfo.Length / 4;
Addpanel();
PagesChanged(pages);
}
}
public void PagesChanged(int pages)//对panel里的label赋值
{
BLL.Logical b_user = new BLL.Logical();
teainfo = b_user.TeaInfo();
int alrShow = pages * 4;
AllTeacher = teainfo.Length / 4;
for (int i = alrShow; i < AllTeacher+alrShow; i++)
{
((Label)this.Controls.Find("lab_TeaName" +(i-3), true)[0]).Text = teainfo[i-4, 0];
((Label)this.Controls.Find("lab_TeaNo" + (i-3), true)[0]).Text = teainfo[i-4, 1];
((Label)this.Controls.Find("lab_Research" + (i-3), true)[0]).Text = teainfo[i-4, 2];
((Label)this.Controls.Find("lab_Contact" + (i-3), true)[0]).Text = teainfo[i-4, 3];
}
}
public void Addpanel()//生成控件(panel和里面的label)
{
for (int i = 0; i < times; i ++)//每一次for循环只生成一个panel
{
Panel panel = new Panel();
panel.Name = "panel" + panelNo;
panel.Width = 221;
panel.Height = 297;
panel.BackColor = SystemColors.Control;
int point = tempPoint + 6;
panel.Location = new Point(point, 3);
tempPoint = point + 221;
Base.Controls.Add(panel);
panel.BringToFront();
Label label1 = new Label();
label1.Name = labelName + labelNo;
labelNo++;
label1.Text = "教师姓名:";
label1.Font = new Font("微软雅黑", 10.5f);
panel.Controls.Add(label1);
label1.BringToFront();
label1.Location = new Point(6, 10);
Label label2 = new Label();
label2.Name = labelName + labelNo;
labelNo++;
label2.Text = "教师编号:";
label2.Font = new Font("微软雅黑", 10.5f);
panel.Controls.Add(label2);
label2.BringToFront();
label2.Location = new Point(6, 65);
Label label3 = new Label();
label3.Name = labelName + labelNo;
labelNo++;
label3.Text = "研究方向:";
label3.Font = new Font("微软雅黑", 10.5f);
panel.Controls.Add(label3);
label3.BringToFront();
label3.Location = new Point(6, 120);
Label label4 = new Label();
label4.Name = labelName + labelNo;
labelNo++;
label4.Text = "联系方式:";
label4.Font = new Font("微软雅黑", 10.5f);
panel.Controls.Add(label4);
label4.BringToFront();
label4.Location = new Point(6, 176);
Label labelr1 = new Label();
labelr1.Name = "lab_TeaName" + panelNo;
labelr1.Text = "null";
labelr1.Font = new Font("微软雅黑", 10.5f);
panel.Controls.Add(labelr1);
labelr1.BringToFront();
labelr1.Location = new Point(77, 10);
Label labelr2 = new Label();
labelr2.Name = "lab_TeaNo" + panelNo;
labelr2.Text = "null";
labelr2.Font = new Font("微软雅黑", 10.5f);
panel.Controls.Add(labelr2);
labelr2.BringToFront();
labelr2.Location = new Point(77, 65);
Label labelr3 = new Label();
labelr3.Name = "lab_Research" + panelNo;
labelr3.Text = "null";
labelr3.Font = new Font("微软雅黑", 10.5f);
panel.Controls.Add(labelr3);
labelr3.BringToFront();
labelr3.Location = new Point(77, 120);
Label labelr4 = new Label();
labelr4.Name = "lab_Contact" + panelNo;
labelr4.Text = "null";
labelr4.Font = new Font("微软雅黑", 10.5f);
panel.Controls.Add(labelr4);
labelr4.BringToFront();
labelr4.Location = new Point(77, 176);
CheckBox checkbox = new CheckBox();
checkbox.Name = "check_Tea" + checkNo;
checkbox.Text = "选择该教师";
checkbox.Font = new Font("微软雅黑", 10.5f);
panel.Controls.Add(checkbox);
checkbox.BringToFront();
checkbox.Location = new Point(52, 250);
checkNo ++;
panelNo++;
}
}
public int[] check_x()
{
int[] check = new int[2];
int ActPages = teainfo.Length / 16; //**一共要装载的页数** 32/16 = 2(32是例子,表示有8个老师的4种信息8*4;16是一页4个老师的4种信息4*4) (一共装载两页)
int x = (ActPages - 1) * 4 + 1; //5 (表示下一页第一个panel的序号,如第二页是“panel5”)
check[0] = ActPages;
check[1] = x;
return check;//return只能返回一个值,为了返回页数“ActPages”和序号数“x”,用数组来return
}
private void btn_NextPage_Click(object sender, EventArgs e)//下一页
{
int[] check = new int[2];
check = check_x();
if (pages < check[0])
{
for (int i = 0; i < 4; i++)
{
//把上一页的4个panel设置不可见
((Panel)this.Controls.Find(("panel" + (check[1] + i - 4)), true)[0]).Visible = false;
//把下一页已经生成的4个panel设置为可见
((Panel)this.Controls.Find(("panel" + (check[1] + i)), true)[0]).Visible = true ;
}
pages++;
}
}
private void btn_PreviousPage_Click(object sender, EventArgs e)
{
int[] check = new int[2];
check = check_x();
if (pages > 1)
{
for (int i = 0; i < 4; i++)
{
((Panel)this.Controls.Find(("panel" + (check[1] + i)), true)[0]).Visible = false;
((Panel)this.Controls.Find(("panel" + (check[1] - 4 + i)), true)[0]).Visible = true;
}
pages--;
}
}