You are not logged in.

Dear visitor, welcome to Jabaco - Community. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

  • "indyglassman" started this thread

Posts: 10

Date of registration: May 9th 2014

  • Send private message

1

Monday, May 12th 2014, 2:23am

Call public function from Form_load - Not working

Here is the code. When I run this, the form will never display on my screen. If move the line from form_load to something like command_click() then it works fine. So it appears that calling a user created function from with in form_load does not work for me.

Quoted

(This is in form1)
Public Sub Form_Load()
label1 = setlabel
End Sub


(This is in Module1)
Public Function Setlabel() As String
Setlabel = "Label set"
End Function
What I am trying to do is to have a function that reads data from a file and use that data to set the text of labels which are on Form1.


Thank you in advance for taking the time to read this.
Running: Windows 7 Enterprise, Framework v 135

This post has been edited 2 times, last edit by "indyglassman" (May 12th 2014, 3:28am)


Dani

Intermediate

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

2

Monday, May 12th 2014, 8:10am

Hey there,

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
Option Explicit

Public Sub Form_Load()
   Label1.Caption = SetLabel()
End Sub

Public Function SetLabel() As String
   Dim fh As VBFileHandler   
   Set fh = FileSystem.Open("c:\test.txt")
   SetLabel = fh.readAll()
   fh.close
End Function



Dani

This post has been edited 1 times, last edit by "Dani" (May 12th 2014, 8:22am)


Dani

Intermediate

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

3

Monday, May 12th 2014, 8:39am

... using your Public Function in Module1:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'Public Form1 As New Form1

Public Sub main(ByJava args() As String)
   Dim Form1 As New Form1
   Dim myArgs() As String
   myArgs = args
   Form1.SetDefaultClose()
   Form1.Show()
End Sub

Public Function SetLabel() As String
   Dim fh As VBFileHandler   
   Set fh = FileSystem.Open("c:\test.txt")
   SetLabel = fh.readAll()
   fh.close
End Function


... I don't know about possible side effects though!

Dani

  • "indyglassman" started this thread

Posts: 10

Date of registration: May 9th 2014

  • Send private message

4

Monday, May 12th 2014, 2:43pm

Dani,

Your first suggestion does work, however, I put the code to read the file in a module because it needs to be accessible to multiple forms.

In your second suggestion I don't see where you suggest I call Setlabel() from. This is exactly what I'm trying to do but calling SetLabel() from form_load causes the main window to not display.

From what I can tell, it is not possible to call a user created function/sub that exists in a module from form_load.

  • "indyglassman" started this thread

Posts: 10

Date of registration: May 9th 2014

  • Send private message

5

Monday, May 12th 2014, 7:48pm

I've tried and tried and still believe it must not be possible to call a user created function from an included module inside of your form_load() event.

To accomplish what I am trying to do I went a different route.

I used the timer control. Upon form_load() I set the timer to 500ms. Then enable the timer and do what I want inside of the timer event. Then turn off the timer.

If there is a better way to accomplish this, let me know. Otherwise I will go with that for now.

Dani

Intermediate

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

6

Tuesday, May 13th 2014, 9:07pm

Hey there,

Quoted

In your second suggestion I don't see where you suggest I call Setlabel() from.


... well, go ahead and use the code I provided for Module1.
And in your Form1 use this:

Jabaco Source

1
2
3
Public Sub Form_Load()
   Label1.Caption = SetLabel()
End Sub


that is what you wanted right?

Quoted

This is exactly what I'm trying to do ...


And that is exacly what this solution is doing on my system!
Running this code on my system displays Form1 with Label1.Caption populated from test.txt.


Dani

  • "indyglassman" started this thread

Posts: 10

Date of registration: May 9th 2014

  • Send private message

7

Tuesday, May 13th 2014, 10:53pm

Dani,

I appreciate your suggestions. But, it does not work.

Here is my code to demonstrate exactly what does not work. Form 1 just has a label1 control, and module1 has a single function I created to return a string for setting label1.caption. Doing this does not work on my system. The java icon shows in my task bar, but the java window for form1 never displays and I can not switch too it.

No matter what I try, I can not call a function that is in a module from form_load.

The reason the code MUST be in a module is because it will be called from multiple forms, not just form1. Therefore I want to have the code be public and callable by all forms.

Form1

Source code

1
2
3
Public Sub Form_Load()
   SetLabel()
End Sub


Module1

Quoted

Public Form1 As New Form1

Public Sub main(ByJava args() As String)
Dim myArgs() As String
myArgs = args
Form1.SetDefaultClose()
Form1.Show()
End Sub

Public Function SetLabel() As String

SetLabel = "MyLabel"

End Function


Let me know if this works for you and the label gets set to "MyLabel" by calling the function in Module1 from the Form_load().

Thank you!! I really do appreciate your time looking at this.

Dani

Intermediate

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

8

Tuesday, May 13th 2014, 11:07pm

well, you are not doing what I proposed! ?(

ADD THIS TO YOUR MODULE1:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'Public Form1 As New Form1

Public Sub main(ByJava args() As String)
   Dim Form1 As New Form1
   Dim myArgs() As String
   myArgs = args
   Form1.SetDefaultClose()
   Form1.Show()
End Sub

Public Function SetLabel() As String
   Dim fh As VBFileHandler   
   Set fh = FileSystem.Open("c:\test.txt")
   SetLabel = fh.readAll()
   fh.close
End Function

notice the difference to your code??

AND THIS TO YOUR FORM1:

Jabaco Source

1
2
3
Public Sub Form_Load()
   SetLabel()
End Sub


It shoul work! ;)


Dani

  • "indyglassman" started this thread

Posts: 10

Date of registration: May 9th 2014

  • Send private message

9

Tuesday, May 13th 2014, 11:26pm

Dani,

You posted:

Source code

1
2
3
Public Sub Form_Load()
   SetLabel()
End Sub

This does nothing because the SetLabel() is a function that returns a value. But the code doesn't tell it where to put that value


However, after looking over the differences again I found the one difference that makes this work. You have "Public Form1 as New Form1" inside the sub Main to "Dim Form1 as New Form1"


I didn't notice that change because I saw that Jabaco automatically created the sub main and the reference to form1, so I assumed it was okay as it was.


So, now this works with the change you suggested. You mentioned you don't know the effects. Do you think this may cause issues for me?

Thanks again!

Dani

Intermediate

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

10

Tuesday, May 13th 2014, 11:36pm

Quoted

This does nothing because the SetLabel() is a function that returns a value. But the code doesn't tell it where to put that value


you are right, I took that from your post of course that does not work!
Look at the Form1 code I posted further up...


Quoted

So, now this works with the change you suggested. You mentioned you don't know the effects. Do you think this may cause issues for me?


That is exactly why I mentioned it, because I do not know!



Dani

Rate this thread
WoltLab Burning Board