In: Computer Science
In case of any query do comment. Please rate answer as well. Thanks
Note: You didn’t mention the functionality of Third button and second text box. Please do let me know via comment if you require anything for Third button. Right now I am just outputting, on the click of third button push the data to Second text box
Code:
=======Form1.vb======
Imports System.IO
Public Class Form1
Dim data() As String
'handles the click of Button 2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
'StreamWrite is used to write lines to the file opened by
AppendText
Using writer As StreamWriter =
File.AppendText("C:\display.txt")
'Write the content of a text box
writer.WriteLine(TextBox1.Text)
End Using
TextBox1.Clear()
End Sub
'handles the click of Button 2
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles
Button2.Click
'assign the ReadData output to Text box 2
TextBox2.Text = ReadData()
End Sub
'Read the data from the file and join it using new line and return
the data
Private Function ReadData() As String
data = File.ReadAllLines("C:\display.txt")
ReadData = Join(data, System.Environment.NewLine)
End Function
'function to read file line by line
Private Function ReadDataLineByLine() As String
Dim reader As StreamReader =
My.Computer.FileSystem.OpenTextFileReader("C:\display.txt")
Dim line As String
Do
line = reader.ReadLine
data.Append(line)
Loop Until line Is Nothing
reader.Close()
ReadDataLineByLine = Join(data, System.Environment.NewLine)
End Function
'handles the click of Button 3
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles
Button3.Click
'assign the ReadData output to Text box 3
TextBox3.Text = ReadDataLineByLine()
End Sub
End Class
=====from1.designer.vb========
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.TextBox3 = New System.Windows.Forms.TextBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
Me.Button3 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(74, 76)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(180, 26)
Me.TextBox1.TabIndex = 0
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(289, 76)
Me.TextBox2.Multiline = True
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.ReadOnly = True
Me.TextBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox2.Size = New System.Drawing.Size(195, 103)
Me.TextBox2.TabIndex = 1
'
'TextBox3
'
Me.TextBox3.Location = New System.Drawing.Point(509, 76)
Me.TextBox3.Multiline = True
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.ReadOnly = True
Me.TextBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox3.Size = New System.Drawing.Size(195, 103)
Me.TextBox3.TabIndex = 3
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(86, 226)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(167, 36)
Me.Button1.TabIndex = 4
Me.Button1.Text = "First Button"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(317, 226)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(167, 36)
Me.Button2.TabIndex = 5
Me.Button2.Text = "Second Button"
Me.Button2.UseVisualStyleBackColor = True
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(555, 226)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(167, 36)
Me.Button3.TabIndex = 6
Me.Button3.Text = "Third Button"
Me.Button3.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(9.0!, 20.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents TextBox1 As TextBox
Friend WithEvents TextBox2 As TextBox
Friend WithEvents TextBox3 As TextBox
Friend WithEvents Button1 As Button
Friend WithEvents Button2 As Button
Friend WithEvents Button3 As Button
End Class
=========screen shot of the code=========
Output:
X Form 1.Designer.vb Form1.vb C# DisplayButtonn Imports System.IO Form 1.vb [Design] Form1 . . ReadData 1 reference Public.Class Form1 F... Dimdata().As String Hoc.'handles.the-click-of-Button-2 O references ... Private Sub-Button1_Click(sender. As. Object, e. As - EventArgs) - Handles Button1. Click ..'Streamwrite is used to write lines to the file opened.by. AppendText ...Using writer. As-StreamWriter.=. File. AppendText("C:\display.txt") ...... 'Write the content of a text box -writer.WriteLine(TextBox1.Text) ...End-Using ...TextBox1.Clear() End Sub ...'handles-the-click-of-Button-2 O references ...Private Sub-Button2_Click(sender. As. Object, .e-As.EventArgs). Handles. Button2. Click ......'assign-the-ReadData-output-to-Textbox3 ...TextBox3.Text = ReadData() ... End Sub ... 'Read the data from the file and join it.using.new-line and return the data 3 references ... Private. Function. ReadData() - As String ...... data = File. ReadAllLines ("C:\display.txt") S ct... ReadData = Join(data,System.Environment. NewLine) ... End- Function .'handles the click.of. Button-3 O references ... Private Sub-Button3_Click(sender. As. Object, e As.EventArgs) - Handles Button3. Click ... ..'assign-the-ReadData.outputto-Text-box-2 Fot...TextBox2.Text =- ReadData() + ... End Sub End.Class
Form1 - 0 x First Button Second Button Third Button
Form1 - O X First Button Second Button Third Button
Form1 - O x First Button Second Button Third Button
. Form1 - O X First Button Second Button Third Button
. Form1 - O X First Button Second Button Third Button
Form1 - O x First Button Second Button Third Button
. Form1 - 0 X First Button Second Button Third Button
o Form1 - D x WN - First Button Second Button Third Button
HNM HNM in
X Form 1.Designer.vb Form1.vb C# DisplayButtonn Imports System.IO Form 1.vb [Design] Form1 . . ReadData 1 reference Public.Class Form1 F... Dimdata().As String Hoc.'handles.the-click-of-Button-2 O references ... Private Sub-Button1_Click(sender. As. Object, e. As - EventArgs) - Handles Button1. Click ..'Streamwrite is used to write lines to the file opened.by. AppendText ...Using writer. As-StreamWriter.=. File. AppendText("C:\display.txt") ...... 'Write the content of a text box -writer.WriteLine(TextBox1.Text) ...End-Using ...TextBox1.Clear() End Sub ...'handles-the-click-of-Button-2 O references ...Private Sub-Button2_Click(sender. As. Object, .e-As.EventArgs). Handles. Button2. Click ......'assign-the-ReadData-output-to-Textbox3 ...TextBox3.Text = ReadData() ... End Sub ... 'Read the data from the file and join it.using.new-line and return the data 3 references ... Private. Function. ReadData() - As String ...... data = File. ReadAllLines ("C:\display.txt") S ct... ReadData = Join(data,System.Environment. NewLine) ... End- Function .'handles the click.of. Button-3 O references ... Private Sub-Button3_Click(sender. As. Object, e As.EventArgs) - Handles Button3. Click ... ..'assign-the-ReadData.outputto-Text-box-2 Fot...TextBox2.Text =- ReadData() + ... End Sub End.Class
We were unable to transcribe this image
We were unable to transcribe this image
We were unable to transcribe this image
. Form1 - O X First Button Second Button Third Button
We were unable to transcribe this image
We were unable to transcribe this image
We were unable to transcribe this image
We were unable to transcribe this image
HNM HNM in