StreamReader to RichTextBox problems
-
Ok so I have TabControl and dynamically created TabPage with dynamically created RichTextBox. I am trying to use a OpenFileDialog to open a .rtf in a new TabPage in a new RichTextBox and read the selected rtf file into it. Here is the code that I am using. Somewhat new so feel free to show me better ways of doing what I am trying to do.
Dim sr As StreamReader Try With OpenFileDialog1 If .ShowDialog = Windows.Forms.DialogResult.OK Then If .FileName.EndsWith(".rtf") Then 'Declare new object variables Dim rtbTextArea As New RichTextBox Dim newTab As New TabPage TabControl1.TabPages.Add(newTab) 'Add new Tab Page to tab control With newTab .Text = OpenFileDialog1.FileName .Controls.Add(rtbTextArea) 'Add the rich text Box to the new tab control rtbTextArea.Dock = DockStyle.Fill 'Set Rich Text box area to fill tab page .Focus() End With For Each ctl As Control In TabControl1.SelectedTab.Controls 'Find RichTextBox control If TypeOf ctl Is RichTextBox Then sr = New StreamReader("" & .FileName & "") ctl.Text = sr.ReadToEnd End If Next Else MessageBox.Show("Please choose a Rich Text File (.rtf)") End If End If End With Catch ex As Exception MessageBox.Show(MessageBoxIcon.Exclamation, "An error has occured. Please make your selections again") Finally If Not (sr Is Nothing) Then sr.Close() End If End Try
Thanks, Taen Karth -- modified at 21:47 Friday 2nd September, 2005 -
Ok so I have TabControl and dynamically created TabPage with dynamically created RichTextBox. I am trying to use a OpenFileDialog to open a .rtf in a new TabPage in a new RichTextBox and read the selected rtf file into it. Here is the code that I am using. Somewhat new so feel free to show me better ways of doing what I am trying to do.
Dim sr As StreamReader Try With OpenFileDialog1 If .ShowDialog = Windows.Forms.DialogResult.OK Then If .FileName.EndsWith(".rtf") Then 'Declare new object variables Dim rtbTextArea As New RichTextBox Dim newTab As New TabPage TabControl1.TabPages.Add(newTab) 'Add new Tab Page to tab control With newTab .Text = OpenFileDialog1.FileName .Controls.Add(rtbTextArea) 'Add the rich text Box to the new tab control rtbTextArea.Dock = DockStyle.Fill 'Set Rich Text box area to fill tab page .Focus() End With For Each ctl As Control In TabControl1.SelectedTab.Controls 'Find RichTextBox control If TypeOf ctl Is RichTextBox Then sr = New StreamReader("" & .FileName & "") ctl.Text = sr.ReadToEnd End If Next Else MessageBox.Show("Please choose a Rich Text File (.rtf)") End If End If End With Catch ex As Exception MessageBox.Show(MessageBoxIcon.Exclamation, "An error has occured. Please make your selections again") Finally If Not (sr Is Nothing) Then sr.Close() End If End Try
Thanks, Taen Karth -- modified at 21:47 Friday 2nd September, 2005 -
Ha, I forgot to add the question didn't I. Here goes...When I use the code above the text that was read is displayed in the wrong tab. Not sure why... Thanks, Taen Karth -- modified at 4:38 Saturday 3rd September, 2005
-
Ha, I forgot to add the question didn't I. Here goes...When I use the code above the text that was read is displayed in the wrong tab. Not sure why... Thanks, Taen Karth -- modified at 4:38 Saturday 3rd September, 2005
Nevermind I figured it out. I added
TabControl1.SelectedTab = newTab
Right after
TabControl1.TabPages.Add(newTab) 'Add new Tab Page to tab control
Thanks, Taen Karth