I have developed a C# form based application. It consists of a number of text boxes and a few radio buttons. If i want to create my own unique file type for this application (e.g - *.ct) how would i go about doing 开发者_开发问答that? What I want to achieve is for the user to be able to go to file save - it will then save a *.ct file which will be the info they have entered into the text boxes and the radio buttons they have chosen. I would then like to have an option when they run the application - File -> Open and they could open the *.ct file and then that would fill the checkboxes and select the radio buttons they have chosen. if anyone can point me out any links to code where they have seen this done, etc or paste up any examples i can work from that would be a great help.
Thanks.
I would suggest you rather save the file as XML, then when you read the xml file you can validate it's schema to determine if the your application can read it.
Xml Files is a standard when it comes to store information. This also allows your application to maybe at a future date communitcate between another application using a standard format. Which are xml files.
It looks like what you want to do quickly and easily map the contents of the file to your windows forms controls.
If that is the case then you should look at creating a class which represents the various states of your windows form - text box contents living in string properties, check boxes as boolean properties etc.
Once you have create that class you can then databind an instance of it to all your windows forms controls, this will mean that the windows forms databinding process will keep the ui and the class in sync for you.
The last step is the save and load that data. For that do some investigation in to C# serialization, there are lots of tutorials on how to write a class out to a file representation, where the only work you need to do is apply a few attributes to the class and call the correct methods from the serialization namespace.
Here is some very quick code showing the databinding for an example form with a single textbox and three radio buttons. You will need to experiment for your real case to get the right implementation of your backing class - radio buttons and radio button groups can be a little tricky.
I'm certain the code could be improved but it is just intended to show the approach.
public partial class Form2 : Form
{
private BindingClass backingClass;
public Form2()
{
InitializeComponent();
backingClass = new BindingClass();
backingClass.Name = "Hippo";
backingClass.One = true;
textBox1.DataBindings.Add("Text", backingClass, "Name");
radioButton1.DataBindings.Add("Checked", backingClass, "One");
radioButton2.DataBindings.Add("Checked", backingClass, "Two");
radioButton3.DataBindings.Add("Checked", backingClass, "Three");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(backingClass.Name);
if (backingClass.One)
{
MessageBox.Show("One");
}
if (backingClass.Two)
{
MessageBox.Show("Two");
}
if (backingClass.Three)
{
MessageBox.Show("Three");
}
}
}
public class BindingClass
{
private bool one;
private bool two;
private bool three;
public string Name { get; set; }
public bool One {
get { return one;}
set
{
one = value;
two = !value;
three = !value;
}
}
public bool Two
{
get { return two; }
set
{
two = value;
one = !value;
three = !value;
}
}
public bool Three
{
get { return three; }
set
{
three = value;
one = !value;
two = !value;
}
}
}
Where I create an instance of my BindingClass above you would instead deserialize the class from your file if it exists.
This approach is in someways leading towards an MVVM approach where the class used to support binding much like a View Model - I'd recommend getting into that mindset since you don't want the binding class to start containing logic. It is only there to give you something to bind against and something to serialize out which represents your form - you should have other logic containing objects that are you actual Model.
I won't show code for the serialization - there are lots of examples online for this. Here is a link to an MSDN article that would be a good first step: http://msdn.microsoft.com/en-us/library/ms950721.aspx
How to associate a file extension to the current executable in C# will be helpful: the answer really depends on how you deploy your app.
File associations are handled by windows. The user can tell what type of application is used to open a file with a certain extension.
In your case you would associate *.ct with your exe file. The full path is then provided as a parameter to the main function. So you need to handle a file argument when associating the extension.
For file open and save, there is a component to display file open and save dialogs. You can configure them to show *.ct only.
精彩评论