 |
 |

07-31-2010, 04:10 PM
|
 |
Junior Contributor
|
|
Join Date: Oct 2003
Location: Pennsylvania
Posts: 224
|
|
Control Array question
|
|
ok , I've read thru Iceplug's tutorial on control arrays and got it working. I tried to add a new event by basically copying his click event code and calling it a doubleclick event. I used the proper addhandler and assigned the correct name to my sub. I have the click event changing the background color to blue and the doubleclick event changing the background color to green. The background color does not change to green. Why?
Heres the tutorial with my new code encased in asterisks ...
Code:
Imports System
Imports System.Windows.Forms
Public Class form1
Private Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim F As Controlland = New Controlland
F.ShowDialog()
End Sub
End Class
Public Class Controlland
Inherits System.Windows.Forms.Form
Friend Buttons(99) As Button
'Declare the buttons.
Public Sub New() 'This is the Sub New, the constructor, etc.
MyBase.New() 'Initialize the form itself.
CreateArray()
End Sub
Public Sub CreateArray() 'This subroutine initializes the array.
'This will be done in a For Loop
Dim LV As Integer 'LV is our loop variable.
For LV = 0 To 99 'Set up the loop to go through each button in the array.
Buttons(LV) = New Button 'Create the button.
'This creates the button at the top left of the form with the default size.
Buttons(LV).Left = Buttons(LV).Width * (LV Mod 10)
Buttons(LV).Top = Buttons(LV).Height * (LV \ 10)
'Initializes the button locations.
Buttons(LV).Name = "Btn" & LV
Buttons(LV).Text = LV.ToString()
'Add an event handler and whatever you may want to add.
AddHandler Buttons(LV).Click, AddressOf ButtonClickEventProc
'This event handler handles the click event of all of the buttons.
' **********************************************************************
AddHandler Buttons(LV).DoubleClick, AddressOf ButtonDoubleClickEventProc
' **********************************************************************
Next
Me.Controls.AddRange(Buttons)
'Add... ALLLL of the buttons to the form.
End Sub
Public Sub ButtonClickEventProc(ByVal sender As Object, ByVal e As EventArgs)
'Button control array event handler.
'sender has to be recast as a Button
Dim Btn As Button = DirectCast(sender, Button)
Btn.BackColor = Color.Blue
End Sub
' *********************************************************************************
Public Sub ButtonDoubleClickEventProc(ByVal sender As Object, ByVal e As EventArgs)
'Button control array event handler.
'sender has to be recast as a Button
Dim Btn As Button = DirectCast(sender, Button)
Btn.BackColor = Color.Green
End Sub
' *********************************************************************************
End Class
|
|

07-31-2010, 08:37 PM
|
 |
Impetuous & volatile
* Expert *
|
|
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,016
|
|
Taking a lot at the documentation for the DoubleClick event of the button it says the following under remarks:
Quote:
|
By default, the ControlStyles.StandardClick and ControlStyles.StandardDoubleClick style bits are set to false for the Button control, and the DoubleClick event is not raised.
|
Thus per default you will not be able to make use of the DoubleClick event. To enable the 'StandardDoubleClick' style you will have to call the SetStyles method, however, it is protected and therefore you won't have access to it. That is unless you subclass the button control. If you do that then you can manually override the control style in the constructor to allow double clicks.
Edit: Just tried out subclassing the button to ensure that this solution actually worked as the documentation said it would. The following class actually raises the DoubleClick event appropriately
Code:
Public Class DblClickButton
Inherits Button
Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.StandardDoubleClick, True)
Me.SetStyle(ControlStyles.StandardClick, True)
End Sub
End Class
|
__________________
Reading is the foundation for all knowledge - Unknown.
|

07-31-2010, 09:27 PM
|
 |
Junior Contributor
|
|
Join Date: Oct 2003
Location: Pennsylvania
Posts: 224
|
|
|
|
Thanks Qua. I understand that you basically have to enable the doubleclick event now. I actually thought my above code was faulty. The subclass code you posted, how do
I implement it? Do I just place it inside the Controlland class or outside it. Is there any other coding I need to do to make it work? Subclassing is still confusing to me.
|
|

07-31-2010, 10:18 PM
|
 |
Impetuous & volatile
* Expert *
|
|
Join Date: Apr 2005
Location: 127.0.0.1
Posts: 2,016
|
|
Quote:
Originally Posted by TRSDOSBasic79
Thanks Qua. I understand that you basically have to enable the doubleclick event now. I actually thought my above code was faulty. The subclass code you posted, how do
I implement it? Do I just place it inside the Controlland class or outside it. Is there any other coding I need to do to make it work? Subclassing is still confusing to me.
|
The code I posted above subclasses the Button class. Subclassing is a fundamental concept within object oriented programming in which you extend or change either the behaviour or state of the class. In this case you're changing whether or not the double click event is raised.
Classes are usually placed in a file of their own. Thus above class would be put in a file called DblClickButton.vb/cs.
The smart thing that follows from the concept of polymorphism (subclassing in this case) is that you can use the DblClickButton class anywhere you would normally use a button. Thus you can have an array of buttons where some of them allow double click and some of them don't. To illustrate this see the code below:
Code:
Dim aButton As Button 'Normal Button
aButton = new DblClickButton() ' this is legal because dblclickbutton inherits from Button
aButton.left = 10
aButton.top = 10
aButton.Text = "@"
|
__________________
Reading is the foundation for all knowledge - Unknown.
|

08-01-2010, 09:45 AM
|
 |
Ultimate Contributor
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,615
|
|
Here's a pedantic note that I always feel compelled to make.
Subclassing is a fundamental Windows API concept in which you use the SetWindowLong() function to replace the WndProc() used by a particular child window so you can implement new functionality. VB6 used subclassing to extend controls. In .NET, it is as rare as a unicorn to find a need to use subclassing.
Inheritance is a pillar of OOP in which you derive a new class from a base class, inheriting the data and behavior of the base class and gaining the ability to augment it with new behavior. VB6 couldn't do this, but this is the bread and butter of extensibility in .NET.
They are similar, but the implementation is very different. I think I'm going to give up on banging this drum though; this will probably be the last time I point it out.
Anyway, I'm curious why you want special behavior when a button is double-clicked, TRSDOSBasic79. This is not a standard behavior of the button control. In general, UI design guidelines point out that a double-click is not normally a good way to initiate an action because it's not discoverable; there's usually no indication that you have to double-click to make something happen. Think about icons on the desktop from the viewpoint of a person who has never used a computer. You *might* come to the conclusion that you need to click them, but would you discover double-click on your own? I didn't, and neither do most people in usability studies. You might counter by saying, "Everyone knows how to double-click icons now!" but do you believe everyone knows to double-click buttons? Have you ever double-clicked a button and expected something different than two single clicks? Even if you put a label that says "double-click to X", many people won't read it. I think this is a bad idea.
So I feel like the right answer to this question is to use a different UI, but I can't suggest a better one because all I know is the solution you are trying. An explanation of the problem might reveal a better and more reliable solution.
|
|

08-01-2010, 11:07 AM
|
 |
Junior Contributor
|
|
Join Date: Oct 2003
Location: Pennsylvania
Posts: 224
|
|
Quote:
Originally Posted by AtmaWeapon
Anyway, I'm curious why you want special behavior when a button is double-clicked, TRSDOSBasic79. This is not a standard behavior of the button control.
|
No No Atma, I was reading on iceplug's tutorial and he used buttons. He stated you can add events. He used "Click" I wanted to try to add an event. I simply tried "DoubleClick". I didn't realize it was so difficult. It is not for some special app that I'm trying to cobble together, it was just an exercise from the tutorial and I was trying to understand and learn from it.
I learned that raising the doubleclick event for a button is not just hard, but practically useless.
I suppose I can erase the button code from Iceplug's tutorial and select another control and try from there.
|
|

08-01-2010, 04:29 PM
|
 |
Ultimate Contributor
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,615
|
|
Or pick another event that's easy to raise. MouseEnter and MouseLeave come to mind.
|
|

08-01-2010, 07:50 PM
|
 |
Junior Contributor
|
|
Join Date: Oct 2003
Location: Pennsylvania
Posts: 224
|
|
Quote:
Originally Posted by AtmaWeapon
Or pick another event that's easy to raise. MouseEnter and MouseLeave come to mind.
|
Took your advice and did this ...
Code:
Imports System
Imports System.Windows.Forms
Public Class form1
Private Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim F As Controlland = New Controlland
F.ShowDialog()
End Sub
End Class
Public Class Controlland
Inherits System.Windows.Forms.Form
Friend Buttons(99) As Button
'Declare the buttons.
Public Sub New() 'This is the Sub New, the constructor, etc.
MyBase.New() 'Initialize the form itself.
CreateArray()
End Sub
Public Sub CreateArray() 'This subroutine initializes the array.
'This will be done in a For Loop
Dim LV As Integer 'LV is our loop variable.
For LV = 0 To 99 'Set up the loop to go through each button in the array.
Buttons(LV) = New Button 'Create the button.
'This creates the button at the top left of the form with the default size.
Buttons(LV).Left = Buttons(LV).Width * (LV Mod 10)
Buttons(LV).Top = Buttons(LV).Height * (LV \ 10)
'Initializes the button locations.
Buttons(LV).Name = "Btn" & LV
Buttons(LV).Text = LV.ToString()
'Add an event handler and whatever you may want to add.
AddHandler Buttons(LV).MouseEnter, AddressOf MouseEnterEventProc
AddHandler Buttons(LV).MouseLeave, AddressOf MouseLeaveEventProc
Next
Me.Controls.AddRange(Buttons)
'Add... ALLLL of the buttons to the form.
End Sub
Public Sub MouseEnterEventProc(ByVal sender As Object, ByVal e As EventArgs)
'Button control array event handler.
'sender has to be recast as a Button
Dim Btn As Button = DirectCast(sender, Button)
Btn.BackColor = Color.Blue
End Sub
Public Sub MouseLeaveEventProc(ByVal sender As Object, ByVal e As EventArgs)
'Button control array event handler.
'sender has to be recast as a Button
Dim Btn As Button = DirectCast(sender, Button)
Btn.BackColor = Color.White
End Sub
End Class
Mouseenter changes it to blue
Mouseleave changes it to white.
I FINALLY understand how to do a control array and add an event!
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| |
|
|
|
 |
|