|
-
Dec 6th, 2009, 12:43 PM
#1
Thread Starter
Fanatic Member
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?
-
Dec 6th, 2009, 01:14 PM
#2
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
-
Dec 6th, 2009, 01:25 PM
#3
Re: Event handler on an external form/module
You could also use a partial class to hold blocks of code.
-
Dec 6th, 2009, 01:50 PM
#4
Re: Event handler on an external form/module
 Originally Posted by bergerkiller
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.
-
Dec 6th, 2009, 05:50 PM
#5
Thread Starter
Fanatic Member
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?
-
Dec 6th, 2009, 06:01 PM
#6
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:
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles ToolStripMenuItem10.Click, ToolStripMenuItem11.Click, ToolStripMenuItem12.Click '<-- all all of them here Dim radarRadius As String = CType(sender, ToolStripMenuItem).Name.Substring(17) ' remove the word 'ToolStripMenuItem' from it ' so that we are left with only the number. '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 " & radarRadius value &= vbCrLf & "rem ---EndComp ---" AddCodeToTextboxNoReplace(value) End Sub
-
Dec 7th, 2009, 10:08 AM
#7
Thread Starter
Fanatic Member
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.
-
Dec 7th, 2009, 10:11 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|