EDN Admin
Well-known member
Hello
I have created a save dialog box, how do i get it to give a default file type and location? i am pasting the code here
Let me know what to do
Thank you
ArjunImports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Imports System.IO.StreamWriter
Imports System.IO
Public Class frmMain
Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String)
Private stopwatch As New Stopwatch
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() Get all com ports available
cmbBaud.Items.Add(9600) Populate the cmbBaud Combo box to common baud rates used
cmbBaud.Items.Add(2400)
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) Set cmbBaud text to the first Baud rate on the list
btnDisconnect.Enabled = False Initially Disconnect Button is Disabled
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text
SerialPort1.BaudRate = cmbBaud.Text
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Open()
btnConnect.Enabled = False Disable Connect button
btnDisconnect.Enabled = True and Enable Disconnect button
Me.stopwatch.Reset()
Label3.Text = "00:00:00"
Timer1.Start() starts the timer
Me.stopwatch.Start() start watch
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close()
btnConnect.Enabled = True
btnDisconnect.Enabled = False
Timer1.Stop()
Me.stopwatch.Stop()
Me.stopwatch.Reset()
Label3.Text = "00:00:00:00"
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
End Sub
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text pop a message box to user if he is changing ports
Else without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text pop a message box to user if he is changing baud rate
Else without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
PrintDialog1.Document = PrintDocument1
PrintDialog1.PrinterSettings is ok
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim font1 As New Font ("Arial", 16, Fontstyle.Regular)
e.Graphics.DrawString(rtbReceived.Text, rtbReceived.Font, Brushes.Black, 100, 100)
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
PintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub rtbReceived_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbReceived.TextChanged
Me.rtbReceived.Select(Me.rtbReceived.TextLength, 0)
Me.rtbReceived.ScrollToCaret()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
Label3.Text = String.Format("{0:00}:{1:00}:{2:00}", _
Math.Floor(elapsed.TotalHours), _
elapsed.Minutes, elapsed.Seconds)
End Sub
Private Sub txtsave_Click(sender As System.Object, e As System.EventArgs) Handles txtsave.Click
SaveFileDialog1.ShowDialog()
the application will check if the file is already exists, if exists, it will ask the user if they want to replace it
If My.Computer.FileSystem.FileExists(SaveFileDialog1.FileName) Then
Dim ask As MsgBoxResult
ask = MsgBox("File already exists, would you like to replace it?", MsgBoxStyle.YesNo, "File Exists")
if the user decides not to replace the existing file
If ask = MsgBoxResult.No Then
SaveFileDialog1.ShowDialog()
if the user decides to replace the existing file
ElseIf ask = MsgBoxResult.Yes Then
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, rtbReceived.Text, False)
End If
if the file doesnt exist
Else
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, rtbReceived.Text, False)
End If
End Sub
End Class
View the full article
I have created a save dialog box, how do i get it to give a default file type and location? i am pasting the code here
Let me know what to do
Thank you
ArjunImports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Imports System.IO.StreamWriter
Imports System.IO
Public Class frmMain
Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String)
Private stopwatch As New Stopwatch
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
myPort = IO.Ports.SerialPort.GetPortNames() Get all com ports available
cmbBaud.Items.Add(9600) Populate the cmbBaud Combo box to common baud rates used
cmbBaud.Items.Add(2400)
cmbBaud.Items.Add(19200)
cmbBaud.Items.Add(38400)
cmbBaud.Items.Add(57600)
cmbBaud.Items.Add(115200)
For i = 0 To UBound(myPort)
cmbPort.Items.Add(myPort(i))
Next
cmbPort.Text = cmbPort.Items.Item(0) Set cmbPort text to the first COM port detected
cmbBaud.Text = cmbBaud.Items.Item(0) Set cmbBaud text to the first Baud rate on the list
btnDisconnect.Enabled = False Initially Disconnect Button is Disabled
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
SerialPort1.PortName = cmbPort.Text
SerialPort1.BaudRate = cmbBaud.Text
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DataBits = 8
SerialPort1.Open()
btnConnect.Enabled = False Disable Connect button
btnDisconnect.Enabled = True and Enable Disconnect button
Me.stopwatch.Reset()
Label3.Text = "00:00:00"
Timer1.Start() starts the timer
Me.stopwatch.Start() start watch
End Sub
Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
SerialPort1.Close()
btnConnect.Enabled = True
btnDisconnect.Enabled = False
Timer1.Stop()
Me.stopwatch.Stop()
Me.stopwatch.Reset()
Label3.Text = "00:00:00:00"
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting()) Automatically called every time a data is received at the serialPort
End Sub
Private Sub ReceivedText(ByVal [text] As String)
compares the ID of the creating Thread to the ID of the calling Thread
If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text]
End If
End Sub
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.PortName = cmbPort.Text pop a message box to user if he is changing ports
Else without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
If SerialPort1.IsOpen = False Then
SerialPort1.BaudRate = cmbBaud.Text pop a message box to user if he is changing baud rate
Else without disconnecting first.
MsgBox("Valid only if port is Closed", vbCritical)
End If
End Sub
Private Sub PrintToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintToolStripMenuItem.Click
PrintDialog1.Document = PrintDocument1
PrintDialog1.PrinterSettings is ok
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim font1 As New Font ("Arial", 16, Fontstyle.Regular)
e.Graphics.DrawString(rtbReceived.Text, rtbReceived.Font, Brushes.Black, 100, 100)
End Sub
Private Sub PrintPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintPreviewToolStripMenuItem.Click
PintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub rtbReceived_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtbReceived.TextChanged
Me.rtbReceived.Select(Me.rtbReceived.TextLength, 0)
Me.rtbReceived.ScrollToCaret()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
Label3.Text = String.Format("{0:00}:{1:00}:{2:00}", _
Math.Floor(elapsed.TotalHours), _
elapsed.Minutes, elapsed.Seconds)
End Sub
Private Sub txtsave_Click(sender As System.Object, e As System.EventArgs) Handles txtsave.Click
SaveFileDialog1.ShowDialog()
the application will check if the file is already exists, if exists, it will ask the user if they want to replace it
If My.Computer.FileSystem.FileExists(SaveFileDialog1.FileName) Then
Dim ask As MsgBoxResult
ask = MsgBox("File already exists, would you like to replace it?", MsgBoxStyle.YesNo, "File Exists")
if the user decides not to replace the existing file
If ask = MsgBoxResult.No Then
SaveFileDialog1.ShowDialog()
if the user decides to replace the existing file
ElseIf ask = MsgBoxResult.Yes Then
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, rtbReceived.Text, False)
End If
if the file doesnt exist
Else
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, rtbReceived.Text, False)
End If
End Sub
End Class
View the full article