在一個form中,如果輸入項太多,那麼最好不要都堆放在一個界面中,比較好的辦法是讓用戶分步填寫(比如很多招聘網站讓用戶填寫信息的方式都是這樣的)。在TabControl(.Net2.0)的基礎上,我把每一個tabPage作為一頁,通過實現一些特定的事件處理函數,達到了一個簡單翻頁效果。實現很簡單,不對的地方還請大家多指教。
界面如下:
代碼實現如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
...{
public partial class Form1 : Form
...{
private delegate void ChangePageHandler(); // 翻頁函數代理
private int currentIndex = 0; // 記錄當前選項卡索引
private bool changeLock = true; // 頁面切換鎖,如果為true,那麼禁止切換tabpage
public Form1()
...{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
...{
this.currentIndex = tabControl1.SelectedIndex;
}
/**//// <summary>
/// 選擇進入下一個選項卡
/// </summary>
private void SelectNextTabPage()
...{
int count = tabControl1.TabPages.Count;
currentIndex = this.tabControl1.SelectedIndex + 1;
if (currentIndex > count - 1)
...{
currentIndex = count - 1;
}
this.tabControl1.SelectTab(currentIndex);
}
/**//// <summary>
/// 選擇退回到上一個選項卡
/// </summary>
private void SelectPreviousTabPage()
...{
currentIndex = this.tabControl1.SelectedIndex - 1;
if (currentIndex < 0)
...{
currentIndex = 0;
}
this.tabControl1.SelectTab(currentIndex);
}
/**//// <summary>
/// 阻止用戶通過點擊選項卡來切換tabpage
/// </summary>
&nbs /// <param name="sender"></param>
/// <param name="e"></param>
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
...{
e.Cancel = changeLock;
}
/**//// <summary>
/// 代理翻頁
/// </summary>
/// <param name="handler">具體的翻頁函數</param>
private void ChangePage(ChangePageHandler handler)
...{
this.changeLock = false;
handler();
this.changeLock = true;
}
/**//// <summary>
/// 上一步
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonPreviousStep1_Click(object sender, EventArgs e)
...{
this.ChangePage(this.SelectPreviousTabPage);
}
/**//// <summary>
/// 下一步
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonNextStep1_Click(object sender, EventArgs e)
...{
this.ChangePage(this.SelectNextTabPage);
}
}
}
<!--[if !vml]--><!--[endif]-->