Detect label and change it

Post

Posted
Rating:
#1 (In Topic #906)
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 I have a form where there are several textboxes and next to each textbox I have put a label to label each textbox.

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. :)
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 First, I really like this question.
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?

Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 Sorry, I found you extremely funny. If it wasn't your intention, forgive me from the heart.

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. :)
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’
No there is nothing automatic, stuff like that you just gotta code yourself.
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)

  1.  
  2. Public Sub CheckTextFields(TextBoxes As TextBox[]) As TextBox[]
  3.  
  4.   Dim CheckList As TextBox[] = TextBoxes.Copy()
  5.   Dim lbl As Object
  6.  
  7.   For c As Integer = CheckList.Max DownTo 0
  8.     lbl = CheckList[c].Parent.Children[0]
  9.     If Object.Type(lbl) <> "Label" Then lbl = CheckList[c].Parent.Children[1]
  10.     If Object.Type(lbl) = "Label" Then
  11.       If Not CheckList[c].Text Then
  12.         lbl.Foreground = Color.Red
  13.       Else
  14.         lbl.Foreground = Color.Default
  15.         CheckList.Remove(c)
  16.       Endif
  17.  
  18.     Else
  19.       Print "Error finding Label for " & CheckList[c].Name
  20.     Endif
  21.   Next
  22.  
  23.   Return CheckList
  24.  
  25.  
  26.  

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…

Code (gambas)

  1.  
  2. Public Sub SubmitForm()
  3.  
  4.   Dim UnfilledTextBoxes As TextBox[] = CheckTextFields([TextBox1, TextBox2, TextBox3])
  5.  
  6.   If UnfilledTextBoxes.Count Then
  7.     Message.Error(UnfilledTextBoxes.Count & " items need to be filled")
  8.     Return
  9.  
  10.  
  11.  
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
vuott is in the usergroup ‘Regular’

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.
Hello,
do you mean something like this?  :|

Code (gambas)

  1.  
  2.  
  3. Public Sub Form_Open()
  4.  
  5.   Dim tb As TextBox
  6.   Dim lb As Label
  7.  
  8.   For c As Byte = 1 To 4
  9.     With tb = New TextBox(Me) As "TBox"
  10.       .X = 100
  11.       .Y = 50 + (c * 50)
  12.       .W = 100
  13.       .H = 40
  14.       .Alignment = Align.Right
  15.       .Text = CStr(c)
  16.     End With
  17.     ttbb.Push(tb)
  18.     With lb = New Label(Me)
  19.       .X = tb.X + tb.W + 30
  20.       .Y = tb.Y
  21.       .w = tb.W
  22.       .H = tb.H
  23.       .Background = Color.SoftYellow
  24.       .Alignment = Align.Right
  25.       .Text = tb.Text
  26.     End With
  27.     llbb.Push(lb)
  28.   Next
  29.  
  30.  
  31. Public Sub TBox_Change()
  32.  
  33.   If ttbb.Count < 4 Then Return
  34.  
  35.   Dim n As Byte = ttbb.Find(Last)
  36.  
  37.   If Last.Text = Null Then
  38.     llbb[n].Text = Null
  39.     llbb[n].Background = Color.Red
  40.   Else
  41.     llbb[n].Text = ttbb[n].Text
  42.     llbb[n].Background = Color.SoftYellow
  43.  

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#6
Guru
BruceSteers is in the usergroup ‘Guru’
Alternatively you can customize the TextBox.class to find it's corresponding Label and have a validating function.

Ie.. save this file in your .src folder as TextBox.class

Code (gambas)

  1. ' Gambas class file
  2.  
  3.  
  4. Public Sub Validate() As Boolean
  5.  
  6.   hLabel = Me.Parent.Children[0]
  7.   If Object.Type(hLabel) <> "Label" Then
  8.     hLabel = Me.Parent.Children[1]
  9.     If Object.Type(hLabel) <> "Label" Then Error.Raise("No label")
  10.  
  11.   If Not Me.Text Then
  12.     hLabel.Foreground = Color.Red
  13.     hLabel.Refresh
  14.     Return True
  15.   Else If hLabel.Foreground <> Color.Default Then
  16.     hLabel.Foreground = Color.Default
  17.     hLabel.Refresh
  18.  
  19.  

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…

Code (gambas)

  1. Public Sub btnSubmitForm_Click()
  2.  
  3. Dim Missing As Integer
  4.  
  5. For Each tbox As TextBox In [TextBox1, TextBox2, TextBox3]
  6.   If tbox.Validate() Then
  7.     Print tbox.Label.Text & " needs to be filled"
  8.     Inc Missing
  9.  
  10. If Missing Then Print Missing & " missing fields"
  11.  
  12.  
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Guru
cogier is in the usergroup ‘Guru’
My solution to this issue is to use a timer.

This is the form I created: -
<IMG src="https://www.cogier.com/gambas/DataNeeded.png"> </IMG>

This is all the code: -

Code (gambas)

  1. Public Sub Timer1_Timer()
  2.  
  3.   Dim TBs As TextBox[] = [TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10]
  4.   Dim Labels As Label[] = [Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9, Label10]
  5.   Dim iLoop As Integer
  6.  
  7.   For iLoop = 0 To TBs.Max
  8.     If Trim(TBs[iLoop].Text) = "" Then
  9.       Labels[iLoop].Background = Color.Yellow
  10.       Labels[iLoop].Foreground = Color.Red
  11.       Labels[iLoop].Font.Bold = True
  12.     Else
  13.       Labels[iLoop].Background = Color.Default
  14.       Labels[iLoop].Foreground = Color.Default
  15.       Labels[iLoop].Font.Bold = False
  16.     Endif
  17.   Next
  18.  

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 Nup. Parent tracing can't work if the parent is the form (or some container in the form that contains multiple controls). Depending on the form layout or user manipulation of the form height and width the internal arrangement calls will make the ordinal positions of the label and the textbox more or less indeterminate. The only way I know of achieving this is to enclose both controls in some composite container control, like a HBox for instance. The thing that knows both the label and the textbox is that container. The container, when the TextBox.Text is set or altered can detect some events and then alter the Label format.
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.

Online now: No Back to the top

Post

Posted
Rating:
#9
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 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.

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘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 :)
Online now: No Back to the top

Post

Posted
Rating:
#11
Avatar
Guru
cogier is in the usergroup ‘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.
Online now: No Back to the top

Post

Posted
Rating:
#12
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
Hello

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.

Attachment

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#13
Avatar
Guru
cogier is in the usergroup ‘Guru’
I still think this solution is a lot easier and uses less code. This is your program modified.

Attachment

Tip: - To make an Archive to post on the Forum, in Gambas: -

<IMG src="https://www.cogier.com/gambas/Archive.png"> </IMG>
Online now: No Back to the top

Post

Posted
Rating:
#14
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 Thanks cogier, I'll do that next time. Greetings.

For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you. :)
Online now: No Back to the top

Post

Posted
Rating:
#15
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 One thing I want to point out.

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. :)
Online now: No Back to the top

Post

Posted
Rating:
#16
Regular
vuott is in the usergroup ‘Regular’
In the initial message you did NOT mention coordinates. Saying "without associating it" means absolutely nothing or is otherwise unclear.
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>
Online now: No Back to the top

Post

Posted
Rating:
#17
Enthusiast
gambafeliz is in the usergroup ‘Enthusiast’
 Hello my dear Vuott.

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. :)
Online now: No Back to the top
1 guest and 0 members have just viewed this.