Jump to content
  • Advertisement
Sign in to follow this  

.NET event trapping of newly created elements

This topic is 5152 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 = ""
        If ofdOpenFile.FileName <> "" Then
        End If
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    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)))
        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
    End Sub

    Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    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
    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
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

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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!