Visual Studio Development Bookmark and Share   
 index > Visual Basic Express Edition > Need some help please on coding
 

Need some help please on coding

Hello there, i have made a another program which saves all my CD keys and passwords so on here is the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ComboBox1.Text = "" Then MsgBox("Please Select an item", MsgBoxStyle.Information)

        If ComboBox1.Text = "SWAT4" Then
            TextBox1.Text = "***********************"
        ElseIf ComboBox1.Text = "SWAT4 STETCHKOV SYNDICATE" Then
            TextBox1.Text = "*********************"
        ElseIf ComboBox1.Text = "RAINBOW SIX VEGAS" Then
            TextBox1.Text = "*******************"
        ElseIf ComboBox1.Text = "HUB PASSWORD" Then
            TextBox1.Text = "****************"
        End If

    End Sub

Ok the thing i want to do is add a button that will allow me to add another item and put its Code/password in, and add it to the combo list without of me going back to VB and so on..anyone know the code?

Thanks
Asim
Asim09  Wednesday, October 14, 2009 10:10 PM
hmm.. about your form2 showing before form1, i did not find anything, other than adding Form1_FormClosing and _Load in form2.  code still worked.. odd.
--
anyways, as i stated in a previous reply containing a link, i managed to to clean up the code for your project, even add some extra goodies.. hope it helps..

try the following w/a new project.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "username" And TextBox2.Text = "password" Then
            MsgBox("Access Granted", MsgBoxStyle.Information)
            Form2.Show()
            Form2.Location = Me.Location
            Me.Hide()
        Else
            MsgBox("Access Denied, bad username or password", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Timer1.Interval = 1
        TextBox1.Text = "username"
        TextBox2.Text = "password"
        TextBox2.PasswordChar = "*"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label3.Text = (TimeOfDay)
    End Sub

End Class

-------------------------------

Public Class Form2

    Dim userdir As String = (Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
    Dim names As String = (userdir & "\appnames.txt")
    Dim serials As String = (userdir & "\appserials.txt")
    '---  create invisible listbox
    Dim invisiblelistbox As New ListBox

    Public Sub WriteItems(ByVal filename As String, ByVal items As IEnumerable)
        Try
            Dim w As New IO.StreamWriter(filename)
            For Each itm In items
                w.WriteLine(itm)
            Next
            w.Close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Function ReadItems(ByVal filename As String) As String()
        Return IO.File.ReadAllLines(filename)
    End Function

    Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '---- save items to files
        WriteItems(names, ComboBox1.Items)
        WriteItems(serials, invisiblelistbox.Items)
        '----- exit app'
        Application.Exit()
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "save"
        TextBox2.Text = "app name"
        TextBox3.Text = "serial code"
        Try
            ComboBox1.Items.AddRange(Me.ReadItems(names))
            invisiblelistbox.Items.AddRange(Me.ReadItems(serials))
        Catch ex As Exception
            MsgBox("if this is first install, then press ok to proceed." _
            & vbCrLf & "otherwise, your files have been deleted/relocated.", MsgBoxStyle.Information)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ComboBox1.Items.Add(TextBox2.Text)
        invisiblelistbox.Items.Add(TextBox3.Text)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        invisiblelistbox.SelectedIndex = ComboBox1.SelectedIndex
        TextBox1.Text = invisiblelistbox.SelectedItem
    End Sub

End Class
--------------

now, about selecting your folder to save your password/apps, you will definitely need to use my.settings for such..
then, at the top of form2, the userdir would equal your settings, not the desktop directory.
and simply add code for a folderbrowser dialog in the try..catch of form2_load, to allow user to select a folder location if first install, files are missing/deleted/etc..

do be aware, some folders will not allow users to write files to, so your best bet is to use this..

   Dim userdir As String = (Application.StartupPath)

and learn about using my.settings for the entire load/save of items.
....
if you need further help pertaining to this thread, do ask, otherwise, start a new thread for questions, and mark your helpfuls and answers on this thread.

i live here and this is my reason.. trujade.
  • Marked As Answer byAsim09 Thursday, October 15, 2009 9:42 PM
  •  
trujade  Thursday, October 15, 2009 7:00 PM
You have to create storage to store the information. So when you run your program, it reads the information and add them to combobox. You can use XML, database or text file. Any items you add to combobox at runtime without save it will be gone when you exit your program.

kaymaf
I hope this helps, if that is what you want, just mark it as answer so that we can move on
kaymaf  Wednesday, October 14, 2009 10:36 PM
as kaymaf mentioned, you have to store the info somewhere.. i would advise using my.settings, to better secure your passwords, but i prefer files, that i can view..

here is an idea on how to achieve a solution...

new project, 1 combobox, 1 button, 3 textboxes.
Public Class Form1

    Dim names As String = ("C:\Users\trujade\Desktop\appnames.txt")
    Dim serials As String = ("C:\Users\trujade\Desktop\appserials.txt")
    '---  create invisible listbox
    Dim invisiblelistbox As New ListBox

    Public Sub saveinfo()
        Try
            Dim w As IO.StreamWriter
            w = New IO.StreamWriter(names)
            For Each itm In ComboBox1.Items
                w.WriteLine(itm)
            Next
            w.Close()
            Dim w2 As IO.StreamWriter
            w2 = New IO.StreamWriter(serials)
            For Each itm In invisiblelistbox.Items
                w2.WriteLine(itm)
            Next
            w2.Close()
        Catch ex As Exception
        End Try
    End Sub

    Public Sub loadinfo()
        Try
            Dim s As New IO.StreamReader(names)
            Dim line As String
            line = s.ReadLine()
            Do While Not (line Is Nothing)
                line = line.Trim()
                If line.Length > 0 Then ComboBox1.Items.Add(line)
                line = s.ReadLine()
            Loop
            s.Close()
            Dim s2 As New IO.StreamReader(serials)
            Dim line2 As String
            line2 = s2.ReadLine()
            Do While Not (line2 Is Nothing)
                line2 = line2.Trim()
                If line2.Length > 0 Then invisiblelistbox.Items.Add(line2)
                line2 = s2.ReadLine()
            Loop
            s2.Close()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        saveinfo()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadinfo()
        Button1.Text = "save"
        TextBox2.Text = "app name"
        TextBox3.Text = "serial code"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ComboBox1.Items.Add(TextBox2.Text)
        invisiblelistbox.Items.Add(TextBox3.Text)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        invisiblelistbox.SelectedIndex = ComboBox1.SelectedIndex
        TextBox1.Text = invisiblelistbox.SelectedItem
    End Sub

End Class


if you have any questions about the above code, do ask..
another thing, do not add items to the combobox/invisiblelistbox, unless done at run time.. this way, your indexes remain equal, and will respond for each listing...
i live here and this is my reason.. trujade.
trujade  Wednesday, October 14, 2009 11:53 PM
sounds like some type of database would be the way to go.

If you don't want to set up a database, here is an example using a DataTable and an XML file as kaymaf said. This is the bare bones code - of course you'll want to put in some validation to make sure you don't add the same CD twice, etc.

need to change the filename to your own. Button1 saves the data, Button2 reads the XML file, and Button3 adds the data from textboxes 2 and 3 to the datatable.

Dim dsKeys As New DataSet("dsKeys")
    Dim dtKeys As New DataTable("dtKeys")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtKeys = dsKeys.Tables.Add("dtKeys")
        dtKeys.Columns.Add("CD_Keys", GetType(String))
        dtKeys.Columns.Add("Pswrd", GetType(String))

        DataGridView1.DataSource = dtKeys.DefaultView
        ComboBox1.DataSource = dtKeys.DefaultView
        ComboBox1.DisplayMember = "CD_Keys"
        
        TextBox1.DataBindings.Add("Text", dtKeys.DefaultView, "Pswrd")
        TextBox1.PasswordChar = "*"c
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dsKeys.WriteXml("C:\Users\Joe\Desktop\Test Folder\Key_Pswrd.xml")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        dsKeys.ReadXml("C:\Users\Joe\Desktop\Test Folder\Key_Pswrd.xml")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If TextBox2.Text <> "" AndAlso TextBox3.Text <> "" Then
            dtKeys.Rows.Add(TextBox2.Text, TextBox3.Text)
        End If
    End Sub
jwavila  Thursday, October 15, 2009 6:37 AM
Hi,

There are loads of ways you could go about doing this. I think a dataset and datatable is a little heavy handed but it sure does make saving much easier, don't think a hidden listbox is a good idea (I've done things like this and eventually and mysteriously they somehow get out of sync) although the code to save to file will do. So anyway here is my attempt...... uses a dictionary where key is the application and value is the serial, the Write and Read code has already been given and besides you can read and write how you want in there.


Imports System.IO

 

Public Class Form1

 

    Dim SerialsFile As String = "serials.xml"

    Dim SerialNumbers As New Dictionary(Of String, String)

 

    Public Sub New()

        InitializeComponent()

 

        If File.Exists(SerialsFile) Then

            Me.ReadSerials(SerialsFile)

        End If

        Me.PopulateSelectableApplications()

    End Sub

 

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        Me.WriteSerials(SerialsFile)

    End Sub

 

    Private Sub cmbApps_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbApps.SelectedIndexChanged

        Dim foundSerial As String = Me.LookupSerial(Me.cmbApps.Text)

        Me.lblSerial.Text = foundSerial

    End Sub

 

   

 

    Private Sub PopulateSelectableApplications()

        Me.cmbApps.Items.AddRange(SerialNumbers.Keys.ToArray)

    End Sub

 

    Private Function LookupSerial(ByVal app As String) As String

        If SerialNumbers.ContainsKey(app) Then

            Return SerialNumbers(app)

        Else

            Return "Application serial number is unknown."

        End If

    End Function

 

    Private Sub ReadSerials()

        ' code already given

    End Sub

 

    Private Sub WriteSerials()

        ' code already given

    End Sub

 

End Class


Like I said there are loads of ways and there is no right or wrong answer just opinions.

Derek Smyth  Thursday, October 15, 2009 7:25 AM
Thanks guyz this helped me alot,,but now i have another problem grrrrr..i made two forms form1 and form2 ok form 1 was the login form and form 2 was were all codes were stored in, but now wen i click debug only form2 opens and form1 does not..whats the probelm please?..and btw i used trujade code/method thanks m8
Asim09  Thursday, October 15, 2009 2:53 PM
post your code with all the login information, your username, password, and all the applications including their serial numbers .. ;o)
..
just a joke .. only post needed code.. this way it can be looked at..
quick suggestion, try your code in a new project, if not too much work, see if you get the same results.  sometimes vb.net express does act funny, and by starting a new project, the problem goes away..

another one, you do have form1 as the startup form in the projects properties/application tab, correct?
----
about using my suggestion, i started my own thread from it.. hope you find it of use to update/minimize code in your app.. here is the thread.
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c59f21bc-72db-4b81-8e11-a1b296564f67

btw, you are welcome.
i live here and this is my reason.. trujade.
trujade  Thursday, October 15, 2009 3:32 PM
haha nice joke,,but im not that stupid:P..lol here is the code for the login:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "******" And TextBox2.Text = "*******************" Then
            MsgBox("Access Granted", MsgBoxStyle.Information)
            Me.Hide()
            Form2.Show()
        Else
            MsgBox("Access Denied, bad username or password", MsgBoxStyle.Critical)




        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Timer1.Interval = 1

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label3.Text = (TimeOfDay)
    End Sub
End Class



ok here is the code now for form2:


Public Class Form2

    Dim names As String = ("C:\Users\**********\Desktop\appnames.txt")
    Dim serials As String = ("C:\Users\****************\Desktop\appserials.txt")
    '---  create invisible listbox
    Dim invisiblelistbox As New ListBox

    Public Sub saveinfo()
        Try
            Dim w As IO.StreamWriter
            w = New IO.StreamWriter(names)
            For Each itm In ComboBox1.Items
                w.WriteLine(itm)
            Next
            w.Close()
            Dim w2 As IO.StreamWriter
            w2 = New IO.StreamWriter(serials)
            For Each itm In invisiblelistbox.Items
                w2.WriteLine(itm)
            Next
            w2.Close()
        Catch ex As Exception
        End Try
    End Sub

    Public Sub loadinfo()
        Try
            Dim s As New IO.StreamReader(names)
            Dim line As String
            line = s.ReadLine()
            Do While Not (line Is Nothing)
                line = line.Trim()
                If line.Length > 0 Then ComboBox1.Items.Add(line)
                line = s.ReadLine()
            Loop
            s.Close()
            Dim s2 As New IO.StreamReader(serials)
            Dim line2 As String
            line2 = s2.ReadLine()
            Do While Not (line2 Is Nothing)
                line2 = line2.Trim()
                If line2.Length > 0 Then invisiblelistbox.Items.Add(line2)
                line2 = s2.ReadLine()
            Loop
            s2.Close()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        saveinfo()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadinfo()
        Button1.Text = "save"
        TextBox2.Text = "app name"
        TextBox3.Text = "serial code"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ComboBox1.Items.Add(TextBox2.Text)
        invisiblelistbox.Items.Add(TextBox3.Text)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        invisiblelistbox.SelectedIndex = ComboBox1.SelectedIndex
        TextBox1.Text = invisiblelistbox.SelectedItem
    End Sub


so do you know the prob?, oh btw say in that program im making the serial/password storage (form2) how can i add a button which will change directory to where the password/apps names will get saved.

Thanks
Asim
Asim09  Thursday, October 15, 2009 4:45 PM
hmm.. about your form2 showing before form1, i did not find anything, other than adding Form1_FormClosing and _Load in form2.  code still worked.. odd.
--
anyways, as i stated in a previous reply containing a link, i managed to to clean up the code for your project, even add some extra goodies.. hope it helps..

try the following w/a new project.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "username" And TextBox2.Text = "password" Then
            MsgBox("Access Granted", MsgBoxStyle.Information)
            Form2.Show()
            Form2.Location = Me.Location
            Me.Hide()
        Else
            MsgBox("Access Denied, bad username or password", MsgBoxStyle.Critical)
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Timer1.Interval = 1
        TextBox1.Text = "username"
        TextBox2.Text = "password"
        TextBox2.PasswordChar = "*"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label3.Text = (TimeOfDay)
    End Sub

End Class

-------------------------------

Public Class Form2

    Dim userdir As String = (Environment.GetFolderPath(Environment.SpecialFolder.Desktop))
    Dim names As String = (userdir & "\appnames.txt")
    Dim serials As String = (userdir & "\appserials.txt")
    '---  create invisible listbox
    Dim invisiblelistbox As New ListBox

    Public Sub WriteItems(ByVal filename As String, ByVal items As IEnumerable)
        Try
            Dim w As New IO.StreamWriter(filename)
            For Each itm In items
                w.WriteLine(itm)
            Next
            w.Close()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information)
        End Try
    End Sub

    Public Function ReadItems(ByVal filename As String) As String()
        Return IO.File.ReadAllLines(filename)
    End Function

    Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        '---- save items to files
        WriteItems(names, ComboBox1.Items)
        WriteItems(serials, invisiblelistbox.Items)
        '----- exit app'
        Application.Exit()
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "save"
        TextBox2.Text = "app name"
        TextBox3.Text = "serial code"
        Try
            ComboBox1.Items.AddRange(Me.ReadItems(names))
            invisiblelistbox.Items.AddRange(Me.ReadItems(serials))
        Catch ex As Exception
            MsgBox("if this is first install, then press ok to proceed." _
            & vbCrLf & "otherwise, your files have been deleted/relocated.", MsgBoxStyle.Information)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ComboBox1.Items.Add(TextBox2.Text)
        invisiblelistbox.Items.Add(TextBox3.Text)
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        invisiblelistbox.SelectedIndex = ComboBox1.SelectedIndex
        TextBox1.Text = invisiblelistbox.SelectedItem
    End Sub

End Class
--------------

now, about selecting your folder to save your password/apps, you will definitely need to use my.settings for such..
then, at the top of form2, the userdir would equal your settings, not the desktop directory.
and simply add code for a folderbrowser dialog in the try..catch of form2_load, to allow user to select a folder location if first install, files are missing/deleted/etc..

do be aware, some folders will not allow users to write files to, so your best bet is to use this..

   Dim userdir As String = (Application.StartupPath)

and learn about using my.settings for the entire load/save of items.
....
if you need further help pertaining to this thread, do ask, otherwise, start a new thread for questions, and mark your helpfuls and answers on this thread.

i live here and this is my reason.. trujade.
  • Marked As Answer byAsim09 Thursday, October 15, 2009 9:42 PM
  •  
trujade  Thursday, October 15, 2009 7:00 PM
you been a great help to me thanks.
Asim09  Thursday, October 15, 2009 9:42 PM

You can use google to search for other answers

Custom Search

More Threads

• LINQ Learning
• Saving...
• how to email the customer when the billingdate = now() ????
• Using Google images to learn VB!!
• error in vb
• How to detect which column was clicked in a Listview
• Having 2 forms with 2 different threads (and at the same time interacting with eachother)
• Changing Border Style on Mouseover
• Refer to object by name.
• How to connect to database so that the data is available to all forms and controls that are called