简体   繁体   中英

User control could not add to winform?

I am working on winform application. I have one user control in my application as a part of same project. I have written following code in the constructor of that user control

 public ctrlCurrentLocation()
    {            
        InitializeComponent();
        string NewLine = System.Environment.NewLine;
        string strHeader = string.Concat("<?xml version=\"1.0\"?>", NewLine, "<TrackMap>", NewLine);
        string strLast = strHeader + string.Concat("</TrackMap>", NewLine);
        strXMLPath = AppDomain.CurrentDomain.BaseDirectory + "JavaScript\\TrackMap.xml";
        FileStream fs1 = File.Open(strXMLPath, FileMode.Create);
        StreamWriter writer1 = new StreamWriter(fs1, Encoding.UTF8);
        writer1.Write(strLast);
        writer1.Close();
        fs1.Dispose();
        .......
        .
        .
    }

Now after building the solution, this user control appears in toolbox. When i am trying to drag this user control in my mainForm, it throws design time exception saying

在此处输入图片说明

What could be the reason, it throws error at line

FileStream fs1 = File.Open(strXMLPath, FileMode.Create);

AppDomain.CurrentDomain.BaseDirectory is returning C:\\Program Files...\\IDE when in design mode. That's not where your TrackMap.xml file is.

You can add a property to the UserControl, example ctrlCurrentLocation.MapFilePath , that can be set in the designer. The userControl is then refreshed with the data of the file.

Example:

private string _mapFilePath = null;

public ctrlCurrentLocation()
{            
    InitializeComponent();
}

private void ReloadMap()
{
    if(_mapFilePath != null && File.Exists(_mapFilePath))
    {
        string NewLine = System.Environment.NewLine;
        string strHeader = string.Concat("<?xml version=\"1.0\"?>", NewLine, "<TrackMap>", NewLine);
        string strLast = strHeader + string.Concat("</TrackMap>", NewLine);
        strXMLPath = AppDomain.CurrentDomain.BaseDirectory + "JavaScript\\TrackMap.xml";
        FileStream fs1 = File.Open(strXMLPath, FileMode.Create);
        StreamWriter writer1 = new StreamWriter(fs1, Encoding.UTF8);
        writer1.Write(strLast);
        writer1.Close();
        fs1.Dispose();
        .......
        .
        .
    }
}

public string MapFilePath
{
    get { return _mapFilePath; }
    set
    {
        _mapFilePath = value;
        ReloadMap();
    }
}

The code that uses the map path was moved from the constructor to its own method. This method is then called whenever the MapFilePath property changes.

This property will appear in the Properties panel of the designer because it is a public property of the UserControl class. There you can paste the file path and the code of ReloadMap will be executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM