B4J Question File Chooser code

AnnnaKiska

New Member
Hi. Please give me the code where there is a selection of files


1686560416655.png
 

Daestrum

Expert
Licensed User
Longtime User
I don't use B4XPages, but the only way I could get it to work was.

1, Make Mainform in Main Public ( ie Dim MainForm .. not Private Mainform ...)
2, Change the FileChooser owner to Main.MainForm ( filename = fc.ShowOpen(Main.mainform) )

There are probably better ways , but who knows.

Edit: @teddybear does :)
 
Last edited:
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Replace fc.ShowOpen(Root) with fc.ShowOpen(MainForm).

Always use code tags (</> on the menu bar) to show your code.

B4X:
Sub Button1_Click
    Dim fc As FileChooser
    Dim filename As String
    fc.Initialize
    fc.InitialDirectory = File.DirApp
    filename = fc.ShowOpen(MainForm)
    Log(filename)
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I think they did the same as I did , create a new B4XPages app and put the fc code in the button click event. MainForm doesn't exist in that module, so you have to directly reference it 'Main.MainForm'.
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Hi. Please give me the code where there is a selection of files
1.As Brian Dean said, use code tage to quote your code
2.For B4xPage you should use B4XPages.GetNativeParent(Me) instead of root
B4X:
fileNotes = fc.ShowOpen(B4XPages.GetNativeParent(Me))
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Longtime User
Or if you want to open a certain file type:

global variables:
Sub Class_Globals
    Public FilePath As String' Full directory and filename
    Public CurrentfileName As String = "*.txt"        ' --- Wanted file type
    Public CurrentDirectory As String = File.DirApp    ' --- Or default file location between ""
End Sub

and select a file and remember path and filename for later:

B4X:
    Dim fc As FileChooser
    fc.Initialize
    fc.InitialFileName = CurrentfileName
    fc.InitialDirectory = CurrentDirectory
    FilePath = fc.ShowOpen(B4XPages.GetNativeParent(Me))
    If File.Exists(FilePath,"") Then
        Log(FilePath)
        CurrentDirectory = File.GetFileParent(FilePath)
        CurrentfileName  = File.GetName(FilePath)
        Else
'        --- process empty chose
    End If   
'    --- Get the size of the selected file
    Log(lstJavaFile.Items.Size)
 
Upvote 0
Top