change text in button

Snookie

Active member
Joined
Dec 13, 2010
Messages
29
Programming Experience
Beginner
I have 2 forms
main form
Form2

Main Form is my startup form and then 2 buttons on the form
button 1 open form2 and main form hidden. button 2 closes and in the form 2 I have a textbox where I write some text
and a button to save the text in a txt. File
Once I've saved my text in txt. file so I click on a button that closes form2 and open main form
My problem is that the button on the main form I should like to have button text is from the txt. file.
And I do not know how I reload the button or form.

if I close the program and start it again, then my button text has change to what i write in txt file..
Someone who can help me ...?
 
While loading the main form, you need to copy the text from the file where you stored to the 'Text' property of the button .

Could you please elaborate the sentence 'if I close the program and start it again, then my button text has change to what i write in txt file..'
 
Snookie, not only could you provide a clear explanation of what you're wanting to do, but could you perhaps provide a screen shot or two?
 
What i want to do is.
Save the text from the textbox into a txt file. - Its ok and works.

Then i have save the text into the txt file.
So i am finnish on the Form2.
now i click on Button1 to close the Form2 and Show the MainForm

The you make a Button There the text on it " button1 " and its that text i want to change
and this text sould be the text from the txt file.
But its doesnt work, only if i restart the program.

And if have in my formload there check if the test.txt

If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Button1.Text = objReader.ReadToEnd
objReader.Close()
End If

But how can i reload or refresh button or form. so its run the formload again..?
 
I just forget.
The first line in my code :
Dim FILE_NAME As String = "C:\Users\" & fName & "\Documents\text.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Button1.Text = objReader.ReadToEnd
objReader.Close()
End If

I hobe its better to see what i want to do.

I have a code to refresh my form...
But i dont want to click on button to update the form..
Yes if its could be in the button i click on Form2 then i exit the form2 and show MainForm...

The code i found to update then i click on button is :
Try
Call MainForm_Load(sender, e)
Catch ex As Exception
MsgBox(ex.Message)
End Try
 
It seems that you are hiding the main form while showing the second form, or making the second form as the child of the first form. If this is the case, then you can do the followings:
==> In the event where you calling the main form, set the text of MainForm's button.
Ex:
VB.NET:
If System.IO.File.Exists(FILE_NAME) = True Then
     Dim objReader As New System.IO.StreamReader(FILE_NAME)
     MainForm.Button1.Text = objReader.ReadToEnd
     objReader.Close()
End If

MainForm.Show()
 
MainForm
Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim FILE_NAME As String = "C:\Users\" & fName & "\Documents\text.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Button1.Text = objReader.ReadToEnd
objReader.Close()
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Hide()
Form2.Show()
End Sub

Form2
Button to save the txt.file
Private Sub Teamv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Teamv.Click
FileOpen(FileNumber, "C:\Users\" & fName & "\Documents\test.txt", OpenMode.Output)
For Each Item As Object In TextBox1.Text
PrintLine(FileNumber, Text.ToString)
Next
FileClose(FileNumber)

Dim FILE_NAME As String = "C:\Users\" & fName & "\Documents\test.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write(TextBox1.Text)
objWriter.Close()
MsgBox("Save")
Else
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Me.Close()
MainForm.Show()

'My.Forms.MainForm.Update() <--- Have try this not work
'My.Forms.MainForm.Refresh() <--- Have try this not work
End Sub


This code are in my MainForm Load
How will it update then i close form2 there i have the code to save txt file.
So then i click on Button1 to close Form2 and Show MainForm
it will not load the MainForm Load so i cannot use the code in there.

Because the MainForm its only Hide its not Close.

I hobe yu can see what i want to do...
Its from Form2 i want to refresh, update, reload the MainForm then i click on button to Close Form2
 
Last edited:
The load event of a form raises when the form is loaded into the memory. When you hide the form, it does not unload from the memory. Hence, the 'load' event of the MainForm will not be raised when you call its show() method on form2.
However, you can change the properties of MainForm's controls on Form2. Hence, write the code:
VB.NET:
Dim FILE_NAME As String = "C:\Users\" & fName & "\Documents\text.txt"

If System.IO.File.Exists(FILE_NAME) = True Then
     Dim objReader As New System.IO.StreamReader(FILE_NAME)
     [B]MainForm[/B].Button1.Text = objReader.ReadToEnd
     objReader.Close()
End If

Me.Close()
MainForm.Show()
on the Form2's Button1_Click event.
 
Back
Top