关于C#中的选项卡的问题 tabcontrol
我做了一个窗体From1,然后这个窗体上面有两个按钮,一个是Btn_A,一个是Btn_B,然后我又做了另外一个窗体From2,在上面做了一个选项卡,选项卡里有两个tabP...
我做了一个窗体From1,然后这个窗体上面有两个按钮,一个是Btn_A,一个是Btn_B,然后我又做了另外一个窗体From2,在上面做了一个选项卡,选项卡里有两个tabPage,一个是tabPage1,一个是tabPage2。然后我现在想让它点击From1中的Btn_A时,打开From2,并且显示在tabPage1,然后点击From1的Btn_B时,打开From2,并且显示在tabPage2上,这应该怎么做呢?最好能详细点。
帮忙回答下啊 展开
帮忙回答下啊 展开
展开全部
//这是MainForm类:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace testWinForm
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void BtnAClick(object sender, EventArgs e)
{
Form2 frm2=new Form2(0);
frm2.ShowDialog();
}
void BtnBClick(object sender, EventArgs e)
{
Form2 frm2=new Form2(1);
frm2.ShowDialog();
}
}
}
============下面是Form2类=================
using System;
using System.Drawing;
using System.Windows.Forms;
namespace testWinForm
{
/// <summary>
/// Description of Form2.
/// </summary>
public partial class Form2 : Form
{
public Form2(int index)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
if(index >= 0 && index < tabControl1.TabPages.Count)
{
tabControl1.SelectedIndex=index;
}
}
}
}
==========================================
如果不想每次都新建一个Form2的实例,可以在MainForm类中添加一个字段(成员变量),字段类型为Form2,然后为Form2添加一个public 方法来更改选取哪一页。
================下面是修改后的MainForm类==============
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace testWinForm
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
private Form2 frm2;
void BtnAClick(object sender, EventArgs e)
{
if(frm2==null)frm2=new Form2(0);
frm2.SelectTabPage(0);
frm2.ShowDialog();
}
void BtnBClick(object sender, EventArgs e)
{
if(frm2==null)frm2=new Form2(0);
frm2.SelectTabPage(1);
frm2.ShowDialog();
}
}
}
===========下面是修改后的Form2类==================
using System;
using System.Drawing;
using System.Windows.Forms;
namespace testWinForm
{
/// <summary>
/// Description of Form2.
/// </summary>
public partial class Form2 : Form
{
public Form2(int index)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
if(index >= 0 && index < tabControl1.TabPages.Count)
{
tabControl1.SelectedIndex=index;
}
}
public void SelectTabPage(int index)
{
if(index >= 0 && index < tabControl1.TabPages.Count)
{
this.tabControl1.SelectedIndex=index;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace testWinForm
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void BtnAClick(object sender, EventArgs e)
{
Form2 frm2=new Form2(0);
frm2.ShowDialog();
}
void BtnBClick(object sender, EventArgs e)
{
Form2 frm2=new Form2(1);
frm2.ShowDialog();
}
}
}
============下面是Form2类=================
using System;
using System.Drawing;
using System.Windows.Forms;
namespace testWinForm
{
/// <summary>
/// Description of Form2.
/// </summary>
public partial class Form2 : Form
{
public Form2(int index)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
if(index >= 0 && index < tabControl1.TabPages.Count)
{
tabControl1.SelectedIndex=index;
}
}
}
}
==========================================
如果不想每次都新建一个Form2的实例,可以在MainForm类中添加一个字段(成员变量),字段类型为Form2,然后为Form2添加一个public 方法来更改选取哪一页。
================下面是修改后的MainForm类==============
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace testWinForm
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
private Form2 frm2;
void BtnAClick(object sender, EventArgs e)
{
if(frm2==null)frm2=new Form2(0);
frm2.SelectTabPage(0);
frm2.ShowDialog();
}
void BtnBClick(object sender, EventArgs e)
{
if(frm2==null)frm2=new Form2(0);
frm2.SelectTabPage(1);
frm2.ShowDialog();
}
}
}
===========下面是修改后的Form2类==================
using System;
using System.Drawing;
using System.Windows.Forms;
namespace testWinForm
{
/// <summary>
/// Description of Form2.
/// </summary>
public partial class Form2 : Form
{
public Form2(int index)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
if(index >= 0 && index < tabControl1.TabPages.Count)
{
tabControl1.SelectedIndex=index;
}
}
public void SelectTabPage(int index)
{
if(index >= 0 && index < tabControl1.TabPages.Count)
{
this.tabControl1.SelectedIndex=index;
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询