Sign in to follow this  

[.net] VB: Getting properties of textbox on current tab page

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got a series of tabpages and am creating a new one every time a source file is opened in the z80 ASM IDE I'm developing. Now, I've got a class called sourceFile which inherits from TextBox, and am adding these to the tab pages. When I switch from tab to tab, I want to update a status bar to display the file details. How can I get the properties of the text box added to the new tab pages? Current source runs thusly: (Source might help)
Public Class frmMain
    Inherits System.Windows.Forms.Form

    Public Const sourceNew = 0
    Public Const sourceDirty = 1
    Public Const sourceSaved = 2


    Private Class sourceFile
        Inherits System.Windows.Forms.TextBox

        Sub sourceDirtied(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.TextChanged
            fileStatus = sourceDirty
            Beep()
        End Sub
        Public fileName As String
        Public fileTitle As String
        Public fileStatus As Integer
    End Class
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.
    Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem
    Friend WithEvents mnuMainMenu As System.Windows.Forms.MainMenu
    Friend WithEvents mnuFile As System.Windows.Forms.MenuItem
    Friend WithEvents mnuFileNew As System.Windows.Forms.MenuItem
    Friend WithEvents mnuFileOpen As System.Windows.Forms.MenuItem
    Friend WithEvents mnuFileSave As System.Windows.Forms.MenuItem
    Friend WithEvents mnuFileSaveAs As System.Windows.Forms.MenuItem
    Friend WithEvents mnuFileExit As System.Windows.Forms.MenuItem
    Friend WithEvents ofdOpenFile As System.Windows.Forms.OpenFileDialog
    Friend WithEvents tabWindows As System.Windows.Forms.TabControl
    Friend WithEvents statStats As System.Windows.Forms.StatusBar
    Friend WithEvents pnlDirty As System.Windows.Forms.StatusBarPanel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.mnuMainMenu = New System.Windows.Forms.MainMenu()
        Me.mnuFile = New System.Windows.Forms.MenuItem()
        Me.mnuFileNew = New System.Windows.Forms.MenuItem()
        Me.MenuItem8 = New System.Windows.Forms.MenuItem()
        Me.mnuFileOpen = New System.Windows.Forms.MenuItem()
        Me.MenuItem4 = New System.Windows.Forms.MenuItem()
        Me.mnuFileSave = New System.Windows.Forms.MenuItem()
        Me.mnuFileSaveAs = New System.Windows.Forms.MenuItem()
        Me.MenuItem7 = New System.Windows.Forms.MenuItem()
        Me.mnuFileExit = New System.Windows.Forms.MenuItem()
        Me.ofdOpenFile = New System.Windows.Forms.OpenFileDialog()
        Me.tabWindows = New System.Windows.Forms.TabControl()
        Me.statStats = New System.Windows.Forms.StatusBar()
        Me.pnlDirty = New System.Windows.Forms.StatusBarPanel()
        CType(Me.pnlDirty, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'mnuMainMenu
        '
        Me.mnuMainMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile})
        '
        'mnuFile
        '
        Me.mnuFile.Index = 0
        Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFileNew, Me.MenuItem8, Me.mnuFileOpen, Me.MenuItem4, Me.mnuFileSave, Me.mnuFileSaveAs, Me.MenuItem7, Me.mnuFileExit})
        Me.mnuFile.Text = "&File"
        '
        'mnuFileNew
        '
        Me.mnuFileNew.Index = 0
        Me.mnuFileNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN
        Me.mnuFileNew.Text = "&New"
        '
        'MenuItem8
        '
        Me.MenuItem8.Index = 1
        Me.MenuItem8.Text = "-"
        '
        'mnuFileOpen
        '
        Me.mnuFileOpen.Index = 2
        Me.mnuFileOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO
        Me.mnuFileOpen.Text = "&Open"
        '
        'MenuItem4
        '
        Me.MenuItem4.Index = 3
        Me.MenuItem4.Text = "-"
        '
        'mnuFileSave
        '
        Me.mnuFileSave.Index = 4
        Me.mnuFileSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS
        Me.mnuFileSave.Text = "&Save"
        '
        'mnuFileSaveAs
        '
        Me.mnuFileSaveAs.Index = 5
        Me.mnuFileSaveAs.Shortcut = System.Windows.Forms.Shortcut.F12
        Me.mnuFileSaveAs.Text = "Save &As"
        '
        'MenuItem7
        '
        Me.MenuItem7.Index = 6
        Me.MenuItem7.Text = "-"
        '
        'mnuFileExit
        '
        Me.mnuFileExit.Index = 7
        Me.mnuFileExit.Shortcut = System.Windows.Forms.Shortcut.AltF4
        Me.mnuFileExit.Text = "E&xit"
        '
        'ofdOpenFile
        '
        Me.ofdOpenFile.DefaultExt = "z80"
        Me.ofdOpenFile.Filter = "ZiLOG Z80 Assembly Source|*.z80|Generic Assembly Source|*.asm|Include Files|*.inc" & _
        "|All Files|*.*"
        '
        'tabWindows
        '
        Me.tabWindows.Location = New System.Drawing.Point(0, 24)
        Me.tabWindows.Name = "tabWindows"
        Me.tabWindows.SelectedIndex = 0
        Me.tabWindows.Size = New System.Drawing.Size(488, 312)
        Me.tabWindows.TabIndex = 1
        '
        'statStats
        '
        Me.statStats.Location = New System.Drawing.Point(0, 411)
        Me.statStats.Name = "statStats"
        Me.statStats.Panels.AddRange(New System.Windows.Forms.StatusBarPanel() {Me.pnlDirty})
        Me.statStats.ShowPanels = True
        Me.statStats.Size = New System.Drawing.Size(672, 22)
        Me.statStats.TabIndex = 2
        '
        'pnlDirty
        '
        Me.pnlDirty.Text = "Saved"
        Me.pnlDirty.ToolTipText = "Displays file status"
        '
        'frmMain
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
        Me.ClientSize = New System.Drawing.Size(672, 433)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.statStats, Me.tabWindows})
        Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Menu = Me.mnuMainMenu
        Me.Name = "frmMain"
        Me.Text = "Latenite z80 Editor"
        CType(Me.pnlDirty, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
        ofdOpenFile.FileName = ""
        ofdOpenFile.ShowDialog()
        If ofdOpenFile.FileName <> "" Then
            openFile(ofdOpenFile.FileName)
        End If
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        newFile()
    End Sub

    Sub newFile()
        addTab("untitled.z80", "", "", sourceNew)
    End Sub

    Sub openFile(ByVal fileName As String)
        Dim i As Long
        For i = 0 To tabWindows.TabPages.Count - 1
            If tabWindows.TabPages(i).Name = getFileTitle(fileName) Then
                Exit Sub
            End If
        Next i
        Dim strFile As String
        FileOpen(1, ofdOpenFile.FileName, OpenMode.Input)
        strFile = (InputString(1, LOF(1)))
        FileClose(1)
        addTab(getFileTitle(fileName), strFile, fileName, sourceSaved)
    End Sub

    Function getFileTitle(ByVal fileName As String) As String
        Dim findName() As String
        findName = fileName.Split("\")
        Return findName(UBound(findName))
    End Function

    Sub addTab(ByVal fileTitle As String, ByVal defaultText As String, ByVal fileName As String, ByVal fileStatus As Integer)
        Dim newTextBox As New sourceFile()
        With newTextBox
            .AcceptsReturn = True
            .AcceptsTab = True
            .Font = New System.Drawing.Font("Lucida Console", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            .Location = New System.Drawing.Point(0, 0)
            .MaxLength = 0
            .Multiline = True
            .ScrollBars = System.Windows.Forms.ScrollBars.Both
            .Size = New System.Drawing.Size(392, 232)
            .Text = defaultText
            .Name = fileTitle
            .WordWrap = False
            .fileName = fileName
            .fileTitle = fileTitle
            .fileStatus = fileStatus
        End With
        Dim newTab As New TabPage()
        newTab.Name = fileTitle
        newTab.Text = fileTitle
        newTab.Controls.Add(newTextBox)
        tabWindows.TabPages.Add(newTab)
        resizeControls()
    End Sub

    Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        resizeControls()
    End Sub

    Sub resizeControls()
        tabWindows.Width = Me.Width - 8
        tabWindows.Height = Me.Height - tabWindows.Top - 55 - statStats.Height
        Dim textBoxProperties As New TextBox()
        Dim i As Long
        For i = 0 To tabWindows.TabPages.Count - 1
            With tabWindows.TabPages(i).Controls.Item(0)
                .Width = tabWindows.TabPages.Item(i).Width
                .Height = tabWindows.TabPages.Item(i).Height
            End With
        Next i
    End Sub


    Private Sub tabWindows_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tabWindows.SelectedIndexChanged
        resizeControls()
        Dim x As sourceFile
        Select Case x.fileStatus
            '    Case sourceNew
        End Select
    End Sub
End Class

Share this post


Link to post
Share on other sites
For a very dynamic solution you could loop through the Controls collection of the active tabpage...

foreach(Control ctrl in tabControl1.SelectedTab.Controls)
{
if(ctrl is TextBox)
{
TextBox tb = ctrl as Textbox;
...
}
}

Share this post


Link to post
Share on other sites
I'm using VB - I don't know if there's a foreach in that.
The textboxes aren't really textboxes - they're "sourceFile"s, sourceFile being a class that inherits TextBox. Would I do this?
foreach(Control ctrl in tabControl1.SelectedTab.Controls)
{
if(ctrl is sourceFile)
{
sourceFile tb = ctrl as Textbox;
MsgBox(sourceFile.Text)
}
}


Say, to display the text in the current textbox when you tab across?

Share this post


Link to post
Share on other sites
Hmm, well if you don't know whether VB has a For Each you should perhaps invest some time in learning the language...

Yes, it has a for each!

Dim ctrl As Control
For Each ctrl in tabPage1.SelectedTab.Controls
If ctrl is SourceFile Then
Dim sf As SourceFile = Ctype(ctrl, SourceFile)
MessageBox.Show(sf.Text)
End If
Next

Do learn VB and Inheritance!

Cheers

Share this post


Link to post
Share on other sites
Quote:
Original post by ernow
Hmm, well if you don't know whether VB has a For Each you should perhaps invest some time in learning the language...
Do learn VB and Inheritance!
Cheers


Any good pointers for a downloadable tutorial? Most books I've seen suck and are hard to search [grin]
I'm used to VB6, alas, so I guess I have to unlearn my bad practices. [sad]

Share this post


Link to post
Share on other sites
Quote:
Original post by ernow
I use the MSDN for most info. The best book I know is Professional VB.NET.

I have no internet connection at home, therefore no MSDN-ing for me. Which really sucks. [sad] Thanks anyway!

Share this post


Link to post
Share on other sites

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this