B4J Question <SOLVED. is it possible to anchor a choicebox based on mouse click?

Mikelgiles

Active Member
Licensed User
is it possible to anchor a choicebox or other views based on a mouse click or position? Or any other way to set top and left positions of a view based on a B4XTable cell? I would like to position a choiceBox in relation to the row and column that has been clicked. Or possibly use the mouse to move a view or pane on the screen.
 

Mikelgiles

Active Member
Licensed User
it would be not that difficult, look at the example attached.

just compile and click on the form.
Thank you Enrique. I really thought it would work but it doesnt trigger the form mouse clicked event because the B4XTable trapped it instead. I also tried B4XTable_mouseclicked and it allowed me to add it without errors but I never managed to trigger it so I am wondering if it is legitimate. I tried a few things inside the B4XTable_CellClicked sub hoping to find the current mouse position with no success. I am going to attempt to just calculate positions for the choicebox. Thanks again!!!
 

mangojack

Well-Known Member
Licensed User
is it possible to anchor a choicebox or other views based on a mouse click or position?
This basic example places a choice box on the clicked table cell.

It calculates the cell position based on row and Column index.

It certainly needs 'tuning' ... but it might be helpful as a Idea / guide



UpDate : ... An error happens when you click on the header first (column is sorted) .. then click another cell ?? ... see line # 54
 

Attachments

Last edited:

mangojack

Well-Known Member
Licensed User
I am wondering now if this example is any use now. If the columns are sorted it really throws a spanner in the works.

Anyways ... having another look at it now.
 

Mikelgiles

Active Member
Licensed User
This basic example places a choice box on the clicked table cell.

It calculates the cell position based on row and Column index.

It certainly needs 'tuning' ... but it might be helpful as a Idea / guide



UpDate : ... An error happens when you click on the header first (column is sorted) .. then click another cell ?? ... see line # 54
mangojack, I used part of what you posted and I thought that I had the choiceBox part working until I started testing. When I click on the column "cycle" it all works as needed but only on every other click. The 1st, 3rd, 5th, etc work as expected showing the choiceBox and allowing a selection and updating sqlite. The even clicks goes thru the cBox_SelectedIndexChanged() sub not allowing input and updates sqlite with a null. I am new to this stuff so I am sure that I am missing something but I cant figure out what'. Here is the code that is causing the problem.

B4X:
Sub B4XTableM_CellClicked (ColumnId As String, RowId As Long)
    Dim column As B4XTableColumn = B4XTableM.GetColumn(ColumnId)
    Dim value As String = B4XTableM.GetRow(RowId).Get(ColumnId)
    Dim visibleRowID As Int = (B4XTableM.VisibleRowIds.IndexOf(RowId))   
    ClickedRow=RowId
    ClickedCol=column.SQLID
    Select ColumnId
        Case "paydate" , "duedate"
            'will use a datepicker here
        Case "cycle", "method", "autopay"  
            Log("Case cycle   method   autopay")
            cBox.Items.Clear
            cBox.Top = visibleRowID * 10+100
            Select ColumnId
                Case "cycle"
                    Log("Case cycle")
                    cBox.Left=250
                    cBox.items.AddAll(Array As String("Monthly", "1st&15th", "Weekly","Special","None","2ndWed","3rdWed","Bi-Month"))
                Case  "method"
                    cBox.Left=250
                    cBox.items.AddAll(Array As String("Chk/DebitC", "Draft", "None","Online","OnlineCC","DraftCC"))
                Case  "autopay"
                    cBox.Left=320
                    cBox.items.AddAll(Array As String("None", "Fixed", "Min","Min+","Stmt"))
            End Select
            Log("cBox.Visible =True   &   cBox.ShowChoices")
            cBox.ShowChoices
            cBox.Visible=True
        Case Else
            If InputTemplate.IsInitialized = False Then InputTemplate.Initialize
            InputTemplate.Text = value
            InputTemplate.lblTitle.Text = ColumnId   'line 118 error   column.Id = 'description'      InputTemplate is all null's except for Text which is = 'AT&T'
            Wait For (Dialog.ShowTemplate(InputTemplate, "OK", "", "CANCEL")) Complete (Result As Int)
            If Result = xui.DialogResponse_Positive Then
                B4XTableM.sql1.ExecNonQuery2($"UPDATE data SET ${column.SQLID} = ? WHERE rowid = ?"$, Array As String(InputTemplate.Text, RowId))
                B4XTableM.Refresh
            End If

    End Select
End Sub
Sub cBox_SelectedIndexChanged(Index As Int, Value As Object)
    Log("cBox_SelectedIndexChanged(Index As Int, Value As Object)"  & "-" & Index & "-" & Value )
    Dim myquery As String = "UPDATE data SET " & ClickedCol & " = '" & Value & "' WHERE rowid = " & ClickedRow
    B4XTableM.sql1.ExecNonQuery(myquery)
    cBox.Visible=False
    Log("B4XTableM.Refresh")
    B4XTableM.Refresh
End Sub
 

mangojack

Well-Known Member
Licensed User
When I click on the column "cycle" it all works as needed but only on every other click.
At a glance your code looks OK .. unsure what might be the cause . (wondering if Ive opened a can of worms.. )

One thing I notice .. Is ClickedRow a Global Var , Maybe better to use visibleRowID ?? unsure at this time.

Are you able to upload the project ? .. File > Export as Zip



* On a side note .. Maybe have a look at Smart String Literals ...
allows you to get rid of all those quotes in your query strings and others (in my opinion easier to read as well) ie:

B4X:
Dim myquery As String = $"UPDATE data SET ${ClickedCol} = ${Value} WHERE ${rowid} = ${ClickedRow}"$

Log($"cBox_SelectedIndexChanged(Index As Int, Value As Object) - Index = ${Index}   Value = ${Value}"$)
 
Last edited:

Mikelgiles

Active Member
Licensed User
"mangojack, post: 688889, member: 28498"]At a glance your code looks OK .. unsure what might be the cause . (wondering if Ive opened a can of worms.. )
If there is a can of works I think it is my can as the problem I am having wasnt from your example LOL

One thing I notice .. Is ClickedRow a Global Var , Maybe better to use visibleRowID ?? unsure at this time.
I think ClickedRow was Global so I could use it in cBox_SelectedIndexChanged but I will check on that to be sure.

Are you able to upload the project ? .. File > Export as Zip
Yes but I need to clean it up first. I still have a lot of stuff remarked off for future use.



* On a side note .. Maybe have a look at Smart String Literals ...
allows you to get rid of all those quotes in your query strings and others (in my opinion easier to read as well) ie

B4X:
Dim myquery As String = $"UPDATE data SET ${ClickedCol} = ${Value} WHERE ${rowid} = ${ClickedRow}"$

Log($"cBox_SelectedIndexChanged(Index As Int, Value As Object) - Index = ${Index}   Value = ${Value}"$)
:
Smart strings is something I need to remember. They will make it easier to read. I started off with sql1.ExecNonQuery2 as I really liked that concept but never figured out how to make it work so I just switched to the old way as I knew how to make it work.
 

Mikelgiles

Active Member
Licensed User
If there is a can of works I think it is my can as the problem I am having wasnt from your example LOL


I think ClickedRow was Global so I could use it in cBox_SelectedIndexChanged but I will check on that to be sure.

Yes but I need to clean it up first. I still have a lot of stuff remarked off for future use.



Smart strings is something I need to remember. They will make it easier to read. I started off with sql1.ExecNonQuery2 as I really liked that concept but never figured out how to make it work so I just switched to the old way as I knew how to make it work.
At a glance your code looks OK .. unsure what might be the cause . (wondering if Ive opened a can of worms.. )

One thing I notice .. Is ClickedRow a Global Var , Maybe better to use visibleRowID ?? unsure at this time.

Are you able to upload the project ? .. File > Export as Zip



* On a side note .. Maybe have a look at Smart String Literals ...
allows you to get rid of all those quotes in your query strings and others (in my opinion easier to read as well) ie:

B4X:
Dim myquery As String = $"UPDATE data SET ${ClickedCol} = ${Value} WHERE ${rowid} = ${ClickedRow}"$

Log($"cBox_SelectedIndexChanged(Index As Int, Value As Object) - Index = ${Index}   Value = ${Value}"$)
At a glance your code looks OK .. unsure what might be the cause . (wondering if Ive opened a can of worms.. )

One thing I notice .. Is ClickedRow a Global Var , Maybe better to use visibleRowID ?? unsure at this time.

Are you able to upload the project ? .. File > Export as Zip



* On a side note .. Maybe have a look at Smart String Literals ...
allows you to get rid of all those quotes in your query strings and others (in my opinion easier to read as well) ie:

B4X:
Dim myquery As String = $"UPDATE data SET ${ClickedCol} = ${Value} WHERE ${rowid} = ${ClickedRow}"$

Log($"cBox_SelectedIndexChanged(Index As Int, Value As Object) - Index = ${Index}   Value = ${Value}"$)
 

Mikelgiles

Active Member
Licensed User
By no means a finished product but it does show the problem and I think all else works except when exiting the app.
When I click on the column "cycle" it all works as needed but only on every other click. The 1st, 3rd, 5th, etc work as expected showing the choiceBox and allowing a selection and updating sqlite. The even clicks goes thru the cBox_SelectedIndexChanged() sub not allowing input and updates sqlite with a null. I am new to this stuff so I am sure that I am missing something but I cant figure out what
 

Attachments

mangojack

Well-Known Member
Licensed User
It would appear that the issue was caused by clearing the ChoiceBox. This refired the SelectedIndexChange event (seem to recall this occurring with comboboxes in VB)

I'm also sure the bug / issue affected all three columns using the Choicebox .

See the fixed example .. I have commented a few lines for your attention . ('@@@)


There was an issue / error when trying to change a table query line to Smart String literal ... but will look at that later.

Edit: This might be of interest , regarding above Query issue.
https://www.b4x.com/android/forum/threads/b4xtable-db-query-error.110423/


Cheers
 

Attachments

Last edited:
Top