欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

3種方法實(shí)現(xiàn)WindowsForm切換窗口

 更新時(shí)間:2020年07月02日 09:01:38   作者:zhuanghamiao  
這篇文章主要介紹了3種方法實(shí)現(xiàn)WindowsForm切換窗口,文中講解非常詳細(xì),示例代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

在Windows Form平臺(tái)下實(shí)現(xiàn)窗口跳轉(zhuǎn),常見(jiàn)的有以下幾種方式,比如通過(guò)Show/ShowDialog方法顯示新的窗口,通過(guò)多文檔界面(MDI)在父窗口中加載子窗口,或者是通關(guān)過(guò)在窗口中動(dòng)態(tài)加載自定義控件,比如通過(guò)將窗口中的Panel控件作為容器,將一些自定義元素作為視圖界面控件,動(dòng)態(tài)加載到Panel控件中。下面我們將簡(jiǎn)單介紹這幾種方式

Show/ShowDialog

通過(guò)這兩種方法可以很簡(jiǎn)單的實(shí)現(xiàn)窗口跳轉(zhuǎn),

Home home = new Home();
home.Show();

Home home = new Home();
home.ShowDialog();

通過(guò)Show方法可以顯示非模式窗體,實(shí)際是將窗口設(shè)置為窗體的頂級(jí)對(duì)象。顯示窗體等效于將Visible屬性設(shè)置為true。 調(diào)用Show方法后,Visible屬性返回一個(gè)true值,直到調(diào)用Hide方法為止。
而ShowDailog方法就是將窗體顯示為一個(gè)模式對(duì)話框。只有將對(duì)話框關(guān)閉之后才會(huì)執(zhí)行之后的代碼。

Home home = new Home();
home.Show();
//home.ShowDialog();
Debug.WriteLine("彈出Home窗口");//使用Show方法時(shí),程序顯示窗口后會(huì)繼續(xù)執(zhí)行輸出代碼,而ShowDialog只有將顯示的窗口關(guān)閉后才會(huì)繼續(xù)執(zhí)行

MDI窗口

這種方式實(shí)際上是通過(guò)創(chuàng)建一個(gè)MDI父窗口容器,然后讓各個(gè)子窗口顯示到父窗口容器中。
首先我們需要將父窗口的IsMdiContainer屬性設(shè)置為T(mén)rue(確定是否將窗口設(shè)置為MDI容器),然后具體的跳轉(zhuǎn)代碼如下

// 加載登錄窗口
private void Form_Container_Load(object sender, EventArgs e)
{
 Login login = new Login();
 login.MdiParent = this;
 login.Show();
}

// 登錄實(shí)現(xiàn)
private void button1_Click(object sender, EventArgs e)
{
 // 賬號(hào)密碼驗(yàn)證
 // ...
 Home home = new Home();
 home.MdiParent = this.MdiParent;
 home.Show();
}

從上圖中,如果根據(jù)平時(shí)的一些軟件開(kāi)發(fā)需求,我們看到這樣的顯示是有一些問(wèn)題的,比如,登錄和主菜單頁(yè)面重疊顯示,子頁(yè)面帶有邊框等等。

在子窗口的加載事件中,增加窗口邊框的處理如下

private void Login_Load(object sender, EventArgs e)
{
 this.FormBorderStyle = FormBorderStyle.None;//將窗口設(shè)置無(wú)邊框模式,即去掉邊框和最大化等按鈕
 this.WindowState = FormWindowState.Maximized;//將子窗口設(shè)置為最大化,即在父容器中起到居中的效果
}

通過(guò)單例模式,創(chuàng)建一個(gè)全局容器實(shí)例,并提供一個(gè)公共的子窗口加載方法

static Form_Container instance;

public static Form_Container Instance
{
 get
 {
  if (instance == null)
  {
   instance = new Form_Container();
  }
  return instance;
 }
}

public void LoadChildPage(Form form)
{
 // 每次加載子窗口的時(shí)候關(guān)閉其他窗口
 foreach (var item in instance.MdiChildren)
 {
  item.Close();
 }
 form.MdiParent = instance;
 form.Show();
}

現(xiàn)在我們可以通過(guò)下面的方式進(jìn)行子頁(yè)面的切換

Form_Container.Instance.LoadChildPage(new Login());

Form_Container.Instance.LoadChildPage(new Home());

動(dòng)態(tài)加載子控件

這種方式的實(shí)現(xiàn)與MDI的方式類似,我們先創(chuàng)建UC_Loging和UC_Home兩個(gè)自定義控件作為子頁(yè)面;然后創(chuàng)建一個(gè)容器窗口Form_Container,在Form_Container中放一個(gè)Panel控件作為子頁(yè)面的容器,通過(guò)將自定義控件綁定到panel實(shí)現(xiàn)頁(yè)面切換的效果。

public partial class Form_Container : Form
{
 public Form_Container()
 {
  InitializeComponent();
 }

 static Form_Container instance;

 public static Form_Container Instance
 {
  get
  {
   if (instance == null)
   {
    instance = new Form_Container();
   }
   return instance;
  }
 }

 public Panel PnlContainer
 {
  get { return pnlContainer; }
  set { pnlContainer = value; }
 }

 public void LoadChildPage(Control control)
 {
  PnlContainer.Controls.Clear();
  PnlContainer.Controls.Add(control);
 }

 private void Form_Container_Load(object sender, EventArgs e)
 {
  Form_Container.Instance.LoadChildPage(new UC_Login());
 }
}

以上就是在Windows Form程序開(kāi)發(fā)過(guò)程中常見(jiàn)的頁(yè)面切換的方式。

到此這篇關(guān)于3種方法實(shí)現(xiàn)WindowsForm切換窗口的文章就介紹到這了,更多相關(guān)WindowsForm切換窗口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論