Answered by:
yes or no text on a click

Question
-
I wanted to use a label, button or checkbox as a button.
Have a default of no displayed and if clicked changes to yes, click again and back to no.
I figured a label since can easily display text only.
I couldnt figure how to display a button text without the button.
in any case if i use the following code ona form with many of these labels, dozens or even 100
anyone see any problems with memory etc?
or is there a better way to do it?
the labels defualt is set to no
Private Sub Label24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label24.Click
Static toggle As Boolean
toggle =
If toggle = True Then
Label24.Text =
"yes"Else
Label24.Text =
"no"End If
End Sub
any thoughts appreciated.
Not toggle
Wednesday, June 9, 2010 3:17 AM
Answers
-
if all of the labels on the Form are going to do this (or only some, but then you'll have to limit the AddHandler to those) you can do something like this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each ctrl As Control In Controls If TypeOf ctrl Is Label Then AddHandler ctrl.Click, AddressOf ClickALabel End If Next End Sub Private Sub ClickALabel(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim ctrl As Label = CType(sender, Label) If ctrl.Text = "No" Then ctrl.Text = "Yes" Else ctrl.Text = "No" End If End Sub
- Proposed as answer by Cor Ligthert Wednesday, June 9, 2010 6:37 AM
- Marked as answer by Liliane Teng Tuesday, June 15, 2010 8:30 AM
Wednesday, June 9, 2010 5:31 AM
All replies
-
The only obvious thing is that the toggle variable is not toggled. Could be it's been dropped in the copy-paste but you need to have:
toggle = Not toggle
(the post has 'toggle = ' but it's not setting a value - in fact it won't compile, so looks like something has been lost).
The only comment is that it's difficult to decide whether this is a good approach because I don't know what the aim of the code is.
For a single control this is quite alright. However, you mention that there might be dozens of these. This is not likely to cause memory problems (unless you have a really small amount. Also, you will have to copy the code into each control's click handler. If this is all the handlers have to do it may not be too bad (although it violates the DRY principle - Do not Repeat Yourself). If there's a lot more to do then it is probably better to have a common handler that all the controls call.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.Wednesday, June 9, 2010 5:10 AM -
if all of the labels on the Form are going to do this (or only some, but then you'll have to limit the AddHandler to those) you can do something like this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For Each ctrl As Control In Controls If TypeOf ctrl Is Label Then AddHandler ctrl.Click, AddressOf ClickALabel End If Next End Sub Private Sub ClickALabel(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim ctrl As Label = CType(sender, Label) If ctrl.Text = "No" Then ctrl.Text = "Yes" Else ctrl.Text = "No" End If End Sub
- Proposed as answer by Cor Ligthert Wednesday, June 9, 2010 6:37 AM
- Marked as answer by Liliane Teng Tuesday, June 15, 2010 8:30 AM
Wednesday, June 9, 2010 5:31 AM -
jwavila,Good concrete example of my vague generalised 'have a common handler'. :-)
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.Wednesday, June 9, 2010 5:41 AM -
jwavila,Good concrete example of my vague generalised 'have a common handler'. :-)
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
@DavidI knew what you meant, so it couldn't have been that vague :)
Besides, I'm lazy and would hate to put that block in the click event for dozens or even 100 labels. Talk about bloated code - and writer's cramp.
@Charlie
you can look at setting the Button FlatStyle property to Flat and the FlatAppearance Border property to Control, as well as the MouseDown and MouseOver property to Control (in the System tab). Or just use the labels
Wednesday, June 9, 2010 6:01 AM -
Riced,
I've never seen the sentence common handler for this solution.
In fact it is the code from Jwavilla everything beside a common handler.
VB is the only program language where a handler can direct be connected to a method, which makes code easier to maintain then in other program languages.
The way with the addhandler is the way it has to be done in the other managed code languages and can also be used in VB.
But common handler, never heard about, in the code from Jwavilla are assigned endless handlers which delegates to one method, not one common handler.
Success
CorWednesday, June 9, 2010 6:42 AM -
If I have a number of events (e.g. clicks on a number of buttons) and each event calls the same method then I'd call that method a common handler.
C# also allows you to connect a handler using code - it may be a bit more complex but it's not only VB that can do it.
E.g.
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += new EventHandler(doit);
}
private void doit(object sender, EventArgs e)
{
MessageBox.Show("oops");
}
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.Wednesday, June 9, 2010 7:09 AM -
I was thinking of a form with various selections as labels or buttons, etc.
click on them to select the text you desired.
They wont all be the same yes/no.
thx for the help appreciate it
Friday, June 11, 2010 12:35 AM -
If I have a number of events (e.g. clicks on a number of buttons) and each event calls the same method then I'd call that method a common handler.
C# also allows you to connect a handler using code - it may be a bit more complex but it's not only VB that can do it.
E.g.
private void Form1_Load(object sender, EventArgs e)
{
button1.Click += new EventHandler(doit);
}
private void doit(object sender, EventArgs e)
{
MessageBox.Show("oops");
}
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.Jwavilla has showed to you that already in his first reply, and I've written that this is not called a common handler.
If fact it is this in VB only commonly used to handle one method by more events (or to remove and add temporally the handler of an event). But that does not make it a common handler.
If TypeOf ctrl Is Label Then
AddHandler ctrl.Click, AddressOf ClickALabel
End If
The only way where I can think of that you use the word common in C# is, because in C# this is the only way, although it is in C# also often done by creating first a delegate (address to the method) (can also be done in VB).Like I wrote, VB has (also) more advanced ways to do this, so the word common is only on its place when we say a handler like commonly done in C#.
Success
CorFriday, June 11, 2010 6:56 AM -
Not sure I understand what you are driving at so here's an explanation.
If I have a control that responds to e.g. mouse clicks, then I need to handle the click event. So I write a routine that is called when the click event is raised. Now if I have a bunch of controls each of the controls could call its own method to handle the click event. However, all the controls could call the same method. If all the controls call the same method, then that method is common to all the controls' click events i.e. it is a common handler. I could call it a shared handler, since all the controls share the same method. However, in VB shared is a keyword with an entirely different meaning hence I used the word common.
You may wish to use a different word to indicate that the method called is used by a bunch of controls i.e. there's not a bunch of methods, one for each control. It's purely a matter of English.
As to VB and C#, you said:
VB is the only program language where a handler can direct be connected to a method, which makes code easier to maintain then in other program languages.
I merely pointed out that this is not the case and gave an example of how it could be done in C#.
Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.Friday, June 11, 2010 7:46 AM -
Hi Charlie,
If I were the end user I think I would prefer check boxes instead of clicking labels.
For
Each ctrl In Controls
If TypeOf ctrl Is CheckBox Then
If ctrl.checked = True Then
MessageBox.Show(ctrl.Text)
End If
End If
Next
Mary
Friday, June 11, 2010 7:59 AM