Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2016-06-19 17:01:44

Latif
Member
From: The Netherlands
Joined: 2015-03-13
Posts: 1,206

[Help] Saving listboxes

I'm trying to save the items in my listbox to settings. But I don't know what the type of the listbox is. So I tried 'System.Collections.Specialized.StringCollection'. But I get an error.. This is my code:

//If you open the form
Listbox.Items = Properties.Settings.Default.Listbox;

//If you close the form
Properties.Settings.Default.Listbox = Listbox.Items;
Properties.Settings.Default.Save();

But then I get these errors:

Cannot implicitly convert type 'System.Collections.Specialized.StringCollection' to 'System.Windows.Forms.ListBox.ObjectCollection'
Cannot implicitly convert type 'System.Windows.Forms.ListBox.ObjectCollection' to 'System.Collections.Specialized.StringCollection'
Property or indexer 'System.Windows.Forms.ListBox.Items' cannot be assigned to -- it is read only

So I opened the settings with XML (text) Editor and changed 'System.Collections.Specialized.StringCollection' into 'System.Windows.Forms.ListBox.ObjectCollection'.
Then I only have this error left.. But I don't know how to fix this. Can someone help me please!

Property or indexer 'System.Windows.Forms.ListBox.Items' cannot be assigned to -- it is read only
---> Or am I doing it wrong? <---

Offline

#2 2016-06-19 17:11:35, last edited by Koya (2016-06-20 12:50:30)

Koya
Fabulous Member
From: The island with those Brits
Joined: 2015-02-18
Posts: 6,310

Re: [Help] Saving listboxes

I made an example application quickly to convert a Listbox into a file in both XML and JSON: https://i.koya.io/XMLJSONListbox.zip (3MB) | http://pastebin.com/eScX6wQX
Everything is fairly clear (for my eyes at least), but if you need help with anything in it be sure to ask here or PM me.

Because this is probably a common question for people who like their GUIs or want to save and load lists I'll explain how to save and load with XML and JSON (IMO JSON is perfect and should be tried before XML)
While it seems that general programming isn't permitted by some here, I had a bad first experience with stack overflow and when searching for something online when you are new you don't know if what you are looking at is what you need and you get errors you don't understand when you try it - I welcome any question related to C# if you PM me.

Note before I start: I am using C#6 in Visual Studio 2015, I have some instances of things like `$"Save {fileFormat.Text} File"` - if you are using older versions it will have to be changed to `"Save " + fileFormat.Text + " File";` or `string.Format("Save {0} File", fileFormat.Text)`

-

The process for saving is:
• Make a listbox class, something as simple as

For JSON or XML:

public class listbox /*Must be a parameterless constructor for XML*/
{
    public List<string> items;
}

or
For JSON:

public class listbox
{
    public List<string> items { get; set; }
    public void listbox(List<string> Items = null) { /*This creates errors when using XML*/
        items = Items
    }
}

• Choose a save location, depending on if you want people to be able to load and save different files.
To open up a save file diologue box you will need to create a new 'saveFileDialog' in the designer and use:

saveFileDialog.Title = $"Save {fileFormat.Text} File";
saveFileDialog.Filter = $"{fileFormat.Text.ToUpper()}|*.{fileFormat.Text.ToLower()}";
saveFileDialog.FilterIndex = 0;
saveFileDialog.OverwritePrompt = true;
if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
{
    saveLocation = saveFileDialog.FileName;
}

The file will be placed at saveLocation

• Now assign the items in the Listbox as a List<string>
Within the Form class I have created a list which will temporarily hold the items from the Listbox while saving

private List<string> tmpItems = new List<string>();

After opening up and selecting a save location you now need to populate the temporary items list with the items from the Listbox, don't forget to clear it beforehand

tmpItems.Clear();
foreach (var item in listBox1.Items)
{
    tmpItems.Add(item.ToString());
}

• Create a new listbox object
For JSON or XML:

listbox lb = new listbox();
lb.items = tmpItems;

or
For JSON:

listbox lb = new listbox(tmpItems);

So much cleaner

• Convert and save the file
You just say where it is being saved to [saveLocation], what template to use (class) [lb] and the object to get the data from [lb].
To save as XML

StreamWriter sw = new StreamWriter(saveLocation);
new XmlSerializer(lb.GetType()).Serialize(sw,lb); /*Write class to file as XML*/
sw.Close();

or
To save as JSON

File.WriteAllText(saveLocation, JsonConvert.SerializeObject(lb));

You now have your listbox saved as either XML or JSON, examples:

XML:

<?xml version="1.0" encoding="utf-8"?>
<listbox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <items>
    <string>toupime</string>
    <string>zaib</string>
    <string>phethuge</string>
    <string>ulonase</string>
    <string>gousate</string>
    <string>shenu</string>
    <string>souw</string>
    <string>entante</string>
    <string>tupi</string>
    <string>irpeft</string>
    <string>viffoste</string>
    <string>zoath</string>
    <string>gaiquadge</string>
    <string>woutche</string>
    <string>ifent</string>
    <string>liekejenost</string>
    <string>nea</string>
    <string>opiffe</string>
    <string>mipuchare</string>
    <string>gouspitch</string>
    <string>zokebriz</string>
    <string>skistund</string>
    <string>inde</string>
    <string>vasutchimge</string>
    <string>omgitode</string>
    <string>lieskegiw</string>
    <string>itedafe</string>
    <string>snu</string>
    <string>moo</string>
    <string>clanaz</string>
    <string>wichessend</string>
    <string>diglen</string>
    <string>dosiestot</string>
    <string>odgecoiso</string>
    <string>shempuspeft</string>
    <string>be</string>
    <string>maisusperp</string>
    <string>woiballe</string>
    <string>tudgekludie</string>
    <string>adgege</string>
    <string>thipepuw</string>
    <string>doreloasp</string>
    <string>stoth</string>
    <string>quaff</string>
    <string>ewiend</string>
    <string>weenkizart</string>
    <string>twefesung</string>
    <string>acha</string>
    <string>awumole</string>
    <string>glemgaft</string>
    <string>chu</string>
    <string>unk</string>
    <string>ecot</string>
    <string>cetchazofe</string>
    <string>navew</string>
    <string>prubeebu</string>
    <string>sice</string>
    <string>utushu</string>
    <string>edemele</string>
    <string>ath</string>
    <string>wurpith</string>
    <string>doafepouki</string>
    <string>aspi</string>
    <string>illift</string>
    <string>soikece</string>
    <string>ewolle</string>
    <string>ungukreth</string>
    <string>dielli</string>
    <string>semald</string>
    <string>wigontot</string>
    <string>couffifequu</string>
    <string>gai</string>
    <string>mekedaz</string>
    <string>clapacurt</string>
    <string>tretudow</string>
    <string>cresagug</string>
    <string>uwore</string>
    <string>twempu</string>
    <string>bu</string>
    <string>curedowirte</string>
    <string>ebleredu</string>
    <string>choght</string>
    <string>doungapabe</string>
    <string>dutebe</string>
    <string>omefe</string>
    <string>laspump</string>
    <string>bace</string>
    <string>thimp</string>
    <string>mookestuz</string>
    <string>muff</string>
    <string>nibgaim</string>
    <string>streso</string>
    <string>nouploste</string>
    <string>opoiweb</string>
    <string>umgishe</string>
    <string>phicob</string>
    <string>loussisone</string>
    <string>bauske</string>
    <string>ca</string>
    <string>shuspineale</string>
  </items>
</listbox>

JSON:

{"items":["phethuge","ulonase","gousate","shenu","souw","entante","tupi","irpeft","viffoste","zoath","gaiquadge","woutche","ifent","liekejenost","nea","opiffe","mipuchare","gouspitch","zokebriz","skistund","inde","vasutchimge","omgitode","lieskegiw","itedafe","snu","moo","clanaz","wichessend","diglen","dosiestot","odgecoiso","shempuspeft","be","maisusperp","woiballe","tudgekludie","adgege","thipepuw","doreloasp","stoth","quaff","ewiend","weenkizart","twefesung","acha","awumole","glemgaft","chu","unk","ecot","cetchazofe","navew","prubeebu","sice","utushu","edemele","ath","wurpith","doafepouki","aspi","illift","soikece","ewolle","ungukreth","dielli","semald","wigontot","couffifequu","gai","mekedaz","clapacurt","tretudow","cresagug","uwore","twempu","bu","curedowirte","ebleredu","choght","doungapabe","dutebe","omefe","laspump","bace","thimp","mookestuz","muff","nibgaim","streso","nouploste","opoiweb","umgishe","phicob","loussisone","bauske","ca","shuspineale"]}

In this example JSON is ~1MB while XML is ~4MB, this should be taken into consideration if storage space is short (also it's mean to users to take up unnecessary space)

-

The process for loading is:

You will require the same listbox class from above, whether you are using the one for XML & JSON or JSON.
• Choose a save location, depending on if you want people to be able to load and save different files.
To open up a save file diologue box you will need to create a new 'saveFileDialog' in the designer and use:

openFileDialog.Title = "Select a File";
openFileDialog.Filter = fileFormat.Text == "XML" ? "XML|*.xml|JSON|*.json" : "JSON|*.json|XML|*.xml"; /*Default preference for the combobox file type selection*/
saveFileDialog.FilterIndex = 0;
if (openFileDialog.ShowDialog() != DialogResult.Cancel)
{
    loadLocation = openFileDialog.FileName;    
}
else
{
    loadLocation = "";
}

The place to load the data from is loadLocation.

• Create a new listbox object and populate it from a deserializer

listbox lb = new  listbox();

For XML:

lb = (listbox)new XmlSerializer(lb.GetType()).Deserialize(new StringReader(File.ReadAllText(loadLocation)));

For JSON:

lb = JsonConvert.DeserializeObject<listbox>(File.ReadAllText(loadLocation));

• Finally to populate the listbox with the saved data:

listBox1.Items.Clear();
foreach (var item in items)
{
    listBox1.Items.Add(item);
}

-

You can now easily save data whether it be for admin lists, ban lists or logs. You can also create a custom class and use the same process - just forget about anything that says listBox1 in the example.
So you can save any settings you want, making it more pleasant to use your bot and not have everything lost when you close - if you make an auto-login feature, it is fine just also make an option to stop auto logging-in.


Po9cnQh.png

PLNQVL8.png
Thank you eleizibeth ^

1SYOldu.png

I stack my signatures rather than delete them so I don't lose them
giphy.gif

WfSi4mm.png

Offline

Wooted by:

#3 2016-06-19 23:40:49

Tomahawk
Forum Mod
From: UK
Joined: 2015-02-18
Posts: 2,847

Re: [Help] Saving listboxes

Try converting each item in the list box to string, then saving those strings in an array. You can use a StringCollection, or do the following to save a list directly:
http://stackoverflow.com/questions/2890 … gs-default

Can we keep this forum section centred around bots, and not general C#? Please use google for questions like this.


One bot to rule them all, one bot to find them. One bot to bring them all... and with this cliché blind them.

Offline

#4 2016-06-19 23:56:26

Koya
Fabulous Member
From: The island with those Brits
Joined: 2015-02-18
Posts: 6,310

Re: [Help] Saving listboxes

Tomahawk wrote:

Can we keep this forum section centred around bots, and not general C#? Please use google for questions like this.

It's not like this forum is being spammed, it's a nicer community than stack exchange - bot related or not it should be welcome here.


Po9cnQh.png

PLNQVL8.png
Thank you eleizibeth ^

1SYOldu.png

I stack my signatures rather than delete them so I don't lose them
giphy.gif

WfSi4mm.png

Offline

Wooted by:

#5 2016-06-20 01:04:26

Xfrogman43
Member
From: need to find a new home
Joined: 2015-02-15
Posts: 4,174

Re: [Help] Saving listboxes

Rurigok wrote:

This section of the forums is for any and all bots, tools, and software related to EE - finished, still in progress, or even in the conceptual stage. Be sure to keep all software legal and trustworthy; any suspicious or illegal software found here will be removed and the accounts responsible will be banned regardless of warnings.

This says "any and all bots, tools, and software related to EE. I'm sorry, but this has to be EE-only.


zsbu6Xm.png thanks zoey aaaaaaaaaaaand thanks latif for the avatar

Offline

Wooted by:

#6 2016-06-20 07:29:50

Tomahawk
Forum Mod
From: UK
Joined: 2015-02-18
Posts: 2,847

Re: [Help] Saving listboxes

Regardless, it took me all of 5 seconds to find that stack overflow link, so there's no reason for this thread to exist.


One bot to rule them all, one bot to find them. One bot to bring them all... and with this cliché blind them.

Offline

Wooted by: (2)

#7 2016-06-20 10:57:35

MartenM
Member
From: The Netherlands
Joined: 2016-03-31
Posts: 978
Website

Re: [Help] Saving listboxes

Would recommend to just save it to .txt files.


lm3hgg8.jpg

Ingame: marten22        My steam: MartenM

Offline

#8 2016-06-20 12:05:50

Koya
Fabulous Member
From: The island with those Brits
Joined: 2015-02-18
Posts: 6,310

Re: [Help] Saving listboxes

MartenM wrote:

Would recommend to just save it to .txt files.

Would definitely not recommend that!
Saving things as JSON is the way to go, you can't go wrong with it - example usage of JSON and how easy it is to store and fetch data


Po9cnQh.png

PLNQVL8.png
Thank you eleizibeth ^

1SYOldu.png

I stack my signatures rather than delete them so I don't lose them
giphy.gif

WfSi4mm.png

Offline

#9 2016-06-20 13:52:05, last edited by Latif (2016-06-20 13:52:54)

Latif
Member
From: The Netherlands
Joined: 2015-03-13
Posts: 1,206

Re: [Help] Saving listboxes

Woah! Thanks Koya //forums.everybodyedits.com/img/smilies/big_smile
You really helped me a lot! Thank you very much! And sorry for the late reply..

And I thought the name of this forum section is 'Bots and programming'...
But I think it's only for EE now. I'm sorry. Next time I won't ask questions like this.

Offline

#10 2016-06-20 13:53:10, last edited by Koya (2016-06-20 13:53:47)

Koya
Fabulous Member
From: The island with those Brits
Joined: 2015-02-18
Posts: 6,310

Re: [Help] Saving listboxes

latif wrote:

Woah! Thanks Koya //forums.everybodyedits.com/img/smilies/big_smile
You really helped me a lot! Thank you very much! And sorry for the late reply..

And I thought the name of this forum section is 'Bots and programming'...
But I think it's only for EE. I'm sorry. Next time I won't ask questions like this.

I thought it was about all programming too, feel free to message me for help if you need.


Po9cnQh.png

PLNQVL8.png
Thank you eleizibeth ^

1SYOldu.png

I stack my signatures rather than delete them so I don't lose them
giphy.gif

WfSi4mm.png

Offline

Wooted by:

#11 2016-06-20 17:11:25

Processor
Member
Joined: 2015-02-15
Posts: 2,246

Re: [Help] Saving listboxes

Closed because this topic is not EE related.

These forums are only meant for EE bot programming. If you need general programming help, there are a lot of other places on the internet you can get help from.


I have never thought of programming for reputation and honor. What I have in my heart must come out. That is the reason why I code.

Offline

Wooted by: (2)
Processor1466439085607969

Board footer

Powered by FluxBB

[ Started around 1732741083.6283 - Generated in 0.428 seconds, 13 queries executed - Memory usage: 1.62 MiB (Peak: 1.84 MiB) ]