Hello all,
I have a combo box in which I have a list of items, each representing a variable part of a file name.
I want to start my application with data from a file name derived from the last selection in the Combo box whith which the application was closed.
Combo Boxes (as well as other controls?) do not preserve their last selection when the app is closed / opened.
Is there a way to 'remeber' the last selected text in the Combo Box?
Is there a workaround or a 'trick' ?
(I have tried several worarounds, like changing a textbox text in runtime, but all controls seem to start fresh and clean).
Thanks
sam
Loading
Pravin GhadgePosted Dec 20, 2011, 8:18 AM
Yes, controls cannot preserve their last selection when app is closed.
So we have to create temporary table in which we store last selected value from combobox when we closed form (i.e. on form_Closed event).
& At the time of Opening Form We have to fetched last Store item from temp table
& shown to combox.
Pravin GhadgePosted Dec 23, 2011, 2:59 AM
But this is also good solution.
@Sam Hobbs:
Can u plz explain us wat is adv. or disadv. of resource file over Setting tab.
or
wat is better tool to use in such condition & why?
SamPosted Dec 23, 2011, 2:42 AM
First - Sam Hobbs is right in writing that the native VS tool to preserve settings is the Setting class.
As I wrote, it is somewhat odd tool when compared to other VS tools.
In any case, if you wish to use the resource class:
Sorry I omitted one important line, namely the initialization of the object for reading.
See the bold line in the reading section.
"FileName" is any path and file name you wish to use for saving the controls.
You naturally must read the same file name.
When you read the file, it loads and makes available any of your saved controls properties as a Key - Value pairs table, where the Key is the name you gave the control.
In the example here, the name is the resource.Text which is the control's .Text, but it can be any other control's property.
As you can see in my example, you can then get any of them simply by filtering each by its Key (name), either individually or in a loop.
(I suspect that the resource can also save objects (rather than properties), but I am not sure about it).
Here is the corrected snippet:
//Saved on form closure:
ResourceWriter rw = new ResourceWriter(fileName);
foreach (Control resource in Controls)
{
{
rw.AddResource(resource.Name, resource.Text);
}
}
rw.close();
//Open on form opening:
ResourceSet br = new ResourceSet(fileName);
foreach (DictionaryEntry baseResource in br)
{
var key = baseResource.Key.ToString();
if ((string)key == "MyComBoxName")
{
MyComBoxName.Text = baseResource.Value.ToString();
fileName = MyComBoxName.Text;
}
}
br.Close();
Pravin GhadgePosted Dec 22, 2011, 11:15 PM
I have tried ur solution but its give me some error.
I have marked it as red color.
Saved on form closure:
ResourceWriter rw = new ResourceWriter(fileName);
foreach (Control resource in Controls)
{
{
rw.AddResource(resource.Name, resource.Text);
}
}
rw.close();
Open on form opening:
foreach (DictionaryEntry baseResource in br)
{
var key = baseResource.Key.ToString();
if ((string)key == "MyComBoxName")
{
MyComBoxName.Text = baseResource.Value.ToString();
fileName = MyComBoxName.Text;
}
}
br.Close();
1)What will be filename?
2)What is br?
can u give me some hint?
Sam HobbsPosted Dec 22, 2011, 8:11 PM
The Settings are designed so that you can store and restore values specific to the application. It is your responsibility to provide the UI. There are limits to what the Settings can be used for but when they are useful they can be very convenient. All of the code needed to read and write the XML is already done for us.
SamPosted Dec 22, 2011, 5:11 PM
Please do not take it too hard.
I truly and deeply appreciate your support (and you helped me a lot in my first C# programming days, about one year ago).
I wonder sometimes when you find the time for free support to every question (many times quite silly ones...).
In any case, you were 100% right.
Settings is what I should have used.
I did not read your reply careful enough and did not catch the meaning of the word 'settings' in your text, mainly because I forgot all together about Settings (That I did use in the past).
Settings is the proper tool although from what I remember it somewhat looks like a non-VS tool, as it is not very user friendly.
(I use VS2008. It may have been modified since then. Did it?).
One would expect to get a list of options (variables) to select from, as in all other VS tools, but there is nothing in the Settings to help.
Further more, the Settings table does nothing without writing the code to each setting (both saving and retrieving).
The table should have done all this automatically, as it is done in creating many other objects.
Regarding the forum: the C# or the Form:
The list of available forum subjects is very long and there is overwhelming overlap of subjects, thus I am never sure of what to choose.
My question was C# in WinForm, but which comes first?
Lets keep up the Sam name brotherhood!!
Thanks again.
Sam
Sam HobbsPosted Dec 21, 2011, 3:08 PM
Note that if this application will be used by multiple users in a system, then consider the possibility that perhaps there should be a field indicating what the user is so you can keep the data separate for each user.
It is good that you have the problem solved using a table. You can keep in mind that the settings for applications is the most common solution for things like this. My guess is that if use of settings had been the first response then you would have done that.
SamPosted Dec 21, 2011, 8:18 AM
Thanks for your replies.
Isn't it quite obvious we talk about WinForm when we have ComboBoxes?
and, the word 'form' is in the subject!
In any case, my solution was simple, yet comprehensive (and it's a table):
I started a generic resource file with a fixed name, and stored there all my controls.
The file is automatically built and saved on form closure, and opens on form opening.
This solution saves all current controls, and enables me select any of the controls when opening the form.
It holds the previous file name in its combobox key, and I can use it if I do not specify any other file name.
Of course, one has to be careful with the type of value stored, and cast accordingly.
A simplified snippet:
Saved on form closure:
ResourceWriter rw = new ResourceWriter(fileName);
foreach (Control resource in Controls)
{
{
rw.AddResource(resource.Name, resource.Text);
}
}
rw.close();
Open on form opening:
foreach (DictionaryEntry baseResource in br)
{
var key = baseResource.Key.ToString();
if ((string)key == "MyComBoxName")
{
MyComBoxName.Text = baseResource.Value.ToString();
fileName = MyComBoxName.Text;
}
}
br.Close();
Sam HobbsPosted Dec 20, 2011, 4:03 PM