如何使用序列化事件“checkedchanged” [英] How to use serialisation for event "checkedchanged"

查看:84
本文介绍了如何使用序列化事件“checkedchanged”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要一些帮助。所以,我已经在Windows窗体中创建了一个应用程序,它工作正常,现在我希望每次Windows启动时启动此应用程序。我已成功使用此代码:

Hello guys, I need a little help. So, I have made an App in Windows Forms, it works fine and now I want to start this App everytime when Windows Starts. I have successfully made it with this code:

RegistryKey add = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
add.SetValue("Battery", "\"" + Application.ExecutablePath.ToString() + "\"");





但是我在Form上编辑了一个Checkbox。如果选中它,它必须在Windows Loads时启动。如果没有选中它,它就不能与Windows一起启动。所以我做了一个Event CheckedChanged来检测复选框何时被更改,但我不知道如何保存Checkbox的价值,这样在关机后它就不会被删除。



我的尝试:



我在互联网上发现我可以用Serializable来做到这一点。我只明白,我必须在课前编写它,而不是在XML,Binarystream,Memory Stream中保存信息。但我不知道我要保存什么以及如何保存?有人可以帮助我吗?



But I have edited one Checkbox on Form. If it's checked, it has to start when Windows Loads.If it's not checked, it haven't to start together with Windows. So I have made an Event CheckedChanged which detects when checkbox is changed but I donn't know how to save value of Checkbox so that after shutdown it wouldn' be deleted.

What I have tried:

I have found in Internet that I can do it with "Serializable". I have only understood, that I have to write it before class and than save information in "XML, Binarystream, Memory Stream". But I donn't know what have I to save and how ? Can someone Help me ?

推荐答案

此Google搜索: c#run app在Windows启动 [ ^ ]出现了这样: c# - 如何设置程序在启动时启动 - Stack Overflow [ ^ ]:



This Google Search: c# run app on windows startup[^] turned up this: c# - How do I set a program to launch at startup - Stack Overflow[^]:

using Microsoft.Win32;

private void SetStartup()
{
	RegistryKey rk = Registry.CurrentUser.OpenSubKey
		("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

	if (chkStartUp.Checked)
		rk.SetValue(AppName, Application.ExecutablePath.ToString());
	else
		rk.DeleteValue(AppName,false);            

}


另一种实现此目的的方法是创建一个应用程序设置[ ^ ]。在此示例中,FormClosing EventHandler将CheckBox状态与设置进行比较,然后在必要时添加或删除注册表项。



设置'Scope参数为你的'bool设置为'用户,而不是'应用。
Another way to achieve this is to create an Application Setting [^] of Type Bool. In this example, the FormClosing EventHandler compares the CheckBox state to the Setting, and then adds, or deletes, the Registry Key if necessary.

Set the 'Scope parameter for your 'bool Setting to 'User, not 'Application.
using System;
using System.Windows.Forms;

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

        private bool isAutoStartNow, isChecked;

        private void Form1_Load(object sender, EventArgs e)
        {
            isAutoStartNow = (bool)Properties.Settings.Default["AutoStart"];

            checkBox1.Checked = isAutoStartNow;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            isChecked = checkBox1.Checked;

            isAutoStartNow = (bool)Properties.Settings.Default["AutoStart"];

            // no change necessary
            if (isAutoStartNow == isChecked) return;

            Properties.Settings.Default["AutoStart"] = isChecked;

            if (isChecked)
            {
                // set the registry key
            }
            else
            {
                // delete thr registry key
            }

           // save setting
            Properties.Settings.Default.Save();
        }
    }
}


假设,如果您在程序加载时尝试打开要添加的密钥,如果它在那里意味着你必须开始检查,如果用户取消选中,删除密钥。如果用户再次检查它,请尝试打开密钥,如果不存在,则创建。
Suppose, if you attempt to open the key you're adding when your program loads, if it's there means you must start checked, if user unchecks, remove the key. If user then checks it again, try to open key, if it's not there, create.


这篇关于如何使用序列化事件“checkedchanged”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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