I am building a Winform based C# desktop application. I have a number of Combox control which the user can select the input from the drop down list or manually enter an input. The Combox Items itself is populated from a static list.
Whenever the user enters a new input which is not present the list, I add to this static list.
Here is the small code snippet which does that :
if (!this.client_type.Items.Contains(items.ClientType))
{
ComboItems.ClientList.Add(temp.CourtName);
}
So far, this approach works as long as the application does not close or exit. When the application restarts, the list does not have the manually entered items.
I am considering saving the list in a configuration file. Is there any other approach apart from using a configuration file ?
Thanks
Any type of serialization should work. A couple of popular ideas:
Database: If you are already saving data to a database or if you want that same customized list to be available to all users regardless of if they are on the same machine then that may make the most sense.
XML: If the changes should be per box that it runs on then you would want to store it locally. XML is popular and there are libraries available in the framework to work with XML files.
Here is a link to look into other options.
Is there any other approach apart from using a configuration file?
Yes. (For example, a database.)
You might also take into consideration writing and loading the items list into the registry. Here is a link containing a nice tutorial on it's manipulation: http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C
Related
In my c# windows application, I like to get multiple files with a same order as user select the file in openfiledialog window. I'm getting all the selected file but its order is not correct. Anyone help me to solve this problem.
If you look at the documentation for the underlying control's interface OPENFILENAME, you'll see this comment:
If the user selects more than one file, the lpstrFile buffer returns the path to the current directory followed by the file names of the selected files.
And, other than some comments on the separator characters used for different scenarios, what to do if there's not enough buffer, and some details on legacy 8.3 support, that's it. In other words, the OpenFileDialog doesn't offer any mechanism to bubble up the sort information as the underlying Windows Common Dialog Box doesn't offer anyway to get this information out via a standard interface.
If you must preserve it, you'll need to create your own Form or Dialog to track the selection in order, and honestly you'll probably be better off providing an slightly different UI that allows the user to reorder files after they've selected them, similar to how one can reorder lists on some mobile devices.
I'm trying to design my C# winform application with a very generalized function to automatically go through all of the form elements and save their states/values in a text file so that I can load it later. I want it to be very generalized so that it'll be a cinch to reuse this code in future projects, as it wouldn't be heavily tied down to the specifics.
The form elements I want to save are text boxes, combo boxes, data grid views, list boxes and that's about it. I want to save their values and everything about them.
One way that I was going about it was to go through every possible form element and then detect eachs type, and then create the corresponding c# code to re-create its value ('tboxmine.value="blue elephant"'), and then writing the code to a file, so that I could load the code from the file and execute it using the CSCcompiler. My code so far doesn't seem to be working correctly and I'm having my doubts that this compiler is actually running the code inside my application (I think it's possibly creating a new thread?), and it just seems like there's probably a far more straightforward relatively standard way of doing this.
This seems a bit like the reverse "best practice" approach. If you dont't know about databinding I suggest you look into that.
Basically you create classes to represent your data and use databinding to associate controls with your objects. The controls will automatically show the right value and allow the user to change it. If the user has changed the value, your object gets automatically updated.
To save the data, you would use some kind of serialization to store your objects in a file. When loading, you let the Serializer rebuilt your class structure and (best case) you are good to go.
This is not exactly what you asked for, but I think it is something you could use well ;-)
N.B.: Not the complete state of the control is saved. e.g. in a Textbox your text would be saved but the BackColor won't.
To get you started look into this tutorial: http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
I am using a localization in my project, But my question is when user install a project
they choose a either English or Marathi language from the radio button option in set up time and my whole project run on that particular language only?
How to set this on set up time?
You'll need to create two files (or static arrays/lists of strings, but this is not recommended as the application will keep both ready in RAM, and this can take up quite some memory if the application contains a lot of text) and then a static List of type string.
Whenever the user selects an option, you'll have to make your application load the appropriate file (by calling List.Add for each row of your file, depending on how you built it).
There is a downside, obviously: Instead of just assigning a string to a control, you'll have to add it to the lang file, and when creating said control, you'll need to pass it the corresponding element in your list.
I think that this is the best option, otherwise you could create two lists containing both languages when the application starts, and dispose of the unused one after the user applies the new configuration.
I want to store a list of 10-20 elements inside application configuration file in a C# application. I realize that I can use add key tags in appsettings section, but then I will have to create a unique key for each.
I am planning to loop over the elements in a section, and tried storing it. but it fails it initialize confiuration managager
EDIT: I want to store a section for table names to verify , where user can enter a list of tables to run a test on each
You can create your own IConfigurationSectionHandler.
MSDN Tutorial
Code Project Tutorial (This directly references multiple values, and may be more helpful with what you're going for)
EDIT
Just as an aside, this may be more work that you need given your last edit. You could just use a delimiter not found in the acceptable identifier list and split the string. (probably a whole lot easier)
I am trying to build a Translation Assistant which can read in other compiled C# application (.exe), and display the forms from the EXE, are displayed individually, along with a table next to it with english column which will show the current english words on display, and another column for the value, which a translator can enter. Once completed translations, the translator can export the translations a resx file, to add to a project and also as an excel file for record purposes.
I am new to C# and hence am not sure if my strucute is correct, i have designed in such that a dll is inserted into the .exe during compilator, and then using this dll, the translation application can extract the string. This works for most strings, but it is getting stuck where there are several string that can apear in the same textbox at different times [e.g. disconnected, connected etc]. I have tried searching everywhere, but I am not able to find information on how i will be able to pull out all strings from an application, and be able to identify which form they belong to, in order to create my application?
the other issue i am faced with is, actually displaying the translated strings, the application i am building would benifit greatly if it could display a example of how the translated strings would look, as translations in some languages could be excessivly long. but i have found that i am only able to read in the aspects of the compiled applications and create an instance, but am not able to translate it.
I am reading in the exe using Reflection, and have understood from online that i need to use reflection.emit to modify the form. but i am finding every sting that is idenfitied from the form, is extracted as an instance, hence changing the string is only changing the instance of the strings , and not the instance of the form itself. hence i am not able so a correct display.
I have been trying for 3 weeks to solve these last two questions, Thanks in advance for helping me solve this.
I think you can't find a general solution to your problem with the texts that may appear in the textbox. Here is why:
If the texts are in the resource file, you could read them, but you still don't know where they are used. You would need to do a complex analysis of the source code to know, where the text is displayed. Just imagine this little scenario:
textBox.Text = GetCorrectText(connection.State);
GetCorrectText could look like this:
string GetCorrectText(ConnectionState state)
{
return string.Format(Resources.ConnectionState, state);
}
Resources.ConnectionState might be "The connection is in the state {0}".
Its a simple example, but you would need to know or extract a lot of things:
The text property of the TextBox class is the string that is shown to the user
The Method GetCorrectText returns the text, so you need to parse it.
The Method string.Format returns the text. Now you either would need to hardcode that for string.Format it should use the first parameter as the text that is displayed or you would have to parse string.Format to learn that fact.
The example shows something else: You wouldn't be able to translate the whole string that is being displayed, because part of it is the name of the enum value.
What I want to show you is that you need to make trade offs.