Detect label and change it
Posted
#1
(In Topic #906)
Enthusiast

As data that I give you is that I have not associated or linked in any way these labels with their respective textbox.
That said, I'll tell you what I want to do and ask if someone knows how to solve it.
I want that when a specific textbox is left empty, the label that is next to it describing it, changes color to red, for example.
Someone tell me how without associating it or is it better to link it, for example, in this way:
label1 with textbox1
Or is it possible to detect the label closest to the textbox that I want to control.
Thanks.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Regular

But it's really a hard one.
And there are some "interesting" OO questions regarding it.
As far as I can tell, you want some control, specifically a Label, to respond to the display value of another control, specifically a textbox, without any knowledge of that control other than it is "nearby".
Hmm, or in other words, hmmmmmm.
Ok, well. Lets take the scenario that the textbox.text contains the string "Fred", then the user selects all the text and deletes it. At this point suddenly nothing happens. Then the user somehow causes the textbox to lose focus. Then textbox_change happens. Now this could be a good thing if you have a handler for textbox_change that tells the label to change its colors.
But I dont think that is what you are really after. For example if the textbox.text is "" when the form opens the same thing should happen.
I'll cut this meandering short. What you want is control X (the label) to have an implicit association with another control Y (the textbox) such that events of the implicitly associated control are detected and handled by the X control. Well that aint gunna happen unless the implicit association is replaced with an explicit one. Further if you try to implicitly associate the textbox control Y with another control, the label X, then that is a prescription for disaster code.
There is a way to achieve this by creating custom controls or specifically what I call custom composite controls. In your case it would consist of a Hbox containing a label and a textbox. Try them, they are good things. All the skills are in the help or at BruceS handiwork.
hth
t'other Bruce
P.S. Wouldn't the textbox thingo property that shows a null value as some string be more easy?
Posted
Enthusiast

Apart from how funny your conversation is. Look, I understand that there are simple ways to associate the TextBox with the Label and that's it. But since I'm a novice I made a very complex form and I forgot this detail.
Now for idleness I said what if someone does magic and solves it without redoing the code. That's really bad, I apologize.
Anyway what you propose I did not understand very well, you know my problem to understand your language, maybe a minimal example with hbox, but it does not matter. Thanks.
My problem is that sometimes there are textboxes that cannot be left blank and there is a final button on the form to record that when it goes to record everything fails because of this damn blank textbox.
Well as I see that what I have proposed is very crazy because I am going to work associating label with Textbox and I end up with the problem. I thought there was something to detect controls around a Textbox or something.
Thank you and a thousand apologies, ah, I liked talking to you. Greetings.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Guru

A function to check would be pretty simple as usually that situation involves a bunch of HBoxes all containing one label and 1 textbox so you can use the .Parent..
Code (gambas)
- lbl = CheckList[c].Parent.Children[0]
- lbl.Foreground = Color.Red
- CheckList.Remove(c)
- Return CheckList
With that you can provide a list of TextBoxes and it will check the container and get the label with it (it checks child[0] and child[1])
it will work for label either to the left or right of the TextBox and providing the container (probably a HBox) only has 2 items in it, a label and a textbox
If textbox has text the item is removed from the checklist then it returns the TextBox[] items that still need to be filled and will set their label text to red.
Eg…
Posted
Regular

Hello,gambafeliz said
I want that when a specific textbox is left empty, the label that is next to it describing it, changes color to red, for example.
do you mean something like this?
Code (gambas)
- .X = 100
- .Y = 50 + (c * 50)
- .W = 100
- .H = 40
- ttbb.Push(tb)
- .X = tb.X + tb.W + 30
- .Y = tb.Y
- .w = tb.W
- .H = tb.H
- .Background = Color.SoftYellow
- .Text = tb.Text
- llbb.Push(lb)
- llbb[n].Background = Color.Red
- llbb[n].Text = ttbb[n].Text
- llbb[n].Background = Color.SoftYellow
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Guru

Ie.. save this file in your .src folder as TextBox.class
Code (gambas)
- ' Gambas class file
- hLabel.Foreground = Color.Red
- hLabel.Refresh
- hLabel.Refresh
That will give all your text boxes a Validate() function and a .Label property
The Validate() function finds the Parents label and sets the TextBox.Label property
So the following code will check TextBox1, TextBox2 and TextBox3 and set the label color accordingly…
Posted
Guru

This is the form I created: -
<IMG src="https://www.cogier.com/gambas/DataNeeded.png">
</IMG>This is all the code: -
Code (gambas)
- Labels[iLoop].Background = Color.Yellow
- Labels[iLoop].Foreground = Color.Red
Posted
Regular

As I (somewhat amusingly it appears) tried to say, the only non-disastriously way to achieve the desired result is to encapsulate the Label and the Textbox in a container by themselves so that the entire effect is handled within that container. Neither the label nor the textbox should try to know something about each other that they shouldn't.
Well, that's my opinion anyway.
b
p.s. cogier posted while I was typing. It's a good simple solution but not robust…. If the form is redesigned at some point in time then the two arrays will not necessarily be correct any longer. Believe me, I know this from sad experience.
Posted
Enthusiast

Of course I am seeing that you all have interesting reasoning.
Please give me some time to implement the most reasonable ones from your solutions.
And later I will explain which one and why I take x solution.
Thank you, you are good friends.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Guru

thatbruce said
Nup. Parent tracing can't work if the parent is the form (or some container in the form that contains multiple controls).
Quite an unlikely "if" though I think.
take for example Cogiers example , i bet each one of those textboxes with labels are in their own HBox containers. so it would work for all of those.
I think in MOST cases if you have a text box with an accompanying Label defining what it is (which is what this post was about) they will be in their own HBox
And i was clear in the first example that having 1 label and 1 textbox in each HBox is how the Parent/Children way of "finding a control next to another" will work best.
gambafeliz said
Thanks, I'm really excited about your proposals.
Of course I am seeing that you all have interesting reasoning.
Please give me some time to implement the most reasonable ones from your solutions.
And later I will explain which one and why I take x solution.
Thank you, you are good friends.
Best of luck and have fun with it
Posted
Guru

p.s. cogier posted while I was typing. It's a good simple solution but not robust…. If the form is redesigned at some point in time then the two arrays will not necessarily be correct any longer. Believe me, I know this from sad experience.
I am struggling with this comment. My solution contains HBoxes with the Labels and TextBoxes, as mentioned by Bruce Steers. Can you post some code that demonstrates your points, perhaps using my Form design.
Posted
Enthusiast

Have a good Sunday, everyone.
Faced with so many good programmers, something simple has occurred to me. I hope they don't laugh and maybe you even like it.
Look at one more proposal, mine
Another thing, it is possible that it says something regarding the coordinate but a range can be made to correct it, only that I have not bothered much to show my code.
Greetings.
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Guru

Tip: - To make an Archive to post on the Forum, in Gambas: -
<IMG src="https://www.cogier.com/gambas/Archive.png">
</IMG>
Posted
Enthusiast

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Enthusiast

Vuott, your code is not valid for my case since it is prepared, that is, you make your labels be linked with some TextBox through the index of the array.
And the same goes for cogier, you bind the labels with the TextBoxes.
If you look at my code that I have uploaded, the label and the TextBox are not known at all, neither by array nor by anything, I track them by their Y coordinate.
And in my example for this publication that is what happens to me, I have a form with label and textbox that do not have any link. And what is intended is to solve it like this.
Eye, I greatly appreciate your help, of course, but I wanted to point out the code at this point and of course with the utmost respect.
Thank you
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Posted
Regular

it is impossible to understand what you want, maybe you didn't realize it.
Europaeus sum !
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Posted
Enthusiast

Everything is possible friend coming from me. But in this case I think that in the body of the publication I say it clearly. But otherwise I apologize. First of all your always good help and your person is for me much more important than discussing the body of the publication.
Best regards
Nota: I also think I remember that it also comes out in the conversation with …Bruce…
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
1 guest and 0 members have just viewed this.


