in a form i have a listbox which has many items and one button,when we execute the project and while execution when i click a items from the listbox it will display the clicked item on the button but the problem i have is how to display the same clicked item when the form is executed for the second time the same clicked item in the previous execution should be on the button without clicking on the listbox items

codeorder commented: well.defined question:) +12

Recommended Answers

All 14 Replies

You can do this with application settings. I don't know what version of Visual Studio you have so I'll describe the procedure for VS 2010.

Start with your project open

Go to the project properties settings

Select the "Settings" tab down the left side of the panel

Add an item, let's name it "LastButtonText", make it of type "String" and scope "User". Don't enter a value.

Go to your Form Load event handler and add the line (I'll assume your button control is named btnMyButton - change it to the correct name)

If My.Settings.LastButtonText <> "" Then
    btnMyButton.Text = My.Settings.LastButtonText
End If

That will load the button text on startup. Now you have to save the last displayed text at app exit. For that you have to add the following code to the FormClosing handler.

My.Settings.LastButtonText = btnMyButton.Text

That should do it.

commented: I agree +8

You can do this with application settings. I don't know what version of Visual Studio you have so I'll describe the procedure for VS 2010.

Start with your project open

Go to the project properties settings

Select the "Settings" tab down the left side of the panel

Add an item, let's name it "LastButtonText", make it of type "String" and scope "User". Don't enter a value.

Go to your Form Load event handler and add the line (I'll assume your button control is named btnMyButton - change it to the correct name)

If My.Settings.LastButtonText <> "" Then
    btnMyButton.Text = My.Settings.LastButtonText
End If

That will load the button text on startup. Now you have to save the last displayed text at app exit. For that you have to add the following code to the FormClosing handler.

My.Settings.LastButtonText = btnMyButton.Text

That should do it.

hey friend it's not working is their any other way to do this please

If it doesn't work then you must be doing something wrong because I use this feature frequently.

Something simple, use a .txt File. This saves the File to Desktop.

Imports System.IO
Public Class Form1
    Private myCoolFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myCoolBtnTextFile.txt"

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        File.WriteAllText(myCoolFile, Button1.Text)
    End Sub
 
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If File.Exists(myCoolFile) Then Button1.Text = File.ReadAllText(myCoolFile)
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        With ListBox1
            If Not .SelectedIndex = -1 Then Button1.Text = .SelectedItem Else Button1.Text = ""
        End With
    End Sub
End Class
commented: he his the best +1

Something simple, use a .txt File. This saves the File to Desktop.

Imports System.IO
Public Class Form1
    Private myCoolFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myCoolBtnTextFile.txt"

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        File.WriteAllText(myCoolFile, Button1.Text)
    End Sub
 
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If File.Exists(myCoolFile) Then Button1.Text = File.ReadAllText(myCoolFile)
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        With ListBox1
            If Not .SelectedIndex = -1 Then Button1.Text = .SelectedItem Else Button1.Text = ""
        End With
    End Sub
End Class

just tell me how to save in this button please refer the below code

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            With New OpenFileDialog
             .Title = "Select a Cool File to load..."
            ' .Filter = ""
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Button2.Text = IO.Path.GetFileName(.FileName) '// only FileName and .ext.
                Button2.Tag = .FileName '// FullPath of .File, to load .File from.
            End If
        End With

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        With Button2
            If Not .Tag Is Nothing Then Process.Start(.Tag.ToString) '// check if it has a .FullPath and load in/as default.
        End With

    End Sub
End Class

>>just tell me how to save in this button please refer the below code
Can you explain a little more in.depth?; as to If you would like a SaveFileDialog to save the File or save the Button it's self to a File?

Did you add the setting the My Project > Settings tab?

>>just tell me how to save in this button please refer the below code
Can you explain a little more in.depth?; as to If you would like a SaveFileDialog to save the File or save the Button it's self to a File?

see the code above is
in my form their are two buttons, when we execute we can select any .exe file from the file open menu

and

that name of the file will be written on the button2, as we click on the button2 it will open the selected .exe file
the problem i have is, when we execute for the second time i want the same file name to be written and by clicking on the button2 it should open the .exe file without doing the work which we did in the previous execution

please help i am getting confused

Thanks for the necessary details to get "another" +=1 to my Solved.Threads list.:D

Imports System.IO
Public Class Form1
    Private myCoolFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myCoolBtnTextFile.txt"

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        With Button2
            If Not .Tag Is Nothing Then File.WriteAllText(myCoolFile, .Text & vbNewLine & .Tag) '// save.File.
        End With
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If File.Exists(myCoolFile) Then
            Dim arCoolT() As String = File.ReadAllLines(myCoolFile) '// read File into Array.
            With Button2
                .Text = arCoolT(0) '// line.1
                .Tag = arCoolT(1) '// line.2
            End With
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        With New OpenFileDialog
            .Title = "Select a Cool File to load..."
            ' .Filter = ""
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Button2.Text = IO.Path.GetFileName(.FileName) '// only FileName and .ext.
                Button2.Tag = .FileName '// FullPath of .File, to load .File from.
            End If
        End With
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        With Button2
            If Not .Tag Is Nothing Then Process.Start(.Tag)
        End With
    End Sub
End Class

Thanks for the necessary details to get "another" +=1 to my Solved.Threads list.:D

Imports System.IO
Public Class Form1
    Private myCoolFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myCoolBtnTextFile.txt"

    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        With Button2
            If Not .Tag Is Nothing Then File.WriteAllText(myCoolFile, .Text & vbNewLine & .Tag) '// save.File.
        End With
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If File.Exists(myCoolFile) Then
            Dim arCoolT() As String = File.ReadAllLines(myCoolFile) '// read File into Array.
            With Button2
                .Text = arCoolT(0) '// line.1
                .Tag = arCoolT(1) '// line.2
            End With
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        With New OpenFileDialog
            .Title = "Select a Cool File to load..."
            ' .Filter = ""
            If .ShowDialog = Windows.Forms.DialogResult.OK Then
                Button2.Text = IO.Path.GetFileName(.FileName) '// only FileName and .ext.
                Button2.Tag = .FileName '// FullPath of .File, to load .File from.
            End If
        End With
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        With Button2
            If Not .Tag Is Nothing Then Process.Start(.Tag)
        End With
    End Sub
End Class

the code is for two buttons, but how to do for the six buttons

i will explain you in details

in the form their are three buttons on the rightside of the form, correct opposite to those three button on the leftside their are another three buttons, but the problem is i am not able code for the rest of the two buttons as you showed

Being a newbie for vb.net, I seek your advice in a windows application I am developing.

See if this helps to load a file by clicking a Button.

Imports System.IO
Public Class Form1
#Region "-----===-----===-----=== DECLARATIONS ===-----===-----===-----"
    Private myCoolFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myCoolBtnTextFile.txt" '// your.File to .Save/.Load to/from.
    Private chrMain As Char = "|" '// char used to .Split each line into 2, for .Name and for .Tag of each btn.
#End Region '===-----===-----===-----'


    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        LoadOrSaveBtnsOptions(True) '// Save.File.
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadOrSaveBtnsOptions() '// Load.File If available.
    End Sub

#Region "-----===-----===-----=== BTNS OPTIONS ===-----===-----===-----"

    Private Sub LoadOrSaveBtnsOptions(Optional ByVal isSaving As Boolean = False)
        '/////////////////////////////////////////////////    YOUR BUTTONS    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
        Dim btnArray() As Button = {Button1, Button2, Button3, Button4, Button5, Button6}
        '/////////////////////////////////////////////////                              \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
        Dim sContent As String = "" : Dim arFileLines() As String = Nothing '// read.File if it.Exists.
        If Not isSaving Then If File.Exists(myCoolFile) Then arFileLines = File.ReadAllLines(myCoolFile)
        For i As Integer = 0 To btnArray.Length - 1 '// loop thru each btn in btn.Array.
            With btnArray(i)
                If Not isSaving Then '// check if it should Save of Not.
                    '// add Event.Handlers to each btn.
                    AddHandler .MouseDown, AddressOf _btnsOptions_MouseDown '// allows right click of .
                    AddHandler .Click, AddressOf _btnsOptions_Click '// executes tag.process of .
                    .Cursor = Cursors.Hand '// cute.Hand cursor. xD
                    If Not btnArray.Length = 0 Then '// check If File has been loaded.
                        '// set .Text and .Tag by .Splitting the line into Arrays.
                        .Text = arFileLines(i).Split(chrMain).GetValue(1) : .Tag = arFileLines(i).Split(chrMain).GetValue(2)
                    End If
                    Continue For '// skip remaining code in current.loop.
                ElseIf isSaving Then '// .Save btn.Names and btn.Tags to File.
                    If Not sContent = "" Then '// Write Content for File.
                        sContent &= vbNewLine & chrMain & .Text
                        If Not .Tag Is Nothing Then sContent &= chrMain & .Tag
                    Else
                        sContent &= chrMain & .Text
                        If Not .Tag Is Nothing Then sContent &= chrMain & .Tag
                    End If
                End If
            End With
        Next
        If isSaving Then File.WriteAllText(myCoolFile, sContent) '// save.File.
    End Sub

    Private Sub _btnsOptions_Click(sender As System.Object, e As System.EventArgs) '//------- Execute.Process.
        With CType(sender, Button)
            If Not .Tag Is Nothing Then '// check If .Tag.Contains(a value as fullPath of File)
                Try
                    If Not .Tag Is Nothing Then Process.Start(.Tag) '// execute.process
                Catch ex As Exception
                    MsgBox("error executing.process: " & .Text, MsgBoxStyle.Critical)
                End Try
            End If
        End With
    End Sub

    Private Sub _btnsOptions_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) '//------- Set.Process File.Path.
        If e.Button = Windows.Forms.MouseButtons.Right Then '// Right.Click btn to set .Tag with a File.Path Value.
            Dim btn As Button = CType(sender, Button)
            With New OpenFileDialog
                .Title = "Select a Cool File to add as an executable.process..."
                .RestoreDirectory = True
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    btn.Text = IO.Path.GetFileName(.FileName) '// only FileName and .ext.
                    btn.Tag = .FileName '// FullPath of .File, to load .File from.
                End If
            End With
        End If
    End Sub
#End Region '===-----===-----===-----'
End Class

.line.21: set the proper 6 Buttons to the Buttons on your Form and call it a day.:)

One more thing, Right.Click each Button to set a File.Path and File.Name to.

What version of Visual Studio are you using? I have Visual Studio 2010. I created a project that has a form with one button. The first time you run the program, the button will have no label. Click it and the label will be set to the current date and time. When you close the program, then rerun it, the label will be what it was when you last closed the app.

I believe this is what you said you wanted. Add extra settings for extra buttons and add extra code based on what I gave you.

The zip of the project is attached.

See if this helps to load a file by clicking a Button.

Imports System.IO
Public Class Form1
#Region "-----===-----===-----=== DECLARATIONS ===-----===-----===-----"
    Private myCoolFile As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\myCoolBtnTextFile.txt" '// your.File to .Save/.Load to/from.
    Private chrMain As Char = "|" '// char used to .Split each line into 2, for .Name and for .Tag of each btn.
#End Region '===-----===-----===-----'


    Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        LoadOrSaveBtnsOptions(True) '// Save.File.
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadOrSaveBtnsOptions() '// Load.File If available.
    End Sub

#Region "-----===-----===-----=== BTNS OPTIONS ===-----===-----===-----"

    Private Sub LoadOrSaveBtnsOptions(Optional ByVal isSaving As Boolean = False)
        '/////////////////////////////////////////////////    YOUR BUTTONS    \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
        Dim btnArray() As Button = {Button1, Button2, Button3, Button4, Button5, Button6}
        '/////////////////////////////////////////////////                              \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
        Dim sContent As String = "" : Dim arFileLines() As String = Nothing '// read.File if it.Exists.
        If Not isSaving Then If File.Exists(myCoolFile) Then arFileLines = File.ReadAllLines(myCoolFile)
        For i As Integer = 0 To btnArray.Length - 1 '// loop thru each btn in btn.Array.
            With btnArray(i)
                If Not isSaving Then '// check if it should Save of Not.
                    '// add Event.Handlers to each btn.
                    AddHandler .MouseDown, AddressOf _btnsOptions_MouseDown '// allows right click of .
                    AddHandler .Click, AddressOf _btnsOptions_Click '// executes tag.process of .
                    .Cursor = Cursors.Hand '// cute.Hand cursor. xD
                    If Not btnArray.Length = 0 Then '// check If File has been loaded.
                        '// set .Text and .Tag by .Splitting the line into Arrays.
                        .Text = arFileLines(i).Split(chrMain).GetValue(1) : .Tag = arFileLines(i).Split(chrMain).GetValue(2)
                    End If
                    Continue For '// skip remaining code in current.loop.
                ElseIf isSaving Then '// .Save btn.Names and btn.Tags to File.
                    If Not sContent = "" Then '// Write Content for File.
                        sContent &= vbNewLine & chrMain & .Text
                        If Not .Tag Is Nothing Then sContent &= chrMain & .Tag
                    Else
                        sContent &= chrMain & .Text
                        If Not .Tag Is Nothing Then sContent &= chrMain & .Tag
                    End If
                End If
            End With
        Next
        If isSaving Then File.WriteAllText(myCoolFile, sContent) '// save.File.
    End Sub

    Private Sub _btnsOptions_Click(sender As System.Object, e As System.EventArgs) '//------- Execute.Process.
        With CType(sender, Button)
            If Not .Tag Is Nothing Then '// check If .Tag.Contains(a value as fullPath of File)
                Try
                    If Not .Tag Is Nothing Then Process.Start(.Tag) '// execute.process
                Catch ex As Exception
                    MsgBox("error executing.process: " & .Text, MsgBoxStyle.Critical)
                End Try
            End If
        End With
    End Sub

    Private Sub _btnsOptions_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) '//------- Set.Process File.Path.
        If e.Button = Windows.Forms.MouseButtons.Right Then '// Right.Click btn to set .Tag with a File.Path Value.
            Dim btn As Button = CType(sender, Button)
            With New OpenFileDialog
                .Title = "Select a Cool File to add as an executable.process..."
                .RestoreDirectory = True
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    btn.Text = IO.Path.GetFileName(.FileName) '// only FileName and .ext.
                    btn.Tag = .FileName '// FullPath of .File, to load .File from.
                End If
            End With
        End If
    End Sub
#End Region '===-----===-----===-----'
End Class

.line.21: set the proper 6 Buttons to the Buttons on your Form and call it a day.:)

One more thing, Right.Click each Button to set a File.Path and File.Name to.

hello

in the above code 34th line i am geting a error like "Index was outside the bounds of the array."

>>in the above code 34th line i am geting a error like "Index was outside the bounds of the array."
Have you tried the previously posted.code in a New Project w/only 6.Buttons? If Not, give that a Try, should Not error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.