Sign in to follow this  

.NET event trapping of newly created elements

This topic is 4841 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'm developing a new version of my z80 ASM 'IDE', but in VB.NET rather than VB6. Here's the code so far (Windows Form creation code cut out)
Public Class frmMain
    Inherits System.Windows.Forms.Form


    Private Class sourceFile
        Inherits System.Windows.Forms.TextBox
        Public fileName As String
        Public fileTitle As String
        Public fileDirty As Boolean
        Public index As Long
    End Class

    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", "", "")
    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)
    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)
        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
            .Text = defaultText
            .Name = fileTitle
            .WordWrap = False
            .fileName = fileName
            .fileTitle = fileTitle
            .fileDirty = False
        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 - 52
        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()
    End Sub
End Class

Now, when I add a new sourceFile, how can I trap it's "change" event? I'm sorry, I'm used to VB6 and control arrays so this is all a bit new to me!

Share this post


Link to post
Share on other sites
Quote:
Original post by joanusdmentia
Do you mean when the text of the TextBox changes? Check out the TextBox.TextChanged event. If you don't know what delegates are then look them up as well.


I can tell when a particular text box changes, yes, if I draw one on the form in the editor and work on it there. It's how I can tell when a textbox that's been added to the form at runtime has changed...

Share this post


Link to post
Share on other sites
Ahh, what you want to do is programmatically add a handler for the event. I mainly use C# for .NET programming, but MSDN spat this out at me for adding a handler to an event:
AddHandler MyObject.Event1, AddressOf Me.MyEventHandler

So in your case it would be something like
AddHandler sourceFile.TextChanged, AddressOf Me.TextChangedHandler

where TextChangedHandler is
Sub TextChanged(sender As Object, e As EventArgs)
' Do stuff
EndSub

And similarly to remove a handler...
RemoveHandler MyObject.Event1, AddressOf Me.MyEventHandler

Share this post


Link to post
Share on other sites

This topic is 4841 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