Saving Controls in C# Windows Application

若如初见. 提交于 2019-12-25 06:29:51

问题


I am creating an application that creates a list of buttons from video files.

Each button is generated to open the file on click, and then I use an open api (omdb.com) to get the movie posters for each one. However, not only is doing this process each time you launch the application slow, but it is also an unnecessary load on the website.

What is the most efficient way to save a control after it is dynamically generated in the backend.

So far I have tried using Properties.Settings, but without any luck.

SettingsProperty newSetting = new SettingsProperty("Movie_Button");
newSetting.Name = "Movie_Button_" + *an int that I generate*.ToString();
newSetting.Attributes.Add(Width, 200);
//Carry on setting attributes

So far this process hasn't yielded any results, even when just trying to create a SettingsProperty that's just a simple string. And I have tried with and without Properties.Settings.Default.Save().

Is there a way to save a control created at run time so it carries over to the next launch of the application?

If there is no way to do this, then can someone please help me create the new SettingsProperty to hold a string, or possibly an XML that I could use.


回答1:


Since I couldn't find a way to save a control, I decided to instead save the images in a new folder and then create an XML containing the path to the image (and some additional information such as title, year, etc.).

To create the XML I used the XmlDocument doc = new XmlDocument(); method, downloaded the images to a folder at the exe path (using Application.ExecutablePath to get the path), then WebClient webclient = new WebClient(); and webclient.DownloadFile(netPath, localPath) to download each image.

Then on form load I regenerate the buttons using the info from the XML.

This method is much faster than my original method of making multiple web requests for the image url or calls to the api.



来源:https://stackoverflow.com/questions/25214778/saving-controls-in-c-sharp-windows-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!