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.
Detect label and change it
- gambafeliz
- Posts: 141
- Joined: Friday 2nd September 2022 7:50pm
- Location: I live in the same city as Picasso
Detect label and change it
For your misfortunes I am Spanish and I only know Spanish, please, be patient with me, Thank you.
Re: Detect label and change it
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?
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?
Have you ever noticed that software is never advertised using the adjective "spreadable".
- gambafeliz
- Posts: 141
- Joined: Friday 2nd September 2022 7:50pm
- Location: I live in the same city as Picasso
Re: Detect label and change it
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.
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.
- BruceSteers
- Posts: 1572
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Detect label and change it
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..
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...
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..
Public Sub CheckTextFields(TextBoxes As TextBox[]) As TextBox[]
Dim CheckList As TextBox[] = TextBoxes.Copy()
Dim lbl As Object
For c As Integer = CheckList.Max DownTo 0
lbl = CheckList[c].Parent.Children[0]
If Object.Type(lbl) <> "Label" Then lbl = CheckList[c].Parent.Children[1]
If Object.Type(lbl) = "Label" Then
If Not CheckList[c].Text Then
lbl.Foreground = Color.Red
Else
lbl.Foreground = Color.Default
CheckList.Remove(c)
Endif
Else
Print "Error finding Label for " & CheckList[c].Name
Endif
Next
Return CheckList
End
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...
Public Sub SubmitForm()
Dim UnfilledTextBoxes As TextBox[] = CheckTextFields([TextBox1, TextBox2, TextBox3])
If UnfilledTextBoxes.Count Then
Message.Error(UnfilledTextBoxes.Count & " items need to be filled")
Return
Endif
End
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: Detect label and change it
Hello,gambafeliz wrote: ↑Friday 30th September 2022 9:46amI 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?
Private ttbb As New TextBox[]
Private llbb As New Label[]
Public Sub Form_Open()
Dim tb As TextBox
Dim lb As Label
For c As Byte = 1 To 4
With tb = New TextBox(Me) As "TBox"
.X = 100
.Y = 50 + (c * 50)
.W = 100
.H = 40
.Alignment = Align.Right
.Text = CStr(c)
End With
ttbb.Push(tb)
With lb = New Label(Me)
.X = tb.X + tb.W + 30
.Y = tb.Y
.w = tb.W
.H = tb.H
.Background = Color.SoftYellow
.Alignment = Align.Right
.Text = tb.Text
End With
llbb.Push(lb)
Next
End
Public Sub TBox_Change()
If ttbb.Count < 4 Then Return
Dim n As Byte = ttbb.Find(Last)
If Last.Text = Null Then
llbb[n].Text = Null
llbb[n].Background = Color.Red
Else
llbb[n].Text = ttbb[n].Text
llbb[n].Background = Color.SoftYellow
Endif
End
Europaeus sum !
Amare memorentes atque deflentes ad mortem silenter labimur.
Amare memorentes atque deflentes ad mortem silenter labimur.
- BruceSteers
- Posts: 1572
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Detect label and change it
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
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...
Ie.. save this file in your .src folder as TextBox.class
' Gambas class file
Export
Property Read Label As Label Use hLabel
Public Sub Validate() As Boolean
hLabel = Me.Parent.Children[0]
If Object.Type(hLabel) <> "Label" Then
hLabel = Me.Parent.Children[1]
If Object.Type(hLabel) <> "Label" Then Error.Raise("No label")
Endif
If Not Me.Text Then
hLabel.Foreground = Color.Red
hLabel.Refresh
Return True
Else If hLabel.Foreground <> Color.Default Then
hLabel.Foreground = Color.Default
hLabel.Refresh
Endif
End
That will give all your text boxes a Validate() function and a .Label propertyThe 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...
Public Sub btnSubmitForm_Click()
Dim Missing As Integer
For Each tbox As TextBox In [TextBox1, TextBox2, TextBox3]
If tbox.Validate() Then
Print tbox.Label.Text & " needs to be filled"
Inc Missing
Endif
Next
If Missing Then Print Missing & " missing fields"
End
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- cogier
- Site Admin
- Posts: 1125
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Detect label and change it
My solution to this issue is to use a timer.
This is the form I created: -
This is all the code: -
This is the form I created: -
This is all the code: -
Public Sub Timer1_Timer()
Dim TBs As TextBox[] = [TextBox1, TextBox2, TextBox3, TextBox4, TextBox5, TextBox6, TextBox7, TextBox8, TextBox9, TextBox10]
Dim Labels As Label[] = [Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9, Label10]
Dim iLoop As Integer
For iLoop = 0 To TBs.Max
If Trim(TBs[iLoop].Text) = "" Then
Labels[iLoop].Background = Color.Yellow
Labels[iLoop].Foreground = Color.Red
Labels[iLoop].Font.Bold = True
Else
Labels[iLoop].Background = Color.Default
Labels[iLoop].Foreground = Color.Default
Labels[iLoop].Font.Bold = False
Endif
Next
End
Re: Detect label and change it
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.
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.
Have you ever noticed that software is never advertised using the adjective "spreadable".
- gambafeliz
- Posts: 141
- Joined: Friday 2nd September 2022 7:50pm
- Location: I live in the same city as Picasso
Re: Detect label and change it
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.
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.
- BruceSteers
- Posts: 1572
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Detect label and change it
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.
Best of luck and have fun with itgambafeliz wrote: ↑Saturday 1st October 2022 3:48pm 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.
If at first you don't succeed , try doing something differently.
BruceS
BruceS