在 C# Windows 窗体上保存/加载输入?

发布于 2024-11-29 17:52:06 字数 290 浏览 2 评论 0原文

我开发了一个基于 C# 表单的应用程序。它由许多文本框和一些单选按钮组成。如果我想为此应用程序创建自己独特的文件类型(例如 - *.ct),我将如何做?我想要实现的是让用户能够进行文件保存 - 然后它将保存一个 *.ct 文件,该文件将是他们输入到文本框和他们选择的单选按钮中的信息。然后,我希望在他们运行应用程序时有一个选项 - 文件 ->打开后,他们可以打开 *.ct 文件,然后该文件将填充复选框并选择他们选择的单选按钮。如果有人可以向我指出他们已经完成此操作的任何代码链接等,或者粘贴我可以使用的任何示例,这将是一个很大的帮助。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

謌踐踏愛綪 2024-12-06 17:52:06

我建议您将文件保存为 XML,然后当您读取 xml 文件时,您可以验证它的架构以确定您的应用程序是否可以读取它。

Xml 文件是存储信息的标准。这还允许您的应用程序将来可能使用标准格式在另一个应用程序之间进行通信。哪些是 xml 文件。

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.

你的背包 2024-12-06 17:52:06

看起来您想要快速轻松地将文件内容映射到 Windows 窗体控件。

如果是这种情况,那么您应该考虑创建一个类来表示 Windows 窗体的各种状态 - 文本框内容位于字符串属性中,复选框作为布尔属性等。

创建该类后,您可以对实例进行数据绑定将其添加到所有 Windows 窗体控件中,这意味着 Windows 窗体数据绑定过程将为您保持 ui 和类同步。

最后一步是保存和加载该数据。为此,请对 C# 序列化进行一些调查,有很多关于如何将类写入文件表示形式的教程,其中您需要做的唯一工作是将一些属性应用于该类,并从该类中调用正确的方法。序列化命名空间。


下面是一些非常快速的代码,显示了带有单个文本框和三个单选按钮的示例表单的数据绑定。您需要针对实际情况进行实验,以获得支持类的正确实现 - 单选按钮和单选按钮组可能有点棘手。

我确信代码可以改进,但它只是为了展示该方法。

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;    
        }
    }
}

当我在上面创建 BindingClass 的实例时,您会从文件中反序列化该类(如果存在)。

这种方法在某种程度上导致了 MVVM 方法,其中类用于支持绑定,就像视图模型一样 - 我建议进入这种思维方式,因为您不希望绑定类开始包含逻辑。它只是为您提供一些可以绑定的东西和一些可以序列化的东西来代表您的表单 - 您应该有其他包含您实际模型的对象的逻辑。


我不会展示序列化的代码 - 网上有很多这方面的示例。以下是 MSDN 文章的链接,这是一个很好的第一步:http:// /msdn.microsoft.com/en-us/library/ms950721.aspx

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

云胡 2024-12-06 17:52:06

如何将文件扩展名关联到C# 中的当前可执行文件 将会有所帮助:答案实际上取决于您如何部署应用程序。

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.

疯到世界奔溃 2024-12-06 17:52:06

文件关联由窗口处理。用户可以知道使用什么类型的应用程序来打开具有特定扩展名的文件。

在您的情况下,您会将 *.ct 与您的 exe 文件相关联。然后将完整路径作为参数提供给主函数。因此,在关联扩展名时,您需要处理文件参数。

对于文件打开和保存,有一个组件可以显示文件打开和保存对话框。您可以将它们配置为仅显示 *.ct。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文