如何在C#中以编程方式更改初始表单应用程序 [英] How to change initial form application programmatically in C#

查看:75
本文介绍了如何在C#中以编程方式更改初始表单应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4种不同的形式。很明显,我可以更改在以下行运行应用程序时将启动的表单:



Application.Run(new Form1()); //我不想改变这一行。



我想通过其他形式的编码改变新Form1部分。



例如:



首先,我运行应用程序并启动Form1。我点击按钮打开Form2。当我打开Form2时,Application.Run(new Form1())应该更改为Application.Run(new Form2())。因此,当我关闭整个应用程序并重新开始时,Form2将启动。在这种情况下,我将能够保存我最新的开放表格。





所以,

如何我可以通过编程方式更改Application.Run(new Form1())吗?



提前谢谢...

解决方案

不喜欢。相反,有一个始终从Application.Run打开的MasterForm。它的可见属性(并在任务栏属性中显示)设置为false。

它在应用程序配置文件中查找,并决定显示哪个表单(让我们说Form1)

它显示form1,并挂钩FormClosed事件以关闭它自己。

当Form1按下form2时,它发出MasterForm处理的Form2Please事件的信号。它关闭Form1,显示Form2,并在下次更改应用程序配置文件。

Quote:

好吧,看起来没问题,但是如何从代码端编写App.Config文件,然后?它自己编写?



是的,差不多。

1)在解决方案资源管理器中打开项目属性,然后双击Settings.settings

2)在结果网格中,将名称更改为MySetting,并且将值设置为默认值。将类型和范围分别保留为字符串和用户。

3)保存并关闭设置窗口。

4)阅读设置:

 string s = Properties.Settings.Default.MySetting; 

5)写下你的设置:

 Properties.Settings.Default.MySetting =我的新设定值; 
Properties.Settings.Default.Save();


I have 4 different forms. It is obvious that I can change the form which will be started when I run the application on the following line:

Application.Run(new Form1()); // I do not want to change this line maunally.

I want to change "new Form1" part by coding from other forms as well.

For example:

First, I run the application and Form1 starts. I click on the button to open Form2. When I open Form2, Application.Run(new Form1()) should be changed to Application.Run(new Form2()). So, when I close the whole application and start again, Form2 will start. In this case, I will be able to save my latest open Form.


So,
How can I change the Application.Run(new Form1()) programmatically ?

Thanks in advance...

解决方案

Don''t. Instead, have a MasterForm which is always opened from Application.Run. It''s Visible property (and show in taskbar property) is set to false.
It looks in the application config file, and decides which form to show (lets say Form1)
It displays form1, and hooks the FormClosed Event to close itself.
When Form1 presses for form2, it signals a "Form2Please" event which the MasterForm handles. It closes Form1, displays Form2, and changes the application config file for next time.

Quote:

"Well, it seems ok, but how to write on App.Config file from code side, then ? it writes itself ?"


Yep, pretty much.
1) Open your projects Properties in the solution explorer, and double click on "Settings.settings"
2) In the resulting grid, change the Name to "MySetting", and set the Value to "Defaulted value". Leave Type and Scope as "string" and "User" respectively.
3) Save and close the settings window.
4) To read your setting:

string s = Properties.Settings.Default.MySetting;

5) To write your setting:

Properties.Settings.Default.MySetting = "My new setting value";
Properties.Settings.Default.Save();


这篇关于如何在C#中以编程方式更改初始表单应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆