Recent

Author Topic: Which button is clicked?  (Read 21985 times)

Haeleng

  • New Member
  • *
  • Posts: 10
Re: Which button is clicked?
« Reply #15 on: May 30, 2017, 03:44:05 pm »
Hi guys,

Thanks so far for all the tips. I have it working thanks to your suggestions. Solved!
I made a stupid error. Have to take a break. Have a nice day

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Which button is clicked?
« Reply #16 on: May 30, 2017, 03:45:33 pm »
@Molly: I strongly advise against giving run-time created objects a name because then it is in your responsibility to make sure that the name is unique.
If thou are unable to create unique name/numbers using Pascal then thou are not worthy of dynamic programming ;-)

You have a point but, please also do not exaggerate the negativity of it. e.g. dumping a load of components on your form at design-time is simply the same thing, and by magic the designer is able to come up with unique names.

I already stated that (at least for me) using component names or not for dynamic components depends on the situation.

I would even be bolder and state the opposite: How dare you create components without a (unique) name, so that you are unable to (uniquely) identify them other than by their address (and what happens if for some reason you lost that address).

Quote
As howardpc already noted the name is useless at run-time, maybe only for debugging. If you need one of these objects you can access it using its variable name.
Or i can search for it using the application variable. That way i am able to access a component from another form, without the need to clutter things creating a specific (global) variable for it.

If the point of the majority that oppose to naming dynamically created components is to not burden a newbie, then i am able to see a point, otherwise and so far i have great difficulty with the arguments that are expressed against it.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Which button is clicked?
« Reply #17 on: May 30, 2017, 03:50:33 pm »
I cannot try wp's clickme.zip. I'm using Lazarus 1.6.4 and got this error:
Either use a trunk Lazarus (or 1.8 RC1), or if you stick with 1.6.4 you'll need to edit the form's .lfm and remove the line that sets the listbox's Options property.

wp

  • Hero Member
  • *****
  • Posts: 13560
Re: Which button is clicked?
« Reply #18 on: May 30, 2017, 04:18:15 pm »
I cannot try wp's clickme.zip.
I am sorry - I used Lazarus trunk to write this demo, and trunk adds a new property to TListbox which Laz 1.6.4 does not know. I replaced the demo project a few posts above by a version which can be loaded by Laz 1.6.4.

Handoko

  • Hero Member
  • *****
  • Posts: 5545
  • My goal: build my own game engine using Lazarus
Re: Which button is clicked?
« Reply #19 on: May 30, 2017, 04:43:50 pm »
Re-tested it and it worked.

wp

  • Hero Member
  • *****
  • Posts: 13560
Re: Which button is clicked?
« Reply #20 on: May 30, 2017, 05:28:57 pm »
@molly:
Quote
I would even be bolder and state the opposite: How dare you create components without a (unique) name, so that you are unable to (uniquely) identify them other than by their address (and what happens if for some reason you lost that address).

You are talking of "lazy-typer" code such as

Code: Pascal  [Select][+][-]
  1. with TButton.Create(self) do
  2.   Parent := self;
  3.   Left := 8;
  4.   // etc

This is only good if you are absolutely sure that you'll never have to access the created button at runtime later. In all other cases use a proper variable name. The compiler checks if the variable name is unique. It will not build a binary if it is not.

If you use the Name property of components then uniqueness is checked at runtime. What about if your user experiences a crash after opening the rarely-tested 109th form out of 300 in which you had once assumed that the component name is unique?

Just my opinion, not religion...

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Which button is clicked?
« Reply #21 on: June 06, 2017, 12:44:35 pm »
You are talking of "lazy-typer" code such as
Not exactly.

What happens when you have your "array of components", relying on the particular order and sort that array because you need to ?

Quote
In all other cases use a proper variable name
Which, imho, defeats the purpose of creating components dynamically.

To make it perhaps more 'alive', have a look at the snippet posted here, to give you a better idea of what i meant (it is just one snippet to illustrate).

Quote
Just my opinion, not religion...
Which applies for me as well

wp

  • Hero Member
  • *****
  • Posts: 13560
Re: Which button is clicked?
« Reply #22 on: June 06, 2017, 01:52:13 pm »
To make it perhaps more 'alive', have a look at the snippet posted here, to give you a better idea of what i meant (it is just one snippet to illustrate).
I do not want to delve into this topic any deeper, but this example is too good to demonstrate why the Name of a component is NOT needed. The user is calling FindComponent for seeking the PopupMenu of the ActiveControl among the components on the current form by using the PopupMenu.Name. But seeking is not necessary at all, because every TControl has a PopupMenu, i.e. ActiveControl.PopupMenu should work immediately.
« Last Edit: June 06, 2017, 01:55:02 pm by wp »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Which button is clicked?
« Reply #23 on: June 06, 2017, 03:03:16 pm »
I do not want to delve into this topic any deeper,
That was rather obvious from the start. Trust me, i did not even wanted to post my previous reply, for obvious reasons.

Quote
but this example is too good to ...
And your reaction to good to show how your brain seems locked to existing patterns. My bad. I was even able to anticipate on that as i suspected that you would only look at what you wanted to see ... and you did indeed.

Nothing wrong with that, but, as long as you misinform:
Quote
If you use the Name property of components then uniqueness is checked at runtime. What about if your user experiences a crash after opening the rarely-tested 109th form out of 300 in which you had once assumed that the component name is unique?
I feel obligated to correct as the above is complete and utter nonsense. With that remark you literally state that you are unable to count.  :o

Your response so far led me to believe that you seem to have very weirds idea's on (real) dynamic creation of GUI's. Sorry for having tried to inform that there are also other methods.

Bart

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
Re: Which button is clicked?
« Reply #24 on: June 06, 2017, 07:13:23 pm »
Can everybody (to whom this applies) moderate their tone please?

Bart

Ondrej Pokorny

  • Full Member
  • ***
  • Posts: 220
Re: Which button is clicked?
« Reply #25 on: June 06, 2017, 07:34:40 pm »
To make it perhaps more 'alive', have a look at the snippet posted here, to give you a better idea of what i meant (it is just one snippet to illustrate).

Holy moly! Only a pure genius can invent an expression like
Code: [Select]
(findcomponent(activecontrol.PopupMenu.Name) as tpopupmenu):D

sky_khan

  • Guest
Re: Which button is clicked?
« Reply #26 on: June 06, 2017, 07:51:53 pm »
I've met a junior programmer where I work long time ago. I've seen his genius code. He had wrote same expression for an array 30 times, one after another. Some may say he did this because he does not know how to use "for" but I believe he invented loop unrolling, by himself only! lol

P.S: Execution speed was not an issue.

 

TinyPortal © 2005-2018