find a control

Post your Gambas programming questions here.
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

find a control

Post by bill-lancaster »

I have a number of frames on a form which are generated dynamically, to recover information about a specific frame I do this:

Code: Select all

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?
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: find a control

Post by thatbruce »

Hard to tell without the context in which this code is executed, but maybe
  Dim hCtl as Object
  hCtl = Last
  DoThings
Have you ever noticed that software is never advertised using the adjective "spreadable".
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: find a control

Post by bill-lancaster »

OK, with this code:-

Code: Select all

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: Select all

Dim hCtrl As Control
  For Each hCtrl In Me.Controls
    If hCtrl.Name = "A2" Then
	Print hCtrl.Text
    Endif
  Next
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: find a control

Post by bill-lancaster »

Sorry, correction. This is my current method:-

Code: Select all


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
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: find a control

Post by vuott »

You can avoid two lines from your code:
Public Sub Button1_Click()

  Dim ob as Object
  
  For Each ob In Me.Controls
    If ob.Name = "A2" Then Print ob.Text
  Next
  
End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: find a control

Post by thatbruce »

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
Have you ever noticed that software is never advertised using the adjective "spreadable".
bill-lancaster
Posts: 195
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: find a control

Post by bill-lancaster »

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.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: find a control

Post by vuott »

bill-lancaster wrote: Wednesday 1st November 2023 9:14amThanks 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 !

Amare memorentes atque deflentes ad mortem silenter labimur.
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: find a control

Post by BruceSteers »

Should also be able to just use the form array

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

Dim hFrm As Frame = Me["A3"]
Print hFrm.Text
If at first you don't succeed , try doing something differently.
BruceS
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: find a control

Post by vuott »

:? ...you could also use an array of Frame:
Private ffrr As New Frame[5]

Public Sub Form_Open()

  For b As Byte = 0 To 4
    With ffrr[b] = New Frame(Me)
      .Name = "A" & b
      .X = (b * 60)
      .Y = 50
      .W = 50
      .H = 50
      .Text = "B" & b
    End With
  Next

End

Public Sub Button1_Click()

  Print ffrr[2].Text

End
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
Post Reply