Results 1 to 8 of 8

Thread: Event handler on an external form/module

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Question Event handler on an external form/module

    Hello

    I have a simple, but important question: How can I raise events of my form1 on another module/form?

    My form tempalte is crowded with events (50 of them contextmenustrip events)

    How can I place those events on another form/module to clean it up a little?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Event handler on an external form/module

    Try using #region instead... organize the code, put things into regions, then collapse the regions. If you move the event handlers to another location, you're going to just complicate things.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Event handler on an external form/module

    You could also use a partial class to hold blocks of code.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Event handler on an external form/module

    Quote Originally Posted by bergerkiller View Post
    Hello

    I have a simple, but important question: How can I raise events of my form1 on another module/form?

    My form tempalte is crowded with events (50 of them contextmenustrip events)

    How can I place those events on another form/module to clean it up a little?
    You would need to refactor your code. Not only are so many events unrealistic but also hard to debug and maintain.

    I assume most of those event handlers are repetitive and can be consolidated into fewer methods. Also you can group the event handlers that have same body by just appending the appropriate Handles clause to it, and using the sender parameter of event handler which is seldom used.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Event handler on an external form/module

    Well, I am making a code editor program.
    All those 50 events cause one sub to add a particular type of code to the tweaker field.

    Here is an example of those code:
    Code:
        Private Sub ToolStripMenuItem10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem10.Click
            'Ability
            Dim value As String = "rem ---BeginComp:Ability ---"
            value &= vbCrLf & "ObjectTemplate.createComponent Ability"
            value &= vbCrLf & "ObjectTemplate.Ability.hasHealingAbility 1"
            value &= vbCrLf & "ObjectTemplate.Ability.hasRepairingAbility 1"
            value &= vbCrLf & "ObjectTemplate.Ability.hasAmmoAbility 1"
            value &= vbCrLf & "ObjectTemplate.Ability.radarRadius 10"
            value &= vbCrLf & "rem ---EndComp ---"
            AddCodeToTextboxNoReplace(value)
        End Sub
    And I have 50 of them, and sometimes all those (minimized) comps open and then it takes minutes to minimize them all again.

    Anyone has an example/tutorial/checklist to place these events somewhere else?

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Event handler on an external form/module

    I assume that you are just changing only one line in that procedure (i.e. radarRadius value) for each menu item. If this is the case then you can handle all of them in same event procedure like this:
    vb.net Code:
    1. Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    2.                 Handles ToolStripMenuItem10.Click, ToolStripMenuItem11.Click, ToolStripMenuItem12.Click   '<-- all all of them here
    3.  
    4.         Dim radarRadius As String = CType(sender, ToolStripMenuItem).Name.Substring(17) ' remove the word 'ToolStripMenuItem' from it
    5.                                                                                         ' so that we are left with only the number.
    6.         'Ability
    7.         Dim value As String = "rem ---BeginComp:Ability ---"
    8.         value &= vbCrLf & "ObjectTemplate.createComponent Ability"
    9.         value &= vbCrLf & "ObjectTemplate.Ability.hasHealingAbility 1"
    10.         value &= vbCrLf & "ObjectTemplate.Ability.hasRepairingAbility 1"
    11.         value &= vbCrLf & "ObjectTemplate.Ability.hasAmmoAbility 1"
    12.         value &= vbCrLf & "ObjectTemplate.Ability.radarRadius " & radarRadius
    13.         value &= vbCrLf & "rem ---EndComp ---"
    14.         AddCodeToTextboxNoReplace(value)
    15.     End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Event handler on an external form/module

    Ah wait, well not that I want to change the radius, lol, but this does give me an idea how to get the clicked toolstrip. Thanks, I'll try some testing, then I can change 50 subs to 1 =D

    Ok works, very, very much thanks you's for the "CType(sender, ToolStripMenuItem).Name" code, it was exactly what I needed =D

    At this point it looks like this:
    Code:
      Private Sub CompMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
            ToolStripMenuItem3.Click, ToolStripMenuItem4.Click, ToolStripMenuItem5.Click, ToolStripMenuItem6.Click, _
            ToolStripMenuItem7.Click, ToolStripMenuItem8.Click, ToolStripMenuItem10.Click, ToolStripMenuItem11.Click, _
            ToolStripMenuItem12.Click, ToolStripMenuItem13.Click, ToolStripMenuItem14.Click, ToolStripMenuItem15.Click, _
            ToolStripMenuItem16.Click, ToolStripMenuItem18.Click, ToolStripMenuItem19.Click, ToolStripMenuItem20.Click, _
            ToolStripMenuItem21.Click, ToolStripMenuItem23.Click, ToolStripMenuItem24.Click, ToolStripMenuItem26.Click, _
            ToolStripMenuItem27.Click, ToolStripMenuItem28.Click, ToolStripMenuItem29.Click, ToolStripMenuItem30.Click, _
            ToolStripMenuItem31.Click, ToolStripMenuItem33.Click, ToolStripMenuItem34.Click, ToolStripMenuItem36.Click, _
            ToolStripMenuItem37.Click, ToolStripMenuItem38.Click, ToolStripMenuItem40.Click, ToolStripMenuItem41.Click, _
            ToolStripMenuItem43.Click, ToolStripMenuItem44.Click, ToolStripMenuItem46.Click, ToolStripMenuItem47.Click, _
            ToolStripMenuItem48.Click, ToolStripMenuItem49.Click, ToolStripMenuItem50.Click, ToolStripMenuItem52.Click, _
            ToolStripMenuItem53.Click, ToolStripMenuItem55.Click, ToolStripMenuItem56.Click, ToolStripMenuItem57.Click, _
            ToolStripMenuItem58.Click, ToolStripMenuItem59.Click
            Dim type As Integer = CType(sender, ToolStripMenuItem).Name.Substring(17)
            Dim value As String = ""
            If type = 3 Then
                value = "rem ---BeginComp:DefaultPenetrateComp ---"
                value &= vbCrLf & "ObjectTemplate.createComponent DefaultPenetrateComp"
                value &= vbCrLf & "ObjectTemplate.penetrate.neverPenetrate 0"
                value &= vbCrLf & "ObjectTemplate.penetrate.allowSolidPenetration 1"
                value &= vbCrLf & "ObjectTemplate.penetrate.allowLiquidPenetration 1"
                value &= vbCrLf & "rem ---EndComp ---"
            End If
            If type = 4 Then
                value = "rem ---BeginComp:DefaultProjSoundComp ---"
                value &= vbCrLf & "ObjectTemplate.createComponent DefaultProjSoundComp"
                value &= vbCrLf & "rem ---EndComp ---"
            End If
            If type = 5 Then
                value = "rem ---BeginComp:DefaultRicochetComp ---"
                value &= vbCrLf & "ObjectTemplate.createComponent DefaultRicochetComp"
                value &= vbCrLf & "rem ---EndComp ---"
            End If
            If type = 6 Then
                value = "rem ---BeginComp:DefaultSoundComp ---"
                value &= vbCrLf & "ObjectTemplate.createComponent DefaultSoundComp"
                value &= vbCrLf & "ObjectTemplate.sound.maxSoundsPerBurst 1"
                value &= vbCrLf & "ObjectTemplate.sound.noisy 0"
                value &= vbCrLf & "rem ---EndComp ---"
            End If
    Thanks
    Last edited by bergerkiller; Dec 7th, 2009 at 10:27 AM.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Event handler on an external form/module

    By the way, you can close all open subs and regions in one shot with Ctrl+M+O.

    I would organize the whole thing with regions.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width