how to add textChanged event to textBox control at runtime

Gorazd

New Member
Licensed User
Hello!

I'd like to add event TextChanged to Object(Door library) object at runtime
like in following example:

B4X:
numberObj.New1(False)
numberObj.FromControl("number")
numberEvt.New1(numberObj.value,"TextChanged")

I tried with following code but it doesn't work:

B4X:
ctrl="number2"
AddObject (ctrl&"_obj", "Object")
Control(ctrl&"_obj").New1(False)
Control(ctrl&"_obj").FromControl(ctrl)
AddEvent(ctrl&"_obj.value",TextChanged, "NumberEvent_NewEvent")

My intent is to add event textChanged to some textBox control at runtime.

Can anybody help me, please

Best regards

Gorazd
 

derez

Expert
Licensed User
Longtime User
I'm not sure about the code, but this seems wrong:

ctrl & "_obj.value"

Try writing:

control( ctrl & "_obj" , "object").value

so it will be

AddEvent( control( ctrl & "_obj" , "object").value , TextChanged, "NumberEvent_NewEvent")
 

agraham

Expert
Licensed User
Longtime User
Like this
B4X:
Sub App_Start
   Form1.Show
   
   ' Specify the control and the Event
   Ctrl = "TextBox1"
   Event = "TextChanged"
   
   ' Names for the new Object and Event
   CtrlObj = Ctrl & "_obj"
   CtrlEvt = Ctrl & "_evt"
   
   ' Add a new object and associate it with the control
   AddObject (CtrlObj, "Object")
   Control(CtrlObj).New1(False)
   Control(CtrlObj).FromControl(ctrl)   

   ' Add a new Event object
   AddObject (CtrlEvt, "Event")
   
   ' Make the new Event a TextChanged event of the specified control
   ' This makes the TextChanged event of the control call the NewEvent event handler of the Event Object
   Control(CtrlEvt).New1(Control(CtrlObj).value, Event)
   
   ' Now make the NewEvent handler of the Event object call our event Sub.
   AddEvent(CtrlEvt, NewEvent, "Some_NewEvent")   

End Sub

Sub Some_NewEvent
   Msgbox("Some_NewEvent!")
End Sub

EDIT :- If you want to optimise compile the above you will need to edit the Control(...) statements to include the object types, Object or Event as appropriate.
 
Last edited:

Gorazd

New Member
Licensed User
Thank you for your help.
Now the code compiles and executes correctly.
I missed two steps and misunderstood AddEvent command.
Guess I miss some basic understanding of objects and events.:)

Best regards

Gorazd
 

HARRY

Active Member
Licensed User
Longtime User
Hallo Andrew,

The sample code you gave does not work anymore (with 6.90?) while compiling for the device. I understand that the keyword Control has been deprecated, but I can't get it working now.

I want to raise an event when a form is activated.

Could you help me (one more time)?

Harry
 

sitajony

Active Member
Licensed User
Hi, Hum TextChanged it's the same think that:
B4X:
value=""

Sub TextBox1_GotFocus
  value=TextBox1.Text
end sub

Sub TextBox1_LostFocus
  if not(value=TextBox1.Text) Then
    'Do something...
  End If
End Sub

Tell me if I'm Out Subject ;)
 

agraham

Expert
Licensed User
Longtime User
Hi, Hum TextChanged it's the same think that: ... Tell me if I'm Out Subject ;)
You are "Out Subject"! :). The TextChanged event is raised every time the contents of the Text property change - not on LostFocus.

Here's the previous code amended for v6.90 new syntax.
B4X:
Sub App_Start
   Form1.Show
   
   ' Specify the control and the Event
   Ctrl = "TextBox1"
   ChangedEvent = "TextChanged"
   
   ' Names for the new Object and Event
   CtrlObj = Ctrl & "_obj"
   CtrlEvt = Ctrl & "_evt"
   
   ' Add a new object and associate it with the control
   AddObject (CtrlObj, "Object")
   Object(CtrlObj).New1(False)
   Object(CtrlObj).FromControl(ctrl)   

   ' Add a new Event object
   AddObject (CtrlEvt, "Event")
   
   ' Make the new Event a TextChanged event of the specified control
   ' This makes the TextChanged event of the control call the NewEvent event handler of the Event Object
   Event(CtrlEvt).New1(Object(CtrlObj).value, ChangedEvent)
   
   ' Now make the NewEvent handler of the Event object call our event Sub.
   AddEvent(CtrlEvt, NewEvent, "Some_NewEvent")   

End Sub

Sub Some_NewEvent
   Msgbox("Some_NewEvent!")
End Sub
 
Top