find a control

Post

Posted
Rating:
#1 (In Topic #1122)
Regular
bill-lancaster is in the usergroup ‘Regular’
I have a number of frames on a form which are generated dynamically, to recover information about a specific frame I do this:

Code

Dim hCtrl As Control
  For Each hCtrl In Me.Controls
    If hCtrl.Name = Last.Name Then
      do things!
    Endif
  Next

Having set the frame's .Name to a unique value.

Is there a better way to do this?
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Hard to tell without the context in which this code is executed, but maybe

Code (gambas)

  1.   Dim hCtl as Object
  2.   hCtl = Last
  3.   DoThings
  4.  

Online now: No Back to the top

Post

Posted
Rating:
#3
Regular
bill-lancaster is in the usergroup ‘Regular’
OK, with this code:-

Code

Public Sub Form_Open()
Dim hFrame As Frame
Dim i As Integer
  For i = 0 To 4
    With hFrame = New Frame(Me)
      .Name = "A" & i
      .X = (i * 60)
      .Y = 50
      .W = 50
      .H = 50
      .Text = "B" & i
    End With
  Next
End

How best can I find the .text value of one of the frames?

At the moment I'm using:-

Code

Dim hCtrl As Control
  For Each hCtrl In Me.Controls
    If hCtrl.Name = "A2" Then
   Print hCtrl.Text
    Endif
  Next
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
bill-lancaster is in the usergroup ‘Regular’
Sorry, correction.  This is my current method:-

Code


Public Sub Button1_Click()
Dim hCtrl As Control
Dim hFrm As Frame
  For Each hCtrl In Me.Controls
    If hCtrl.Name = "A2" Then
      hFrm = hCtrl
      Print hFrm.Text
    Endif
  Next
End
Online now: No Back to the top

Post

Posted
Rating:
#5
Regular
vuott is in the usergroup ‘Regular’
You can avoid two lines from your code:

Code (gambas)

  1. Public Sub Button1_Click()
  2.  
  3.   Dim ob as Object
  4.  
  5.   For Each ob In Me.Controls
  6.     If ob.Name = "A2" Then Print ob.Text
  7.   Next
  8.  

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
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 Ah, yes. Obviously Last wont work, as Last is the button.
But, if you know the name of the control, e.g. "A2" then doesn't Me.A2 work?
or maybe Me.Controls["A2"]
I'm just poking about here.
b

Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
bill-lancaster is in the usergroup ‘Regular’
Thanks Vuott, this works and is much better than my original
Dim hFrm As Frame
  hFrm = Me.Controls["A3"]
  Print hFrm.Text

Thanks for your advice.
Online now: No Back to the top

Post

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

bill-lancaster said

Thanks Vuott, this works…
Actually, my code is more functional if you have Controls of various types on the Form that do not need to be searched based on one of their Properties.
If, on the other hand, you need to know the text, owned by the ".Text" Property of a specific Control, to which a value has also been assigned to the ".Name" Property, I think thatbruce's suggestion is better.

Europaeus sum !

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

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’
Should also be able to just use the form array

Me["A3"]
or
Form1["A3"]

Code (gambas)

  1. Dim hFrm As Frame = Me["A3"]
  2. Print hFrm.Text
  3.  
Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
vuott is in the usergroup ‘Regular’
:? …you could also use an array of Frame:

Code (gambas)

  1.  
  2. Public Sub Form_Open()
  3.  
  4.   For b As Byte = 0 To 4
  5.     With ffrr[b] = New Frame(Me)
  6.       .Name = "A" & b
  7.       .X = (b * 60)
  8.       .Y = 50
  9.       .W = 50
  10.       .H = 50
  11.       .Text = "B" & b
  12.     End With
  13.   Next
  14.  
  15.  
  16. Public Sub Button1_Click()
  17.  
  18.   Print ffrr[2].Text
  19.  

Europaeus sum !

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

Post

Posted
Rating:
#11
Avatar
Expert
Quincunxian is in the usergroup ‘Expert’
Here is a recursive routine to 'reset' a form - normally some sort of maintenance form to add/modify records.
It is called after the record is updated, resetting the input fields so new data can be entered. Aspects of the code may be useful if you want to do more than a simple search.

It is designed to work by passing a parent container such as a panel or frame, rather than a Form…
As I utilise single form maintenance rather than one form for displaying & selecting the records and another to do the maintenance so the controls to enter/update data exist on a panel/frame which I set visibility as required.



Code (gambas)

  1. Public Sub FormResetControls(InControl As Container)
  2.  
  3.   Dim ControlElement As Control
  4.   Dim TmpInt As Integer
  5.   Dim TPanel As TabPanel
  6.   Dim TStrip As TabStrip
  7.   Dim EmptyString As Variant = ""
  8.  
  9.   For Each ControlElement In InControl.Children
  10.  
  11.     If ControlElement Is Frame Then FormResetControls(ControlElement) 'It may have children so do a recursive search
  12.     If ControlElement Is Panel Then FormResetControls(ControlElement) 'It may have children so do a recursive search
  13.  
  14.     If ControlElement Is TabPanel Then
  15.       TPanel = ControlElement
  16.       For TmpInt = 0 To TPanel.Count - 1
  17.         FormResetControls(TPanel[TmpInt]) 'It may have children so do a recursive search
  18.       Next
  19.     Endif
  20.  
  21.     If ControlElement Is TabStrip Then
  22.       TStrip = ControlElement
  23.       For TmpInt = 0 To TStrip.Count - 1
  24.         FormResetControls(TStrip[TmpInt]) 'It may have children so do a recursive search
  25.       Next
  26.     Endif
  27.     If ControlElement Is TextBox Then Object.SetProperty(ControlElement, "Text", EmptyString)
  28.     If ControlElement Is TextBox Then Object.SetProperty(ControlElement, "Background", Color.White)
  29.     If ControlElement Is TextArea Then Object.SetProperty(ControlElement, "Text", EmptyString)
  30.     If ControlElement Is TextEditor Then Object.SetProperty(ControlElement, "Text", EmptyString)
  31.     If ControlElement Is ValueBox Then Object.SetProperty(ControlElement, "Value", Null)
  32.     If ControlElement Is CheckBox Then Object.SetProperty(ControlElement, "Value", 0)
  33.     If ControlElement Is DirBox Then Object.SetProperty(ControlElement, "Path", EmptyString)
  34.  
  35.     If ControlElement Is ComboBox Then
  36.       If Object.GetProperty(ControlElement, "Count") > 0 Then Object.SetProperty(ControlElement, "Index", 0)
  37.     Endif
  38.  
  39.     If ControlElement Is SpinBox Then
  40.       TmpInt = Object.GetProperty(ControlElement, "MinValue")
  41.       Object.SetProperty(ControlElement, "value", TmpInt)
  42.     Endif
  43.  
  44.   Next
  45.   Message(Error.Text & Gb.CrLf &  Error.Where)
  46.  
  47.  

Cheers - Quin.
I code therefore I am
Online now: No Back to the top

Post

Posted
Rating:
#12
Regular
bill-lancaster is in the usergroup ‘Regular’
 Thanks vuott, my project is a family tree where a frame represents a person so the number of frames is not known.
Regards
Online now: No Back to the top
1 guest and 0 members have just viewed this.