0

I have managed to create the button but whenever i close the program and reload it the button and details are no longer there and it all needs to be created again. How can i set it to save the buttons details so they show up on next boot.

Here's the code to populate the button

Button BB = new Button();
BB.Location = new System.Drawing.Point(710,500);
BB.Name ="Button " 
BB.Size = new Size(136,100);
BB.UseVisualStyleBackColor = true;
this.Controls.Add(BB);

MessageBox.Show("Button Created");
dovid
  • 6,354
  • 3
  • 33
  • 73
Kentao
  • 197
  • 1
  • 1
  • 11
  • Use the designer view instead. – BartoszKP Jan 05 '14 at 15:53
  • When is this code called? – Sam Leach Jan 05 '14 at 15:53
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jan 05 '14 at 15:54
  • You could save this information in your app settings. – Max Jan 05 '14 at 15:55
  • Its called from a MenuStrip. The idea is for the user to add website urls to different buttons to make it easier for children to explore the internet. – Kentao Jan 05 '14 at 15:59
  • Is their a way to import the button create information into the Designer.cs when the button is created? – Kentao Jan 05 '14 at 16:06
  • 1
    You might find this useful: http://stackoverflow.com/questions/10739641/how-i-can-save-controls-created-in-run-time-in-windows-forms – Dimitar Dimitrov Jan 05 '14 at 16:10
  • Anything created in the memory (= at runtime, via code) is lost when the application is closed. Anything you want to keep, has to be stored externally (file, database, etc.) and then retrieved again. For example, you might store all the properties for each button in a CSV file, which would be loaded (and the buttons, created and populated with these properties) at the start of the program. The button you show might be stored as (1 line of the file): 710, 500, Button ,136, 100, true – varocarbas Jan 05 '14 at 16:14
  • Thank you Dimitar i`ll try it out and let you know how i get on but it looks similar to the problem i am having. Thanks again – Kentao Jan 05 '14 at 16:14

1 Answers1

1

If you need to add more than one button you should consider serialization.

Take a look at XmlSerializer, SoapFormatterand/or BinaryFormatter. The three of them would allow you to save a whole bunch of buttons properties and when your application is launched to reload them and create them again.

Irwene
  • 2,807
  • 23
  • 48
  • Can you please clarify what you mean with "If there is only one button, you can save it's settings in your app settings"? It is possible to modify information at runtime and retrieve these modifications in subsequent executions? – varocarbas Jan 05 '14 at 16:19
  • I meant if he only want to save the informations about only one button. Because i dont' remember you can add fields in your app settings dynamically. – Irwene Jan 05 '14 at 16:21
  • No, you cannot add fields dynamically; exactly the same that you cannot modify the contents of any existing field UNLESS relying on an external file. But what you see at design time in VS cannot be affected at all at runtime. – varocarbas Jan 05 '14 at 16:24
  • Yes, because the application settings are stored in the AppData folder of the current user. So you are relying on an external storing element but this is entirely automated. – Irwene Jan 05 '14 at 16:24
  • No, it is not automated. Actually, accessing these resources is less intuitive and evident than setting a simple streamwriter and write to a temporary file. In any case... please, clarify this issue: you have to write to a file (or equivalent) no matter what. – varocarbas Jan 05 '14 at 16:25
  • It would be for multiple buttons. – Kentao Jan 05 '14 at 16:30
  • Ok i just checked that on a dummy project. In a c# project you have a `Properties` node. Under that properties node, you have the `Settings.settings` file. If you edit this file you can add fields and even default values. Then to access it from your code you just have to type `Properties.Settings.Default.` – Irwene Jan 05 '14 at 16:33
  • As explained: less intuitive than a simple streamwriter. You can affect settings either by using the "runtime means" (accessing the assembly, etc.), what is very un-intuitive, or, directly, locating (creating) the file and accessing it as a normal file (but by making sure that you "respect its rules", otherwise you might provoke an eror somewhere). With not being intuitive I referred to the first option (accessing the assembly), but also works for the file. In any case, there is no "automatic treatment" as anyone would understand, that is, a property like .Add. You have to modify the files... – varocarbas Jan 05 '14 at 16:42
  • ... even in case of forgetting about all this and seriously wanting to rely on a so inflexible format, you might store as many buttons as you wish (your restriction of just one button is arbitrary). So... please, update your answer to reflect the reality, that is: any change performed at runtime HAS TO be stored in an external format (like a file); accessing external files is extremely easy in .NET and gives you full freedom; if you prefer to restrict yourself to the limited availability of Settings, it is completely up to you, but remember that this is also a file. – varocarbas Jan 05 '14 at 16:44
  • I agree on the point that the settings file is not really flexible and that it is an external file. But please remember that i offered a better way to store multiple settings through the serialization process wich allow to save in a external file of you choice. Anyway the author confirmed that it was for multiple buttons so i'll just delete the settings possibility – Irwene Jan 05 '14 at 16:48
  • I did +1 you but actually, the fact of being 1 button or 1000 does not change anything, you can choose the alternative you want (and you keep saying that it matters at all). The whole point of my comments was highlighting that you have to make clear a point which does not seem to be clear to the OP, that is: buttons or text or settings or anything, have to be stored externally in order to be retrieved in subsequent executions. – varocarbas Jan 05 '14 at 16:53