cheatcountry

What I am trying to do in this code is if the user clicks the checkbox (default is to be unchecked), then it will stay checked when the user goes back to this window. My code is below:

private void frmPreferences_Load(object sender, EventArgs e)
{
// Attempt to open the key
RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Franklin's Stock Project\\Preferences");

// If the return value is null, the key doesn't exist
if (key == null)
{
chkAutoStart.Checked = false;
}
else
{
key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\\Franklin's Stock Project\\Preferences");
key.SetValue("AutoStart", chkAutoStart.Checked);
chkAutoStart.Checked = Convert.ToBoolean(key.GetValue("AutoStart").ToString());
chkAutoStart.Checked = true;
}
}


Re: Visual C# General why is this statement not working?

In Love With Dotnet

Hi,

Check below links. Should help you.

http://en.csharp-online.net/Read_a_Registry_key

http://www.c-sharpcorner.com/UploadFile/sushmita_kumari/RegistryKeys102082006061720AM/RegistryKeys1.aspx ArticleID=0ce07333-c9ab-4a6a-bc5d-44ea2523e232

I have never used registry in .net so did not try dig into your code. Using registry is never recommended. You look like storing user choice so when user return, user can find settings set last time. To do this, .net has a new functionality in 2.0 version called Profiler. This settings are stored in client side. I would strongly recommend that you have a look to this and if you can implement profilers, would be great. There are many links available, you can just google it. Few I found in google as initial search results are as following.

http://quickstart.developerfusion.co.uk/QuickStart/aspnet/doc/profile/default.aspx

http://msdn.microsoft.com/msdnmag/issues/05/10/CuttingEdge/

You can find plenty of information from MSDN and other sources. If you try profiler, let me know your experience with this.

Hope this will help.






Re: Visual C# General why is this statement not working?

cheatcountry

This is for asp.net and I am using C#

Won't that difference make it to not work




Re: Visual C# General why is this statement not working?

In Love With Dotnet

No it does not make any difference. Did you think to use profiles






Re: Visual C# General why is this statement not working?

cheatcountry

I do not know how to use profiles in C#
How do I do that




Re: Visual C# General why is this statement not working?

In Love With Dotnet

I suggest you invest sometime in reading about profiles. Its very easy and recommended to use in scenario like you have. Just google it go through MSDN




Re: Visual C# General why is this statement not working?

cheatcountry

That is what I have been doing but all I can find is creating user profiles in ASP.NET




Re: Visual C# General why is this statement not working?

cheatcountry

This is a windows application so I can't use profiles in it




Re: Visual C# General why is this statement not working?

In Love With Dotnet

You just said its asp.net application and now you are saying windows application. I dont know how you can go about this in win application. But still, try to use resource file. I would still not recommend registry usages. I got an article, dont know its much userful or not. Have a look. http://www.devx.com/dotnet/Article/34273

Let someone else have a look at this and may provide good solution.






Re: Visual C# General why is this statement not working?

cheatcountry

That link doesn't have anything to do with what I am doing. I have tried to think of a solution to my problem but the only thing that I can think of was to use the registry data. Is my if statement correct




Re: Visual C# General why is this statement not working?

In Love With Dotnet

Hi,

I tried something, have a look.

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Franklin's Stock Project\\Preference");

string sAnswer="";

// If the return value is null, the key doesn't exist

if (key == null)

{

blntest = false;

key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\\Franklin's Stock Project\\Preference");

key.SetValue("AutoStart", "Nikunj");

sAnswer = key.GetValue("AutoStart").ToString();

}

else

{

sAnswer = key.GetValue("AutoStart").ToString();

}

Hope this will help






Re: Visual C# General why is this statement not working?

Sharad Nair

Hi,

If I understand your code correctly you are trying to maintain the state of the checkbox in a WinForms application so that when a user comes back to the form later on then the last checked status of the checkbox is displayed. For this I have modified your code a bit as follows

Code Block

private void frmPreferences_Load(object sender, EventArgs e)

{

// Attempt to open the key

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Franklin's Stock Project\\Preferences");

// If the return value is null, the key doesn't exist

if (key == null)

{

// Create the key only if the key doesn't exist. In your case you had created the key in the else section.

key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\\Franklin's Stock Project\\Preferences");

key.SetValue("AutoStart", chkAutoStart.Checked);

chkAutoStart.Checked = false;

}

else

{

// Read the value of AutoStart from the Key if the key exists

chkAutoStart.Checked = Convert.ToBoolean(key.GetValue("AutoStart").ToString());

}

// Close the key

key.Close();

}

Now I have added some code in the Form_Closing event handler for the form as you need to write the status of the checkbox back to the registry when the form closes. You may choose to write this in the chkAutoStart_CheckedChanged event handler instead. Following is the code:

Code Block

private void frmPreferences_FormClosing(object sender, FormClosingEventArgs e)

{

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Franklin's Stock Project\\Preferences",true);

key.SetValue("AutoStart", chkAutoStart.Checked);

key.Close();

}

Hope this post is of some help to you.

Cheers

Sharad






Re: Visual C# General why is this statement not working?

cheatcountry

I tried your code and it didn't work...




Re: Visual C# General why is this statement not working?

cheatcountry

In Love With Dotnet wrote:

Hi,

I tried something, have a look.

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Franklin's Stock Project\\Preference");

string sAnswer="";

// If the return value is null, the key doesn't exist

if (key == null)

{

blntest = false;

key = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\\Franklin's Stock Project\\Preference");

key.SetValue("AutoStart", "Nikunj");

sAnswer = key.GetValue("AutoStart").ToString();

}

else

{

sAnswer = key.GetValue("AutoStart").ToString();

}

Hope this will help



Where is this blntest coming from




Re: Visual C# General why is this statement not working?

Sharad Nair

Hi,

What did u do to check if the code works or not Could you please let me know the steps. I have tried the code on my system and it works.

Moreover It would be helpful if you could write down point by point on what you are trying to achieve

cheers

Sharad