I have created code in WPF to let the window remember its last location like this:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
Rect storedLoc = Properties.Settings.Default.WindowSavedLocation;
this.Top = storedLoc.Top;
this.Left = storedLoc.Left;
}
catch
{
MessageBox.Show("No settings stored !");
}
}
private void Window_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.WindowSavedLocation = RestoreBounds;
Properties.Settings.Default.Save();
}
When I build the application I can see that the app.exe.config file has the setting
WindowSavedLocation
but it just does not save and throws no exception.
Everytime I run the application, it says "No settings stored !".
It's scope is user.
I repro. The Remarks section of the Window.RestoreBounds property docs is relevant to your problem:
If you query RestoreBounds before the
window has been shown or after it has
been closed, Empty is returned.
Use the Closing event instead so the RestoreBounds property is still valid.
Related
Currently trying to save settings of last inputted textBox value (e.g. number).
Partially this is working code. However, that code allows to remember last value while closing/opening of Parameter_Form(subForm). In case of closing MainForm (application itself), the last textBox value doesn't retain. Why? The history hasn't record. Also I can't get why the cell 'value' is empty. Please see pic.
private void Parameter_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.textBoxLastValue = textBox1.Text;
Properties.Settings.Default.Save();
}
I've found following stuff. Please see attached pic.
Principally it's that number I've inputted in textBox.
There's no issue within app running and opening/closing subForm.
There's an issue after closing of MainForm.
If you load and save settings manually, you should make sure you load settings in form load event and also save it in form closing event:
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = Properties.Settings.Default.Test;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Test = textBox1.Text;
Properties.Settings.Default.Save();
}
If you are using data binding to settings, then you just need to save when closing.
I have a Winforms app with buttons that when clicked go to various internal and external locations (Network locations/Web paths) I was asked if it was possible for the end users to have their own names for the buttons, instead of what I called them.
I've created an App.config file so if the links change they can at least edit that file reflecting the change in path/URL etc
The button properties text field doesn't appear to allow code behind it.
private void GoogleS_Click(object sender, EventArgs e)
{
try
{
string GoogleS = ConfigurationManager.AppSettings["Google"];
Process.Start(GoogleS);
}
catch (Exception GoogleErr)
{
MessageBox.Show(GoogleErr.Message);
}
}
And the key inside the App.config file
<add key="Google" value="http://www.google.com/" />
And the fix
App.config
<add key="GoogleButton" value="Google Search"/>
<add key="GoogleLink" value="http://www.google.com"/>
//Code for the Button names which are loaded on Form Loading from the App.config file
private void CSS_Load(object sender, EventArgs e)
{
btnGoogleS.Text = ConfigurationManager.AppSettings["GoogleButton"];
}
private void btnGoogleS_Click(object sender, EventArgs e)
{
try
{
string GoogleS = ConfigurationManager.AppSettings["GoogleLink"];
Process.Start(GoogleS);
}
catch (Exception GoogleErr)
{
MessageBox.Show(GoogleErr.Message);
}
}
private void btnGoogleS_MouseHover(object sender, EventArgs e)
{
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip(this.btnGoogleS, (ConfigurationManager.AppSettings["GoogleLink"]));
}
You can manually set the button captions:
btnGoogle.Text = ConfigurationManager.AppSettings["GoogleCaption"];
Probably you would do it in a Form Load handler.
I am developing an add-in for outlook 2010.
Basically, I have a button on my ribbon that takes the selected email, and saves it to a text file. If the email contains a certain subject then the save is done automatically to a hard coded file path. If not, a windows form is opened asking the user to enter a filepath.
When the user has selected a path, and clicked 'OK' the save takes place and then the form closes... but then it re-opens... it seems to be creating a new instance of it or something... if I click 'Cancel' or 'X' it closes, but I can't see why it's not closing properly the first time.
Below is my code
//This is myRibbon.cs
private void btn_SaveFile_Click(object sender, RibbonControlEventArgs e)
{
//other code
if (subject = "xyz")
{
//other code
textFile.Save();
}
else
{
MyPopup popup = new MyPopup();
popup.ShowDialog();
}
}
//This is MyPopup.cs
private void btnOK_Click(object sender, EventArgs e)
{
var filePath = txtFilePath.Text;
if (!string.IsNullOrWhiteSpace(filePath))
{
SaveEmailToText(filePath);
this.Close();
}
else
{ //show message box with error }
this.Close();
}
private static void SaveEmailToText(string filePath)
{
//other code
textFile.Save();
}
I have simplified this quite a bit so its easier to read.
Any help would be greatly appreciated.
Consider to use OpenFileDialog instead of your popup form
Use your popup (or file dialog) only for getting file name
Keep email saving code in one place (otherwise you will have duplicated code)
Verify DialogResult of dialog form, before processing further
Forms are disposable - using statement will dispose them automatically
Do not close dialog form - set it's DialogResult property instead
Here is refactored code:
private void btn_SaveFile_Click(object sender, RibbonControlEventArgs e)
{
string filePath = defaultPath;
if (subject != "xyz")
{
using(MyPopup popup = new MyPopup())
{
// user can close popup - handle this case
if (popup.ShowDialog() != DialogResult.OK)
return;
filePath = popup.FilePath;
}
}
SaveEmailToText(filePath);
}
private void SaveEmailToText(string filePath)
{
//other code
textFile.Save();
}
And your popup, which should be replaced with OpenFileDialog:
private void btnOK_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(FilePath))
{
//show message box with error
DialogResult = DialogResult.Cancel;
return;
}
// you can assign default dialog result to btnOK in designer
DialogResult = DialogResult.OK;
}
public string FilePath
{
get { return txtFilePath.Text; }
}
This button opens a text file which has error log information:
private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
Process.Start(AppVars.ErrorLogFilePath);
}
When the user goes to do any processing in the application, I want to check whether the file is open or not, if it is open, then I want to close it. How would I go about doing this?
This example is almost identical to what you're trying to do:
Process.Close Method
Process myProcess;
private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
myProcess.Start(AppVars.ErrorLogFilePath);
}
private void doSomething()
{
if (!myProcess.HasExited)
{
myProcess.CloseMainWindow();
myProcess.Close();
}
// Do whatever you need with the file
}
Shows how to check if it's running and how to close it.
I have a WinForm with a menu bar, a menu and a menuItem (called BlaBlub).
the menu item has CheckOnClick = True and (ApplicationSettings)->(PropertyBindings)->Checked mapped to the setting SomeBool (type bool, scope user, initial value= false)
the value is read correctly from the settings-file (i use a label to check it and also the menu item gets selected/deselected when I make changes to the file between sessions).
However, using the following code I was not able to open the application, click on the menu item and store the changed value back into file
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = string.Format("Value is: {0}", Properties.Settings.Default.SomeBool);
}
I was able to store the value back into file, using the following code, but since this does not seem to be the idiomatic approach, I still seek some enlightment as to how to do this.
private void blaBlubToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
{
Properties.Settings.Default.SomeBool = blaBlubToolStripMenuItem.Checked;
}
You said:
the value is read correctly from the settings-file
However, based on the code presented, that wouldn't be correct because on load you aren't setting the checked state. Instead, I think your testing is showing the initial persisted setting value (being false) is also the default Checked state for the menu item.
Therefore, you should also intialize the control too by adding:
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = string.Format("Value is: {0}", Properties.Settings.Default.SomeBool);
blaBlubToolStripMenuItem.Checked = Properties.Settings.Default.SomeBool;
}
Note: Ordinarily I would tell you to use databinding but you can't because I believe MenuItem's do not support this.