Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Setting saving form in C #

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章将为大家详细讲解有关C#中如何实现Setting保存窗体,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

C# Setting步骤一:打开项目属性窗口,切换到设置(Settings)标签,

如下图添加属性

Name Type Scope Value

WindowLocation System.Drawing.Point User 0,0

WindowSize System.Drawing.Size User 300,300

C# Setting步骤二:

在要保存状态的窗体代码头部添加代码

using UserSettingsDemo.Properties;

在窗体的FormLoad事件中添加以下代码:

private void FormMain_Load(object sender, EventArgs e) { // Set window location if (Settings.Default.WindowLocation != null) { this.Location = Settings.Default.WindowLocation; } // Set window size if (Settings.Default.WindowSize != null) { this.Size = Settings.Default.WindowSize; } }

C# Setting步骤三:

在窗体的FormClosing事件中添加如下代码:

private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { // Copy window location to app settings Settings.Default.WindowLocation = this.Location; // Copy window size to app settings if (this.WindowState == FormWindowState.Normal) { Settings.Default.WindowSize = this.Size; } else { Settings.Default.WindowSize = this.RestoreBounds.Size; } // Save settings Settings.Default.Save(); }

以上是原作者写的,窗体最小化后在任务栏右键关闭窗体,再次打开窗体会有点问题

private void frmMain_FormClosing(object sender, FormClosingEventArgs e) { // Copy window location to app settings Settings.Default.WindowLocation = this.Location; // Copy window size to app settings if (this.WindowState == FormWindowState.Normal) { if (this.Size.Width != 0 && this.Size.Height != 0) { Settings.Default.WindowSize = this.Size; } } else { if (this.RestoreBounds.Size.Width != 0 && this.RestoreBounds.Size.Height != 0) { Settings.Default.WindowSize = this.RestoreBounds.Size; } } // Save settings if(this.WindowState!=FormWindowState.Minimized) Settings.Default.Save(); }关于"C#中如何实现Setting保存窗体"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report