In this post and the next two posts I will try to explain how to rebuild this project exactly in older versions of Visual Studio. It's
also a chance to see all of the code without downloading my attachment.
Note that I have only ever used VS2005 and VS2008. If anything is different in older versions you will have to
decide for yourself how to overcome any difficulties!
1. Open a new Windows Forms Application project
2. Add a UserControl and call it cTabControl
3. Add another UserControl and call it cTabPage
4. Change the UserControl types to TabControl and TabPage:
Press the "Show All Files" button in the Solution Explorer (second button) and expand the [+] sign in front of the
'cTabControl.vb' usercontrol. Open the 'cTabControl.Designer.vb' file and find the following line somewhere at the
top:
vb.net Code:
Inherits System.Windows.Forms.UserControl
Change the word "UserControl" to "TabControl". There may be some errors popping up around the "AutoScaleMode"
property. If they do, comment out those line(s).
Do the same for the 'cTabPage' control, except change "UserControl" into "TabPage" this time.
You can now press the "Show All Files" button again to disable showing all files if you want to.
5. 'cTabControl' code:
Add the following code to the 'cTabControl.vb' usercontrol:
6. 'cTabPage' controls and code:Code:' Gets a tab from a location within the screen ' Works by checking if the 'loc' location is within the tab rectangle Public Function GetTabFromLocation(ByVal loc As Point) As cTabPage For i As Integer = 0 To Me.TabPages.Count - 1 If Me.GetTabRect(i).Contains(loc) Then Return Me.TabPages(i) Exit Function End If Next Return Nothing End Function ' Active tab changed Private Sub cTabControl_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SelectedIndexChanged Dim tab As cTabPage = frmMain.getTab() If tab IsNot Nothing Then frmMain.UpdateTitle() frmMain.UpdateLineInfo(tab) frmMain.UpdateWindowList() Else frmMain.lblLineInfo.Text = "" frmMain.lblFilePath.Text = "" End If End Sub Private Sub cTabControl_ControlAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlAdded frmMain.UpdateWindowList() End Sub Private Sub cTabControl_ControlRemoved(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles MyBase.ControlRemoved frmMain.UpdateWindowList() End Sub
Add a 'RichTextBox' control to your 'cTabPage.vb' usercontrol (drag it into the gray area) and name it 'RTB'. You
can choose which properties you want to set but most importantly is to set the 'Dock' property to 'Fill'.
Add the following code to your 'cTabPage.vb' usercontrol:
Code:#Region " VARIABLES " Dim _DocName, _DocPath As String Dim _DocIndex As Integer Dim _DocSaved, _DocChanged As Boolean #End Region Public Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. frmMain.tabCtrl.SelectedTab = Me RTB.Focus() End Sub #Region " PROPERTIES " ' Name of the document (including extension) Public Property DocName() As String Get Return _DocName End Get Set(ByVal value As String) _DocName = value UpdateTitle() End Set End Property ' Filepath of the document Public Property DocPath() As String Get Return _DocPath End Get Set(ByVal value As String) _DocPath = value End Set End Property ' Index of the document (only for 'Untitled' documents) ' Example: Untitled 3 --> index = 3 Public Property DocIndex() As Integer Get Return _DocIndex End Get Set(ByVal value As Integer) _DocIndex = value End Set End Property ' Whether or not the document already exists on the computer ' (has been saved before or not) Public Property DocSaved() As Boolean Get Return _DocSaved End Get Set(ByVal value As Boolean) _DocSaved = value End Set End Property ' Whether or not the document text has been changed since the last save action Public Property DocChanged() As Boolean Get Return _DocChanged End Get Set(ByVal value As Boolean) _DocChanged = value End Set End Property #End Region #Region " MISC FUNCTIONS " ' Updates the document title to reflect the Changed status Public Sub UpdateTitle() If DocChanged Then Me.Text = Me.DocName & "*" Else Me.Text = Me.DocName End If End Sub #End Region #Region " DOCUMENT RELATED " ' Checks if the document has changed and asks whether or not to close ' If yes --> tab is saved then closed. If no --> tab is not saved then closed. If cancel --> tab is not closed Public Sub Close() Dim blnCancel As Boolean = False If DocChanged Then Dim str As String If DocPath <> "" Then str = "The following document has changed:" & ControlChars.CrLf & ControlChars.CrLf & DocName & ControlChars.CrLf & "(" & DocPath & ")" & ControlChars.CrLf & ControlChars.CrLf & "Do you want to save before closing?" Else str = "The following document has changed:" & ControlChars.CrLf & ControlChars.CrLf & DocName & ControlChars.CrLf & ControlChars.CrLf & "Do you want to save before closing?" End If Dim msgBoxRes As MsgBoxResult = MessageBox.Show(str, "Document has changed", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) If msgBoxRes = MsgBoxResult.Cancel Then blnCancel = True ElseIf msgBoxRes = MsgBoxResult.Yes Then If DocSaved Then frmMain.SaveTab(Me) Else frmMain.SaveTabAs(Me) End If End If End If If Not blnCancel Then Me.Dispose() End If End Sub #End Region #Region " RTB RELATED " ' Changes the LineInfo ("Ln x, Ch y") Private Sub RTB_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RTB.SelectionChanged frmMain.UpdateLineInfo(Me) End Sub Private Sub RTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RTB.TextChanged If Not Me.DocChanged Then Me.DocChanged = True UpdateTitle() frmMain.UpdateTitle() End If End Sub ' Loads a file into the RTB ' Throws error and returns False on error Public Function LoadFile(ByVal path As String) As Boolean Try Dim richTb As RichTextBox = RTB RTB.LoadFile(path, RichTextBoxStreamType.PlainText) Return True Catch ex As Exception WriteError(ex.Message) Return False End Try End Function ' Saves a file into the RTB ' Throws error and returns False on error Public Function SaveFile(ByVal path As String) As Boolean Try RTB.SaveFile(path, RichTextBoxStreamType.PlainText) Return True Catch ex As Exception WriteError(ex.Message) Return False End Try End Function #End Region




Reply With Quote