Results 1 to 8 of 8

Thread: My.Settings - adding new values

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    12

    My.Settings - adding new values

    Hello!

    How can i add a new value, from a textbox or listview, without adding manualy in my settings?

    Lets say i have 2 textbox = name, surname and i want to click button "Add username" and then:

    Create My.Settings.Username = "Name" from textbox1
    Create My.Settings.Surname = "Surname" from textbox2

    I dont want to add tose manually, because i have a lot of usernames...

    I tryed this:

    Dim str(2) As String
    Dim itm As ListViewItem
    str(0) = Username.Text
    My.Settings. & UsernamePost.Text = Username.Text

    str(1) = Surname.Text
    My.Settings. & AdresaSop.Text = Surname.Text

    But didnt work.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: My.Settings - adding new values

    You don't add settings at run time. You add them at design time and then set them at run time. In your case, you could use two settings of type StringCollection, where one is the given names and one is the surnames. You can then simply add a new item to each list when you add a new user. You could possibly even use a single StringCollection, where all the even-indexed items are given names and all the odd-indexed items are surnames. Alternatively, you could just use a String that contains multiple values separated by a comma or some other delimiter.

    If you don't want to go that way then the alternative would be to not use My.Settings at all, but rather to create your own class for the data and then serialise/deserialise it manually.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2014
    Posts
    12

    Re: My.Settings - adding new values

    I see... so i think this is a little more difficult, cuz i am a beginer...
    I would like to try ini files with groups it will be easier...

    [Username]
    option1= something
    option2 = something
    etc

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Norway; Haugesund
    Posts
    39

    Re: My.Settings - adding new values

    Quote Originally Posted by afpromania View Post
    I see... so i think this is a little more difficult, cuz i am a beginer...
    I would like to try ini files with groups it will be easier...

    [Username]
    option1= something
    option2 = something
    etc
    As you're a beginner, I will give you a good way to store users. I created some functions so you can easily add, remove, retrieve surname of selected user or retrieve usernames with the same surname. I have also commented each line, so it will be easier to understand what the functions are doing. I hope you learn something from this example!

    Code:
    Private userList As New Hashtable 'Hashtable which holds all the information about the users.
    
        Private Sub addUser(ByVal [Username] As String, ByVal [Surname] As String) 'Call this sub to create a new user
            userList([Username]) = [Surname] 'Adds the surname to the username in the hashtable.
        End Sub
    
        Private Sub deleteUser(ByVal [Username] As String) 'Call this sub to delete a user
            userList.Remove([Username]) 'Removes the user from the hashtable
        End Sub
    
        Private Function getSurnameByUsername(ByVal [Username] As String) ' Call this function to retrieve the surname of user.
    
            Dim Result As String = Nothing
    
            For Each User In userList.Keys 'Searches all the users in the hashtable.
                If User = [Username] Then 'Looks if the current user in the hashtable equals to the username.
                    Result = userList(User) 'User found, returns the users surname.
                End If
            Next
    
            Return Result 'Returns the surname of user
    
        End Function
    
        Private Function getUsernamesBySurname(ByVal [Surname] As String) ' Call this function to retrieve the surname of user.
    
            Dim Result As String = Nothing
    
            For Each User In userList.Keys 'Searches all the users in the hashtable.
                Dim SelectedSurname As String = userList([User]) 'Gets the surname of selected user.
    
                If SelectedSurname = [Surname] Then 'Looks if the current user's surname in the hashtable equals to the username's surname.
                    Result &= Environment.NewLine & (User)  'User found, returns the users surname.
                End If
    
            Next
    
            Return Result 'Returns the surname of user
    
        End Function
    I hope the code I wrote is simple enough for you to understand, and I think of this as a good way to store such information. If you have any questions, or the code didn't get to your expectations, don't be afraid to ask!

    [EDIT]

    I'm not sure if you're familiar with functions yet, but here's how to call them:

    Code:
    'Add user to your collection:
    
    addUser("USERNAME", "SURNAME")
    
    'Remove user from your collection:
    
    deleteUser("USERNAME")
    
    'Retrieves the surname of selected user:
    
    Label1.text = getSurnameByUsername("USERNAME")
    
    'Retrieves the users with the same surname:
    
    RichTextBox1.text = getUsernamesBySurname("SURNAME")
    Note, you don't need to use either the label or the RichTextBox, but what suits you best!

    With kind regards,

    - Frek
    Last edited by Frekvens1; Feb 4th, 2015 at 06:15 AM. Reason: More Info

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,961

    Re: My.Settings - adding new values

    I wouldn't use a hashtable... they pretty much went out of style 5 minutes after they appeared. A Dictionary(Of String, String) would be better. Username would be the key and surname (or what ever you want) would be the value.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    4,553

    Re: My.Settings - adding new values

    I'm unclear if the OP want to save these usernames and surname permanently or temporarily. Would make a big difference on the approach.

  7. #7
    Member
    Join Date
    Oct 2013
    Location
    Norway; Haugesund
    Posts
    39

    Re: My.Settings - adding new values

    Quote Originally Posted by techgnome View Post
    I wouldn't use a hashtable... they pretty much went out of style 5 minutes after they appeared. A Dictionary(Of String, String) would be better. Username would be the key and surname (or what ever you want) would be the value.

    -tg
    Interesting. It seems like I've learnt something new today.

    Cheers,

    - Frek

  8. #8
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: My.Settings - adding new values

    Quote Originally Posted by wes4dbt View Post
    I'm unclear if the OP want to save these usernames and surname permanently or temporarily. Would make a big difference on the approach.
    I'm assuming that they want to persist the data since they first looked at using settings and then at an ini file... I'd think that Settings would be easier to work with since they already have code to read and set their values at run-time. Using an ini on the other hand would require you to make your own procedures to store and retrieve the data. However if you are really set on using INI configuration file, I'd suggest you take a look at the Nini library (see here). I had made a program a good decade ago that used this to create ini configuration files.

    Another option for persisting the data would be to use a database... This could be as simple as a .CSV file (a Comma Separated Value text file) where each field in a record is separated by a comma and each line is a new record / tuple. Creating a CSV file would also be fairly easy to use, esp. with the .Net class TextFieldParser (see here)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width