Jump to content

Load\Save Listbox VB.NEt

EmptySoul

Hey

 

I am new to VB.NET, I played around with VB6 almost 10 years ago and am just trying to learn again. I am trying to make a basic program and I need to save a settings file.

 

Name, Department, Account Number, Reference Number.  I only need one account and reference number but multiple names and departments. 

 

I was trying to do it with an INI file and was told that an XML file would be easier and give me more room to expand if need be as it has a lot more options. A basic example of the settings file would be;

 

[Users]

John Smith=Department 1

Jane Smith= Department 2

Alex Smith=Department 1

Oliva Smith=Departmnet 3

[Accounts]

AccountNumber=123456

ReferenceNumber=987654

 

and it to save it into some sort of file so that the program would be able to read and save the data. I need to be able to add and delete users from the program.

 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, EmptySoul said:

I am new to VB.NET, I played around with VB6 almost 10 years ago and am just trying to learn again.

Whatever would posses you to do that? Why not C#?

1 hour ago, EmptySoul said:

I was trying to do it with an INI file and was told that an XML file would be easier and give me more room to expand if need be as it has a lot more options.

Indeed you should be using XML over INI. Have a look at Serialization and LINQ to XML for more granular control over your serialization.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

If you are not familiar with XML, depending on what requirements you need and how Name, Department, Account Number, and Reference Number connect with each other, your XML will look something similar to this as a direct translation from your INI above:

 

<Users>
  <User>
    <Name>John Smith</Name>
    <Department>Department 1</Department>
  </User>
  <User>
    <Name>Jane Smith</Name>
    <Department>Department 2</Department>
  </User>
  <User>
    <Name>Alex Smith</Name>
    <Department>Department 1</Department>
  </User>
  <User>
    <Name>Oliva Smith</Name>
    <Department>Departmnet 3</Department>
  </User>
</Users>

<Accounts>
  <Account>
    <AccountNumber>123456</AccountNumber>
    <ReferenceNumber>987654</ReferenceNumber>
  </Account>
<Accounts>

However, knowing more about your data, you can change it to something that makes more sense like this?:

<Account>
  <AccountNumber>123456</AccountNumber>
  <ReferenceNumber>987654</ReferenceNumber>
  <Users>
    <User>
      <Name>John Smith</Name>
      <Department>Department 1</Department>
    </User>
    <User>
      <Name>Jane Smith</Name>
      <Department>Department 2</Department>
    </User>
    <User>
      <Name>Alex Smith</Name>
      <Department>Department 1</Department>
    </User>
    <User>
      <Name>Oliva Smith</Name>
      <Department>Departmnet 3</Department>
    </User>
  </Users>
<Account>

As you see elements "<...>" can have a child element which gives an information about the parent element, and each child element can have another child element. I would also suggest taking a look at http://www.w3schools.com/xml/. That particular site is more oriented for XML in web, but the basics of XML still apply outside and I find that site as a handy cheat sheet for reference.

 

@Nuluvius posted some great resources, especially about Serialization, about how to serialize/deserialize XML in VB.NET.

 

Is there a particular reason you are using VB.NET, I find it very outdated and C# much easier to work with.

 

Link to comment
Share on other sites

Link to post
Share on other sites

17 hours ago, Nuluvius said:

Whatever would posses you to do that? Why not C#?

Indeed you should be using XML over INI. Have a look at Serialization and LINQ to XML for more granular control over your serialization.

Why not C#? Because he/she is familiar with the syntax of VB and doesn't sound/seem to be in a programming role anymore. XML isn't familiar here & so the idea of serialization and .net namespaces etc. likely to be difficult enough to grasp most likely - LINQ here on top could be daunting & overkill (although a valid & decent point for others reading).

 

@EmptySoul. The reasons INI files aren't preferred anymore are listed here: https://blogs.msdn.microsoft.com/oldnewthing/20071126-00/?p=24383/ & .Net was built with XML being a base for most of the language & environment etc. settings files. It's preferred you use XML when using the .Net platform nowadays. Files (text or XML) are nowadays written with things called streams or serializers to pipe the text data to/from them. You've got a bit of a learning curve here I'm afraid & I'd suggest to read up on these 2 terms then hit the sample links from MSDN provided above.

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, alex_read said:

Why not C#? Because he/she is familiar with the syntax of VB and doesn't sound/seem to be in a programming role anymore. XML isn't familiar here & so the idea of serialization and .net namespaces etc. likely to be difficult enough to grasp most likely - LINQ here on top could be daunting & overkill (although a valid & decent point for others reading).

Why not let OP answer his own questions before jumping to such assumptions and speculations... yes? The information is laid out in those links in a simple way with annotated examples. I suspect that OP is reasonably intelligent and as such can easily follow the concepts further, asking questions when/if necessary.

 

Moreover I fail to see how LINQ would be 'daunting & overkill' as you have described it. LINQ greatly simplifies dealing with XML and there is a class specifically provided for this purpose XDocument. You want an element, you ask for it, you want to save and load the thing... it simply supports that on it's interface. It allows far more granular control over the whole process... Have you even used any of these concepts that you have dammed? I think not, that is my assumption about you ;)

 

In any event it's our responsibility to point out what may or may not be the best choices in relation to what is currently trending in the industry today. This is primarily the reason why I draw such highlight to Visual Basic. It is widely considered to be a deprecated language.

 

Lets get something clear though right now since you're relatively new here: In relation to what I have said above, the language itself is still a tool in the proverbial toolbox and thus still has valid uses - 'the right tool for the right job'. Moreover I don't personally ascribe to hatred or bias of any particular language/technology as I believe that they all have their uses given the appropriate context. The context of Visual Basic is currently legacy and support work.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

I used Visual Basic's Settings.

 

Project -> Properties -> Settings -> Create a setting variable:

Then you can do such things as 

My.Settings.ListBoxCollection.Add(TextBox1.Text)

 

CPU: i5 4670k @ 3.4GHz + Corsair H100i      GPU: Gigabyte GTX 680 SOC (+215 Core|+162 Mem)     SSD: Kingston V300 240GB (OS)      Headset: Logitech G930 

Case: Cosair Vengance C70 (white)                RAM: 16GB TeamGroup Elite Black DDR3 1600MHz       HDD: 1TB WD Blue                              Mouse: Logitech G602

OS: Windows 7 Home Premium                       PSUXFX Core Edition 750w                                                Motherboard: MSI Z97-G45               Keyboard: Logitech G510

Link to comment
Share on other sites

Link to post
Share on other sites

heh, ok quite true, I've used LINQ but not in the context of handling XML, you're correct on that point. ;)  I was concerned of its database like syntax here on top of everything else, but perhaps you're right & it could be useful to the OP too.

 

I'm here to assist not argue. The points I was trying to get across were that (i) I simply don't get the off-the-cuff comment that vb.net is dead and (ii) for the longest time I used to be a self-learnt VB5-6 developer then found myself moving to .net with all the alien OOP concepts & ways of working. If the OP is anything like this (which the XML, serialisation, VB6 mention seems to point toward), the whole thing here could be daunting. The posts you gave although simple to us have a mountain of new keywords, concepts and terms in to anyone new to .net.

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Bittenfleax said:

I used Visual Basic's Settings.

I think you're meaning the Application Settings i.e. .config files - this is a concept across .NET applications. It's a valid point in fact, not only is it XML based but it also supports user accounts (if I recall correctly).

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×