B4J Question is there any way to assign and ID to an element on the designer?

omarruben

Active Member
Licensed User
Longtime User
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    MainForm.Stylesheets.Add( File.GetUri(File.DirAssets, "Buttons.css") )
    Button1.Id="mybutton"  ' < -------------------------- done in code , could be done on the designer? for css purposes
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
End Sub
 

Mariano Ismael Castro

Active Member
Licensed User
B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    MainForm.Stylesheets.Add( File.GetUri(File.DirAssets, "Buttons.css") )
    Button1.Id="mybutton"  ' < -------------------------- done in code , could be done on the designer? for css purposes
End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello World!", "B4X")
End Sub
Tag properties.
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
Tag is a different attribute to the view id.

is there any way to assign and ID to an element on the designer?

I don't think the designer allows setting the view ID. I tend to just have a separate sub called from AppStart to set all the view IDs that I need.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The view name is already unique like an id.
I don't think you can use that in a css #selector, but you can use the control id when using a css file (but as usual I could be wrong).
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can use a Designer Script Extension. Here are examples to set the Id and Styleclass(es)
B4X:
'Parameters: Id As String, 1 View
'Designer Script : {Class}.AddCSSId("button-sl",Button1)
Public Sub AddCSSId(DesignerArgs As DesignerArgs)
    If DesignerArgs.FirstRun Then
        Dim Id As String = DesignerArgs.Arguments.Get(0)
        Dim V As Node = DesignerArgs.GetViewFromArgs(1)
        Id = Id.Trim
        V.id = Id
    End If
End Sub

'Parameters: ClassNames As String (Comma delimited), 1 or more Views comma delimited
'Designer Script : {Class}.AddStyleClass("button-sl",Button1,Button2,Button3)
Public Sub AddStyleClass(DesignerArgs As DesignerArgs)
    If DesignerArgs.FirstRun Then
        Dim ClassNames() As String = Regex.Split(",",DesignerArgs.Arguments.Get(0))
        For i = 1 To DesignerArgs.Arguments.Size - 1
            Dim V As Node = DesignerArgs.GetViewFromArgs(i)
            For Each ClassName As String In ClassNames
                ClassName = ClassName.Trim
                If V.StyleClasses.IndexOf(ClassName) = -1 Then
                    V.StyleClasses.Add(ClassName)
                End If
            Next
        Next
    End If
End Sub

Where {Class} is whichever class you put the subs in.
 
Upvote 1
Top