Where are these settings coming from? - c#

I have to pretty simple pieces of code:
private void frmMain_Load(object sender, EventArgs e)
{
string[] EmployeeNames = Settings.Default.Employee_Names.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
employee_NameComboBox.Items.AddRange(EmployeeNames);
}
And
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
string Employee_Names = string.Empty;
foreach (string Name in employee_NameComboBox.Items)
{
Employee_Names += Name + ";";
}
if (Employee_Names.Length > 1)
{
Settings.Default.Employee_Names = Employee_Names.Substring(0, Employee_Names.Length - 1);
Settings.Default.Save();
}
}
So, to test my code I added the values "Foo;Bar"
To my Settings.Settings in the VS Designer
but when I run the Code Nothing comes up. So I add this code:
private void AddName()
{
if (!employee_NameComboBox.Items.Contains(employee_NameComboBox.Text))
{
employee_NameComboBox.Items.Add(employee_NameComboBox.Text);
}
}
And then I call it on the Leave event of employee_NameComboBox:
private void employee_NameComboBox_Leave(object sender, EventArgs e)
{
AddName();
}
To test this I enter the employee_NameComboBox and type in "Employee1" and leave the CB then enter again and type in "Employee2" and Leave now when I click on the drop down I have these strings in the Items Collection. Great!
But when I close the application and then goto my settings.settings under properties in the Solution Explorer the values "Foo;Bar" are still there.
Continue to start Debugging again and when I click the drop down, the values are "Employee1" and "Employee2". In turn I search through the Solutions Directory in Windows Explorer to try and find the new settings file and I simply CAN NOT find ANY files with "Employee1" or "Employee2" all I see in ANY files is "Foo;Bar".
How can this be? I am saving to somewhere but where!
Also Why doesn't the debugger use the values in the settings.settings or App.Config files?!?!?!
Note: I also tried doing a Find from Visual Studios and Searched Entire Solution for the value Employee1 to no avail.
My end result is that I would like to send out an application with editable combobox that can be saved and edited without a Re-compile.

When such setting are changed at run-time, the values are stored in the following directory:
C:\Documents and Settings\<username>\Local Settings\Application Data
That's where the app then goes and looks for them the next time.

The settings are overwritten on each build by whatever you have set up in VS. If you want to use the settings from a previous compilation (or once deployed to a client machine, a previous version after an update) you have to use Settings.Upgrade(). This should be called once per runtime, most easily in the constructor. See this other StackOverflow question. C# .NET Application Settings and Upgrading

Related

Show a Messagebox if program is ran for the first time in C#

When a user runs the program for the first time I want a message box to show up.
I was thinking of something like this:
private void Form1_Load(object sender, EventArgs e)
{
if(firstTime)
{
MessageBox.Show("Welcome");
}
How could I get my program to display a message box when a user launches the program for the first time in c#?
You would need to store that information somewhere
File
System registry
Database
Settings in application
Then read the value, and setup the firstTime flag prior to check.
You can add a parameter to the application settings.
Go to the solution explorer in the section Properties and double click on Settings.settings.
Add a parameter named for example IsFirstLaunch and set type to bool with value True.
Then you can write:
if ( Properties.Settings.Default.IsFirstLaunch )
{
Properties.Settings.Default.IsFirstLaunch = false;
Properties.Settings.Default.Save();
MessageBox.Show("Welcome");
}
The settings are stored in:
c:\Users\{UserName}\AppData\Local\{Assembly CompanyName}\{Assembly Name}.Url__________
So be careful to set Assembly CompanyName in the AssemblyInfo.cs in the same section.
Assembly Name is from the application project properties (double click on this Properties section).
You can delete this file to test again.

Settings.Default value does not change after release

I have a windows forms desktop application. I am using a settings variable in resource named Settings.Default.Code
So I set the value initial value in Form->Properties->Settings.Code="123"
And I have changed the value in application using code like this:
private void button1_Click(object sender, EventArgs e)
{
Settings.Default.Code = "ABC";
Settings.Default.Save();
}
After button click the Code value is same as following image
:
But if I get the value in code Settings.Default.Code is "ABC"
When I created a setup project and release the application, the value of this settings should be "123" but it is "ABC". The initial value is not get from first setted value. Why? How can I solve this problem?
Add this line to the beginning of your program:
Settings.Default.Reset();
More info: ApplicationSettingsBase.Reset Method
As #Peter Duniho mentioned, the problem is probably that the setting is already saved to your profile on your computer. Try to delete the config file before you test again:
C:\Users{username}\AppData\Local\{ApplicationName}
or
C:\Users{username}\AppData\Roaming\{ApplicationName}

Compiled HTML Help file shows "This program cannot display...", when pressing F1 on the debugged application

I have this problem on my compiled html help file, which runs on Microsoft Visual Studio 2010.
Note: I don't have enough reputation points to post images. Please bear with my problem.
The name of the compiled html help file is GeneralHelp.chm. In order to make it appear, there are two ways:
Clicking the "General" from "Help" Tab.
Pressing F1 Key when the main form is only active.
I don't modify the default values of the properties, but here are the c# codes for the activation:
private void generalToolStripMenuItem_Click(object sender, EventArgs e)
{
Help.ShowHelp(this, Application.StartupPath + #"\GeneralHelp.chm");
}
private void mdiMain_Load(object sender, EventArgs e)
{
helpProviderGeneral.HelpNamespace = Application.StartupPath + #"\GeneralHelp.chm";
}
The first way is working properly, but the second way (pressing the F1 Key when the main form is only active) is not. It has a message display "This program cannot display the webpage". I tried reconstructing the .chm file, but it still happens.
Furthermore, I found out it becomes normal when I've clicked the other links first, then clicking the page that I would want to see in the navigation pane. My other .chm files doesn't work in this way. I also saved it in the proper folders: Debug and Release. Also, the spelling and case of GeneralHelp.chm is correct. Lastly, when I tried opening the GeneralHelp.chm, outside from MS Visual Studio 2010, it's just normal.
If you need further info, please comment and I'll answer. I just really want to know how this problem be solved. Thanks for the time reading this, I'm looking forward in granting me a solution.
You can imagine helpProvider1.HelpNamespace as a link to the .chm help file. Thus, no help topic on this way can be called. You must have knowledge of the internal structure of the CHM help file as a root node. Think of a structured web page ("homepage") on a web Server with (sub)directories and HTML Topics e.g. #"/Garden/flowers.htm".
As mentioned earlier by other comments (see above) make sure that the .chm file got copied to your EXE project's bin\Debug directory.
In the code example, I've set constants at the beginning. The examples of the CHM Help files depend on how they were compiled by the technical writer e.g. with HTMLHelp Workshop or other Help Authoring tools and are intended here to represent the possibilities.
Please note the inline comments: // set F1 help topic for this form ... // and set F1 help topic for some controls of this form (two lines per control).
namespace C_Sharp_CHM
{
/// <summary>
/// Using C# und CHM files.
/// </summary>
public partial class frmMain : Form
{
private const string sHTMLHelpFileName_ShowSingleHelpWindow = #"\help\CHM-example_ShowSingleHelpWindow.chm";
private const string sHTMLHelpFileName_ShowWithNavigationPane = #"\help\CHM-example_ShowWithNavigationPane.chm";
private const string sHTMLHelpFileName_ShowWithoutAutoSync = #"\help\CHM-example.chm";
public frmMain()
{
InitializeComponent();
}
...
private void frmMain_Load(object sender, EventArgs e)
{
// example: System.Diagnostics.Process.Start(Application.StartupPath + sHTMLHelpFileName_ShowWithoutAutoSync);
webBrowser1.Navigate(new Uri(GetChmUrl(Application.StartupPath + sHTMLHelpFileName_ShowWithoutAutoSync, "Garden/garden.htm")));
if ((chkShowHelpWithNavigationPane.Checked == true))
{
helpProvider1.HelpNamespace = Application.StartupPath + sHTMLHelpFileName_ShowWithoutAutoSync;
}
else
{
helpProvider1.HelpNamespace = Application.StartupPath + sHTMLHelpFileName_ShowSingleHelpWindow;
}
// set F1 help topic for this form
helpProvider1.SetHelpNavigator(this, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this, #"index.htm");
// and set F1 help topic for some controls of this form (two lines per control)
helpProvider1.SetHelpNavigator(this.btnPopulate, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this.btnPopulate, #"/Garden/flowers.htm");
helpProvider1.SetHelpNavigator(this.btnExit, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this.btnExit, #"/Garden/tree.htm");
helpProvider1.SetHelpNavigator(this.chkShowHelpWithNavigationPane, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this.chkShowHelpWithNavigationPane, #"/HTMLHelp_Examples/jump_to_anchor.htm#AnchorSample");
helpProvider1.SetHelpNavigator(this.btnOpenHelpShowTopic, HelpNavigator.Topic);
helpProvider1.SetHelpKeyword(this.btnOpenHelpShowTopic, #"/HTMLHelp_Examples/image_and_text.htm");
}
private void btnOpenHelpShowTopic_Click(object sender, EventArgs e)
{
Help.ShowHelp(this.btnOpenHelpShowTopic, helpProvider1.HelpNamespace, HelpNavigator.Topic, #"/HTMLHelp_Examples/image_and_text.htm");
}

Winforms preferences values

I'm a beginner in winforms, and just starting using it's preferences.
So, I add in my Settings.settings a Value named path, as string and User Scope.
I change it when I choose a new path with a FolderBrowserDialog and then, after a click on a Ok button, i change the preferences like this :
private void buttonPref_Click(object sender, EventArgs e)
{
Form2 subForm2 = new Form2(textBoxRep.Text);
subForm2.ShowDialog();
if (subForm2.DialogResult == DialogResult.OK)
{
Settings.Default.path= subForm2.rep();
subForm2.Close();
}
else
{
subForm2.Close();
}
}
public string rep()
{
return textBoxRep.Text;
}
Then, when I run my Application, I load the value in my preferences :
textBoxRep.Text = Settings.Default.path;
But, the value is set to empty after every new run.
So I tried with a Application Scope, but I got a read Only error on this : Settings.Default.path
How can I fix this? Is there a way to register the settings after mofified them?
Thank you.
you need to call Save method as below
Settings.Default.path= subForm2.rep();
Settings.Default.Save();
Settings that are application-scoped are read-only, and can only be
changed at design time or by altering the .config file in between
application sessions. Settings that are user-scoped, however, can be
written at run time just as you would change any property value. The
new value persists for the duration of the application session. You
can persist the changes to the settings between application sessions
by calling the Save method.
How To: Write User Settings at Run Time with C#
You need to also call Settings.Default.Save();

application won't start on startup after adding a notifyicon

my app was working ok and it would execute on startup before.
I added a notify icon and in my code,there are some places that this icon changes.I added all required icons in the root folder of my app,and everything is working fine with the icons,except the startup boot of my app.
I can see my app's address in the "run" part of the registry(I mean everything is the same as when my app booted at startup properly).but my app won't run at startup anymore.
any advice on my matter?
PS:I thought I should explain my work a little bit and I wrote a little piece of app that has the exact same problem
public Icon[] icons = new Icon[2] { new Icon("icon1.ico"), new Icon("icon2.ico") };
public int counter = 0;
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.Visible = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
counter %= 2;
notifyIcon1.Icon = icons[counter];
counter++;
As you can see,the app changes the icon of the notifyicon in every tick.with this code,the app won't run at startup.but if I remove the iconchanging feature of the app,it will actually run at startup
This requires psychic debugging, I'll guess that you are loading these icons using their relative path name. Something like new Icon("foo.ico").
This can only work correctly if the default working directory of your program is set where you hope it will be. It usually is, certainly when you start your program from Visual Studio or start it from a desktop shortcut. But not when you added it to the Run registry key. Environment.CurrentDirectory will be set elsewhere, typically the Windows directory.
You must always use the full path name of files. An easy way to get that path is:
var home = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
var path = System.IO.Path.Combine(home, "foo.ico");
var icon = new Icon(path);
But there's certainly a better way than storing icons as files, you can embed them in your program. Project + Properties, Resources tab. Click the arrow on the Add Resource button, Add Existing File and navigate to your .ico file. Now the icon is embedded in your program, you'll never lose track of it and can't forget to copy it when you deploy your program on another machine. And the code is simpler as well:
var icon = Properties.Resources.foo;

Categories

Resources