C# forms loading and saving  
Author Message
Peter De





PostPosted: Visual C# Express Edition, C# forms loading and saving Top

Hi

i want to produce a app for testing different devices connect to the computer, and each device has different user interface. Im thinking the best way is to have a MDI form, then load a child form for each different device.

Im thinking the best way to do this would be to be able to "save" a form to the HD, then load it when a user selects the device in the MDI.

Is there a way to save a form, with all its buttons and executable events and load it as required, for each device, or do forms have to compiled into the executeable.

Or is there a better way



Visual Studio Express Editions36  
 
 
rauhanlinnake





PostPosted: Visual C# Express Edition, C# forms loading and saving Top

If the forms are pre-defined, so they appear every time in the same way, I think there is no reason to serialize the whole forms to the hard disk. Create needed forms with visual studio, and compile them as usually.

If you need to save the device forms' states, save the values of the controls to the file, and read when necessary. You could use the Settings functionality of Visual Studio 2005, or create your own settings objects and serialize them in a file. You dont need to save the whole form object to regain its state. When the form reloads, read and apply the settings, and user can't see the difference ;)

Here's one example how to use custom settings objects.

    [Serializable]
    public class FormSettings
    {
        public FormSettings() //Important to have when using XmlSerializer
        {
        }

        private string _textBoxText;

        public string TextBoxText
        {
            get { return _textBoxText; }
            set { _textBoxText = value; }
        }
        private int _comboBoxSelectedIndex;

        public int ComboBoxSelectedIndex
        {
            get { return _comboBoxSelectedIndex; }
            set { _comboBoxSelectedIndex = value; }
        }
    }

            //Creating settings object
            FormSettings settings = new FormSettings();
            settings.ComboBoxSelectedIndex = 3;
            settings.TextBoxText = "Hello";

            //Writing to xml file
            FileStream fileStream = new FileStream("mySettingsFile.xml", FileMode.Create);
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(
                typeof(FormSettings));
            serializer.Serialize(fileStream, settings);
            fileStream.Close();

            //Writing to binary file
            FileStream fileStream2 = new FileStream("mySettingsFile.bin", FileMode.Create);
            BinaryFormatter serializer2 = new BinaryFormatter();
            serializer2.Serialize(fileStream2, settings);
            fileStream2.Close();

Reading of the settings is as easy:

            FileStream fileStream3 = new FileStream("mySettingsFile.bin", FileMode.Open);
            BinaryFormatter serializer3 = new BinaryFormatter();
            FormSettings settings2 = (FormSettings)serializer3.Deserialize(fileStream3);
            fileStream3.Close();

You may want to notice, that this approach will require admin rights, if used wrong. You must select the paths so that user has rights to write and read the settings. You can use the users temp (you can get the temp path using Path-class), or Isolated Storages. Check out this tutorial: http://www.dotnetdevs.com/articles/IsolatedStorage.aspx

 
 
Peter De





PostPosted: Visual C# Express Edition, C# forms loading and saving Top

Hi thanks for your response.

I dont need to save the state of the forms or anything, so the forms/interfaces for the devices will be static/unchanged everytime they are used/shown, so yes i could compile them into the exe as normal.

however, suppose i want to add new devices on an ongoing basis and i end up with 500-1000 different hardware devices each with a difference interface.

Should i then addopt the serialiaztion method do you think,

i guess i was wondering can i design the form interface in c# express designer, then save that one form to HD with specific name, not as part of a project or anything, but just the form code, then load that individual saved form for each different device, as a child form, when the user selects a device from a list i put into the parent mdi application.

This way if it is possible i could write the main controlling application, then create a new individual interface form for new devices as i go along, have the new forms stored in a directory for the user to load and use, and i could also distribute new forms/interfaces for users without having to send a new full application each time, if i were to compile the forms into the exe itself.

thanks for any advice


 
 
rauhanlinnake





PostPosted: Visual C# Express Edition, C# forms loading and saving Top

Oh now i'm getting into the point.

So, all devices hasn't got similar form, right I would not distribute the source code or anything that is compiled runtime when showing the form, but I would do a abstact class or interface, that is used to create a plugin dlls. Yes, the plugins is what you need.

Ok, how's I'd do it:

1) Create main project, that contains the main program (exe)

2) Add a new project to the solution, that is the plugin interface project. Generates the dll that is used to inherit the plugins. In main project, add reference to this project

3) Add a new project for the default plugins you wish to distribute with the app. You can include all default plugins in one dll, or add a separate project for each plugin. You just have to load all types from the dll and inspect them all when loading the plugin. Add reference to the plugin interface project


4) In Plugin interface project, create new interface and possibly abstact class so you can inherit the plugins from common base. These types will be usable in both main program and plugin dlls.

public interface IPluginInterface
{
//Interface methods declarations here
}

public abstract class PluginForm : Form, IPluginInterface
{
//Abstract, virtual and regular methods here. Implenent the interface.
}

5) In plugins project(s), create new classes that are inherited from the plugin form or are implementing the interface:

    public class Plugin : PluginForm
    {
        //override the virtual methods here
    }

6) When solution compiles, copy the plugin dll's to predefined subdirectory inside the main programs debug or release directories

7) In main program, when the user selects the device, search and dynamically load the correct plugin dll that matches the device user has selected. You can use Assembly class to load a dll from hard disk.


You must include information inside the plugins, so the right one can be selected. This can be achieved by using custom attributes, or interface methods that provide the main form with the information. Basically the loading goes so, that you search every dll inside the folder, using FileInfo class, and the load the file using Assembly.LoadFile, then inspect the interfaces that it has, and the attributes that it has to ensure that it is a plugin and suites to the selected device. Here's some dll loading examples you to look at.  Not the best example, but just do the loading and inpecting in a foreach loop.

So, now you can just publish new dlls, and when user (or installer) places them in the plugins directory, the new device plugins are ready for the user.

 
 
Peter De





PostPosted: Visual C# Express Edition, C# forms loading and saving Top

Sounds good, not an approach i would have considered.

i will try this, thanks for your help