I want to be able to load some information programmatically into Properties.Settings.Default before I publish it, but it doesn't persist. How do I overcome that?
I have (as a test):
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Setting1 = "abc";
Properties.Settings.Default.Save();
}
private void button2_Click(object sender, EventArgs e)
{
Text = Properties.Settings.Default.Setting1;
}
I clicked on button1, then published (with clickonce) and then run the published application and clicked on button2. The Text was empty.
If don't publish the application, rather just close it and reopen it and click on button2 – I get "abc".
See the following post http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C
publishing won't click the button for you.
You need to detect the setting isn't initialised correctly (compare with an application setting perhaps) and then set and save it yourself.
Blank / doesn't exist / = somesettingToUpgrade
and a little routine to find a setting by name, set and save then you could put that in the button click handler as well as say FormLoad.
It seems that the problem was that the scope was "user". But if the scope is "application" – it can't be changed programmatically.
So there seems not to be a way to do it. Persist from build programmatically, that is.
Related
When I add www.stackoverflow.com into my RichTextBox and run the program it is shown in blue and as a hyperlink yet when I click it nothing happens. How can I fix this?
Make sure the text property includes a valid url. E.g. http://www.stackoverflow.com/
set the DetectUrls property to true
Write an event handler for the LinkClicked event.
Personally, I wouldn't pass "IExplore.exe" in as a parameter to the Process.Start call as Microsoft advise as this presupposes that it is installed, and is the user's preferred browser. If you just pass the url to process start (as per below) then Windows will do the right thing and fire up the user's preferred browser with the appropriate url.
private void mRichTextBox_LinkClicked (object sender, LinkClickedEventArgs e) {
System.Diagnostics.Process.Start(e.LinkText);
}
RichTextBox class allows you to customize its behavior when user clicks the hyperlink. Add an event handler for the RichTextBox.LinkClicked event
Process p = new Process();
private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
{
p = Process.Start("IExplore.exe", e.LinkText);
}
You should make sure that DetectUrls is set to true. If that doesn't work on its own, you may need to add a handler for the LinkClicked event.
Is yourTextBox.DetectUrls set to true? We may need some more info to provide a better answer.
I am trying to have my application save some form settings on exit.
Eg. I have a textbox and a checkbox and I want to save the states of both to a settings file.
I already know how to save the settings to the settings file, the trouble is that the settings don't save in the exiting method.
My application exit method is this:
void OnApplicationExit(object sender, EventArgs e)
{
MessageBox.Show("Exiting...");
session.Default.Path = txtPath.Text;
session.Default.Regex = chkRegex.Checked;
}
These are the two ways I've tried to have it call on exit:
private void Form1_Load(object sender, EventArgs e)
{
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnApplicationExit);
}
And
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += OnApplicationExit;
}
With both, the method gets called, the message box is shown but neither save the settings. I think the form controls are getting unloaded before it has a chance to read the values.
How can I save these form control values on exit?
If this is what i think it is, then this might be relevant
How To: Write User Settings at Run Time with C#
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.
So likely, you could just call
session.Default.Save();
Note : I could be completely wrong
I want to select all the contents of a MaskedTextBox when the clicks (or tabs onto) the control, so they can easily replace the old content. I tried calling SelectAll() in the Enter event, but that didn't work at all.
I switched to using the GotFocus event, which works great when tabbing through controls, but doesn't work when I click on it with the mouse. I would only want to select all the contents when first entering/focusing on the control (subsequent clicks might be used to position the cursor to edit the existing text).
I added a button and tried calling SelectAll() in the button click event, but that didn't do anything either. What's going on? Is this a bug?
How can I get around this?
Steps to reproduce
Create a new Windows Form Application in .NET 4.0 in Visual
Studio 2010.
Add a TextBox, MaskedTextBox, and Button to the default form
Change the Mask property on the MaskedTextBox to "_____".
Add some event handlers:
private void maskedTextBox1_GotFocus(object sender, EventArgs e)
{
Debug.WriteLine("GotFocus");
maskedTextBox1.SelectAll();
}
private void button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Click");
maskedTextBox1.SelectAll();
}
Run the program, entered some data into the MaskedTextBox, tab through controls back to it. It selects the contents of the MaskedTextBox.
Select the other TextBox. Try clicking on MaskedTextBox. Output shows that GotFocus event was called, but text doesn't get selected.
Try clicking on button in form. Text doesn't get selected.
Tested in Visual Studio 2010 with .NET 4.0 in a Windows Forms Application project
Why this isn't a duplicate of TextBox.SelectAll() does not work with TAB
If you notice, the title says "SelectAll doesn't work with TAB". In my case, it does work with Tab, it doesn't work with the mouse - completely opposite scenario. The answer for that question is to use the GotFocus event. I'm already using the GotFocus event, but it doesn't work. That answer does not answer this question. It is clearly not a duplicate. If I'm wrong, please explain in the comments.
Your SelectAll() is being overwritten by the default functionality of the masked textbox select. I would use the Enter event, it allows for tabbed entry or mouse click entry to the masked text box. You will most likely need to use the BeginInvoke method. Try the code below. It worked for me when I tried...
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}
private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
txtbox.SelectAll();
}
Executing Focus before Select All worked for me:
private void Masked_Enter(object sender, EventArgs e) {
((MaskedTextBox)sender).Focus();
((MaskedTextBox)sender).SelectAll();
}
Day 1 coder here:
private void button1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
string firstName;
}
In Visual C# Express, I changed my button name (through properties) from button1 to btnString, but as you can see it is not adapting in the code.
Did I do something wrong ?
private void button1_Click(object sender, EventArgs e)
{
}
This is the event handler, and by changing the control's name it won't automatically change the event handler name (because it was created prior to changing the control's name), you can manually do it though. You can also change it through the properties window, change the tab to "events" instead of the default selected "properties" tab
Changing the button name seemingly hasn't updated the Click event handler.
Either go to the button properties event tab, and update the button1_Click to btnString_Click and the same to the event function, or delete the button1_Click function and double click the button again, so visual studio will generate the correct handler name.
When you change the name of a control, the designer will search your code and replace instances where you have used that variable name, but it will not change the name of event handlers.
Why? What if instead of the "default name" (for a button click it would be <buttonName>_Click) you had your own custom name, like MyCoolEventHandler_Click? The designer would not know how to rename that. The same thing applies if you have coincidentally used a variable name in a totally unrelated function. Would you want it changing the name on you?
You have to do these changes manually. My best advice is to name the controls before you create event handlers. But you can always go into the Properties panel and change the links.
Visual Studio 2010 Express, Windows Forms.
Have made myself my first little application which used a local database (a journal app).
First form (which load when app starts) shows every entry that has been made.
A "Add button" show up a new form, from where I can add a new entry.
Now, everything works this far.
But when I close the add form, after adding a new entry, the new entry isn't displayed in the first form. I have to close the program and start it up again the see the new entry.
How can I refresh the data in the first form, when the add form closes?
Best regards from Denmark
First of all you must open your second form as dialog like new FormAdd().ShowDialog(this);
this will stop the code until FormAdd close, then you can reload the data as you load in form firs load.
your code will be like this
private void Form1_Load(object sender, EventArgs e)
{
LoadData();
}
private void btnAdd_Click(object sender, EventArgs e)
{
new FormAdd().ShowDialog();
LoadData();
}
private void LoadData()
{
//load your data
}
by the way you can use DialogResult for unnecessary prevent refreshs is user does not insert a record to db. in FormAdd code set DialogResult value and in main form use like this
private void btnAdd_Click(object sender, EventArgs e)
{
if (new FormAdd().ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
LoadData();
}
I'm aware that this is an old post, but in looking for an answer on this site to the same situation, I also discovered another method which, may work as well.
I also have Visual Studio 2010 Express, Windows Forms. I invoke a separate form via a MenuStrip button click. Using this 2nd form, I add various hours-of-the-day to a dataGridView on my main form, i.e., 9:00AM, 9:30AM etc.
I found that after adding a new time, when I clicked a combobox dropdown button on my dataGridView that, the new entry was not there. Like the original poster, I would have to close and reopen my main form.
In my Form_Load event, VS had added a TableAdapter entry, which loads data into the table, i.e.
this.TimesTableAdapter.Fill(this.AssignmentsDataSet.Times);
However, by copying this line into the form's Form__Activated event, when I click on the dataGridView's dropdown list, the newly created entry now appears.
Hopefully, this proves useful to any who run into the same issue.