[Help]savefile dialog[Solved]

Posts 115 of 18 · Page 1 of 2
[Help]savefile dialog[Solved]
hi i was wondering if anyone could help me im making a program it got 5 textboxes and 3 combo boxes and 2 radiobuttons

well i want to know how to use the savefiledialog to save all the info in those things

thanks
Why would you need a savefiledialog for that?

Use My.settings for this.
because savedialog would be easier

any way do u have the code?
You wouldn't use SaveDialog at all for saving settings. Use My.settings , simple really.
Quote Originally Posted by NextGen1 View Post
You wouldn't use SaveDialog at all for saving settings. Use My.settings , simple really.
QFT. A save file dialog specifies where you want to save a new file...how does that have any relevance to saving textbox/checkbox content.

Now you just need to go Project-> (projectname)Properties
Then go to the settings tab and add 5 string settings and 2 booleans. There's a tut somewhere in NG's tut sticky about settings saving.
Yea, I even added a how to easily modify settings in application by adding a settings area for your application. pretty straight forward.
I DONT WANT TO SAVE THE SETTING THO I WANT TO SAVE WHATS IN THE TEXTBOXES AND SAVE THE RADIOBUTTON THATS CLICKED INTO A TXT FILE
You can do that using the settings
Quote Originally Posted by Lolland View Post
You can do that using the settings
Yes but apparently Mr Bigshot here knows better than we do.
Quote Originally Posted by bayley60 View Post
I DONT WANT TO SAVE THE SETTING THO I WANT TO SAVE WHATS IN THE TEXTBOXES AND SAVE THE RADIOBUTTON THATS CLICKED INTO A TXT FILE
Here you go:

Add the following sub-procedure to your program:

[php]Sub SaveInfo()
Dim file As String = My.Computer.FileSystem.SpecialDirectories.MyDocume nts & "\Settings.txt"
Dim strB As New StringBuilder
For Each n As Object In Me.Controls
If n.GetType.Name = "CheckBox" Then
strB.AppendLine(n.Name & ":" & n.checked)
ElseIf n.GetType.Name = "RadioButton" Then
strB.AppendLine(n.Name & ":" & n.checked)
ElseIf n.GetType.Name = "TextBox" Then
strB.AppendLine(n.Name & ":" & n.text)
End If
Next
My.Computer.FileSystem.WriteAllText(file, strB.ToString, False)
End Sub[/php]

Then call it from where ever you want. As an example, call it from the Form's Closing event:

Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SaveInfo()
    End Sub
Then, to retrieve the saved settings add the following sub:

[php]Sub RetrieveInfo()
Dim file As String = My.Computer.FileSystem.SpecialDirectories.MyDocume nts & "\Settings.txt"
Dim x As New IO.StreamReader(file)
While Not x.EndOfStream
Dim curLine As String = x.ReadLine.Trim
If Not curLine = vbNullString Then
If curLine.Contains(":") Then
Dim sp() As String = curLine.Split(":")
If Me.Controls.ContainsKey(sp(0)) Then
Dim C_Obj As Object = Me.Controls(sp(0))
If C_Obj.GetType.Name = "CheckBox" Or C_Obj.GetType.Name = "RadioButton" Then
If sp(1) = "False" Then
C_Obj.checked = False
Else
C_Obj.checked = True
End If
ElseIf C_Obj.GetType.Name = "TextBox" Then
C_Obj.text = sp(1)
End If
End If
End If
End If
End While
x.Dispose()
End Sub[/php]

Again, you can call it from where ever you want. As an example, call it on the Form's Load event:

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RetrieveInfo()
    End Sub
And you're done xD.
Quote Originally Posted by Hassan View Post


Here you go:

Add the following sub-procedure to your program:

[php]Sub SaveInfo()
Dim file As String = My.Computer.FileSystem.SpecialDirectories.MyDocume nts & "\Settings.txt"
Dim strB As New StringBuilder
For Each n As Object In Me.Controls
If n.GetType.Name = "CheckBox" Then
strB.AppendLine(n.Name & ":" & n.checked)
ElseIf n.GetType.Name = "RadioButton" Then
strB.AppendLine(n.Name & ":" & n.checked)
ElseIf n.GetType.Name = "TextBox" Then
strB.AppendLine(n.Name & ":" & n.text)
End If
Next
My.Computer.FileSystem.WriteAllText(file, strB.ToString, False)
End Sub[/php]

Then call it from where ever you want. As an example, call it from the Form's Closing event:

Code:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SaveInfo()
    End Sub
Then, to retrieve the saved settings add the following sub:

[php]Sub RetrieveInfo()
Dim file As String = My.Computer.FileSystem.SpecialDirectories.MyDocume nts & "\Settings.txt"
Dim x As New IO.StreamReader(file)
While Not x.EndOfStream
Dim curLine As String = x.ReadLine.Trim
If Not curLine = vbNullString Then
If curLine.Contains(":") Then
Dim sp() As String = curLine.Split(":")
If Me.Controls.ContainsKey(sp(0)) Then
Dim C_Obj As Object = Me.Controls(sp(0))
If C_Obj.GetType.Name = "CheckBox" Or C_Obj.GetType.Name = "RadioButton" Then
If sp(1) = "False" Then
C_Obj.checked = False
Else
C_Obj.checked = True
End If
ElseIf C_Obj.GetType.Name = "TextBox" Then
C_Obj.text = sp(1)
End If
End If
End If
End If
End While
x.Dispose()
End Sub[/php]

Again, you can call it from where ever you want. As an example, call it on the Form's Load event:

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RetrieveInfo()
    End Sub
And you're done xD.

yes bt i dont want to save the setting i want to save the info in a txt file using a savefiledialog and all i need plz is the code to get the savefiledailog to do that plz
Quote Originally Posted by bayley60 View Post
yes bt i dont want to save the setting i want to save the info in a txt file using a savefiledialog and all i need plz is the code to get the savefiledailog to do that plz
how about trying to google some snippets and try that yourself?
Quote Originally Posted by bayley60 View Post
yes bt i dont want to save the setting i want to save the info in a txt file using a savefiledialog and all i need plz is the code to get the savefiledailog to do that plz
Try growing a fucking brain, seriously everything doesn't need to spoonfed to you. But just so we can finally make this topic effing solved

Still credits to Xscapism, I just editted it a little:

[php]
Sub SaveInfo(ByVal WhereTo As String)
Dim file As String = WhereToSave
Dim strB As New StringBuilder
For Each n As Object In Me.Controls
If n.GetType.Name = "CheckBox" Then
strB.AppendLine(n.Name & ":" & n.checked)
ElseIf n.GetType.Name = "RadioButton" Then
strB.AppendLine(n.Name & ":" & n.checked)
ElseIf n.GetType.Name = "TextBox" Then
strB.AppendLine(n.Name & ":" & n.text)
End If
Next
My.Computer.FileSystem.WriteAllText(file, strB.ToString, False)
End Sub


Sub RetrieveInfo(ByVal WhereFrom As String)
Dim file As String = My.Computer.FileSystem.SpecialDirectories.MyDocume nts & "\Settings.txt"
Dim x As New IO.StreamReader(file)
While Not x.EndOfStream
Dim curLine As String = x.ReadLine.Trim
If Not curLine = vbNullString Then
If curLine.Contains(":") Then
Dim sp() As String = curLine.Split(":")
If Me.Controls.ContainsKey(sp(0)) Then
Dim C_Obj As Object = Me.Controls(sp(0))
If C_Obj.GetType.Name = "CheckBox" Or C_Obj.GetType.Name = "RadioButton" Then
If sp(1) = "False" Then
C_Obj.checked = False
Else
C_Obj.checked = True
End If
ElseIf C_Obj.GetType.Name = "TextBox" Then
C_Obj.text = sp(1)
End If
End If
End If
End If
End While
x.Dispose()
End Sub
[/php]

Now add a button to your form, change it's text to "Save Settings" and add the following code

[php]

Dim SaveDialog as New SaveFileDialog

If SaveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

SaveInfo(SaveDialog.FileName)

End If

[/php]


Then on another button called "Load Settings" add this

[php]

Dim OFD As New OpenFileDialog

If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then

RetrieveInfo(OFD.FileName)

End If

[/php]

But you'll need like some kind of identifier so the user can't just grab any file and attempt to load it.



EDIT

In case all you wanted it to do was save 3 textboxes ONLY

Add this to a button:
[php]

Dim SaveDialog as New SaveFileDialog

If SaveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then

Using sWrite as New IO.StreamWriter(SaveDialog.FileName)

sWrite.WriteLine(TextBox1.Text)
sWrite.WriteLine(TextBox2.Text)
sWrite.WriteLine(TextBox3.Text)

End Using

End If
[/php]

And to retrieve it again add this to another button

[php]
Dim OFD As New OpenFileDialog

If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then

Using sRead As New IO.StreamReader(OFD.FileName)

TextBox1.Text = sRead.ReadLine
TextBox2.Text = sRead.ReadLine
TextBox3.Text = sRead.ReadLine

End Using

End If
[/php]

String-Slave is obviously back.
Quote Originally Posted by Blubb1337 View Post
String-Slave is obviously back.
Get on MSN and SHD, nao.
Posts 115 of 18 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?