c#窗体隐藏到任务栏后,再显示~~~

gcdreams 2008-03-19 10:03:08
两个窗体form1,form2.m form1最小化隐藏到任务栏后,通过form2按钮使其显示。

使用
form1 f=new form1();
f.show();

后会重新启动一个form1(也就是说变成两个form1),怎样才能使原来的form1显示出来?谢谢!
...全文
398 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xray2005 2008-03-19
  • 打赏
  • 举报
回复
注:
public Form2 frm { get; set; }

是C#3.0属性的写法. 如果你是C#2.0,C#1.1那么请改成传统的写法
xray2005 2008-03-19
  • 打赏
  • 举报
回复

Form1代码:

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public Form2 frm { get; set; }

private void button1_Click(object sender, EventArgs e)
{
if (this.frm == null)
this.frm = new Form2();
this.frm.Show();
this.Hide();
}
}
}



Form2代码:

namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

public Form1 frm { get; set; }
private void button1_Click(object sender, EventArgs e)
{
if (this.frm == null)
this.frm = new Form1();
this.frm.Show();
this.Hide();
}
}
}
dancingbit 2008-03-19
  • 打赏
  • 举报
回复

form2定义一个属性Form OtherForm,在生成form2的地方:

Form2 form2=new Form2();
form2.OtherForm=this;
form2.ShowDialog();//或者是Show

然后在form2的按钮事件中:
OtherForm.Show();
growleaf 2008-03-19
  • 打赏
  • 举报
回复
你的form1是否为程序的启动窗体呀,如果是的话
你就在program的main方法中修改一下,不过在之前,你需要在form2的窗体中加入一个form1的静态属性(public static Form1 fm1 = null)

然后在修改Main函数如:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 fm1 = new Form1();
Form2.fm1 = fm1 ;
Application.Run(fm1 );
}

最后你在form2窗体中显示form1的时候,只需要调用fm1.show();
gcdreams 2008-03-19
  • 打赏
  • 举报
回复
dancingbit ,谢谢!新手能方便写出代码吗?谢谢
dancingbit 2008-03-19
  • 打赏
  • 举报
回复
form2需要保持对form1的引用,通过此引用来Show
new肯定是会生成一个新窗体的了

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧