[Gambas-user] Make Datacontrol act like textbox with password set to true

Tobias Boege taboege at ...626...
Mon Nov 3 22:24:44 CET 2014


On Mon, 03 Nov 2014, Martin McGlensey wrote:
> Hello,
> 
>  
> 
> I need to have users enter a password into a data control. The password
> should be hidden from view as the user types. This can be done in a textbox
> by setting the password property to true. Nice but, there is no password
> property for a data control. An old post by Bennoit suggests  the following:
> 
>  
> 
> Dim hTextbox As TextBox = MyDataControl.Children[0]
> 
> hTextBox.Password = True
> 
>  
> 
> The first statement returns error - "Wanted textbox got datacontrol"   Is it
> =Datasource.Children or is it =datacontrol like Datacontrol4? 
> 
>  
> 
> Maybe you guys could point out my mistake or suggest a better way. I'm using
> Gambas 3.6.2 [...]
> 

Maybe I'll borrow your time machine some day ;-)

About your problem: I have just tested this code and it worked here:

--8<------------------------------------------------------------------------
'' Set the Password property of all TextBoxes contained in hDataControl
'' (normally just one) to bVal.
Private Sub SetPassword(hDataControl As DataControl, bVal As Boolean)
  Dim aTextboxes As TextBox[] = Discover(hDataControl, "TextBox")
  Dim hTextBox As TextBox

  For Each hTextBox In aTextboxes
    hTextBox.Password = bVal
  Next
End

'' Return all children of hContainer (recursively) which are of class sClass.
Private Function Discover(hContainer As Container, sClass As String) As Control[]
  Dim hControl As Control
  Dim aRes As New Control[]

  For Each hControl In hContainer.Children
    If hControl Is Container Then aRes.Insert(Discover(hControl, sClass))
    If Object.Type(hControl) = sClass Then aRes.Add(hControl)
  Next
  Return aRes
End
--8<------------------------------------------------------------------------

This is a plain generalisation of Benoit's suggestion. It doesn't assume
that the TextBox is at some specific index in the children array and it
doesn't assume that there is only one TextBox. We set the Password property
of *every* TextBox, just to be sure -- but there should really be just one.

It will work until something *seriously* changes in the way DataControl
is implemented or container children are managed. But it's hard to imagine
that such a big change will ever happen.

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list