Refering to a specific form from a custom function declared in a module.
Hi! I'm doing a custom function that will change the text on the menuItem on the frmMDI form. Hi have this:
VB Code:
Module _mod
Public parent As frmMDI
Function loadLanguageString(ByVal lang)
' create a connection string
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Documents and Settings\jeanca.DSD-LAN\Desktop\Signs PRO\Signs PRO\signspro.mdb"
Dim conn As OleDbConnection = New OleDbConnection
conn.ConnectionString = strConn
' set the string table to load
Dim tblString As String
If lang = "FRENCH" Then
tblString = "tbl_french_string"
ElseIf lang = "ENGLISH" Then
tblString = "tbl_english_string"
End If
' create a data adapter
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from " & tblString, conn)
' create a new dataset
Dim ds As DataSet = New DataSet
' fill dataset
da.Fill(ds, "string")
' write dataset contents to an xml file by calling WriteXml method
[COLOR=Sienna]parent.MenuItem1.Text = ds.Tables("string").Rows(0).Item(2)[/COLOR]
End Function
but I get an error when I refer to parent at the last line: "Object reference not set to an instance of an object."
anyone can help? where should I declare the form so I can refer to them everywhere???
Re: Refering to a specific form from a custom function declared in a module.
Hi!
That caused by the varaible "parent" not instanciated
You must instanciate the "parent" form before refering to it
So in a sub Main you should add something like Parent = New frmMDI before calling loadLanguageString
Zak
Re: Refering to a specific form from a custom function declared in a module.
huh... like
dim parent = new frmMDI
of no "dim" ??? and where do I declare it? in the module or in the same sub load juste before I call the function??? and do I have to declare it in the module too???
bcause I wrote " Dim frmMain = New frmMDI" just before the function definition and now I have this "Public member 'MenuItem1' on type 'frmMDI' not found."
thank you dude!!!
Re: Refering to a specific form from a custom function declared in a module.
Quote:
Originally Posted by jcavard
huh... like
dim parent = new frmMDI
of no "dim" ??? and where do I declare it? in the module or in the same sub load juste before I call the function??? and do I have to declare it in the module too???
bcause I wrote " Dim frmMain = New frmMDI" just before the function definition and now I have this "Public member 'MenuItem1' on type 'frmMDI' not found."
thank you dude!!!
If the definition is in a Module and the variable is declared Public, the variable will be available to the entire project. so you may refer to it anywhere in then project.
But you may also just add New frmMDI
in the variable declaration
Like this
VB Code:
Module _Mod
Public parent As frmMDI [B]= New FrmMDI[/B]
'Blabla ...
and it should just work fine !!
PS.
Es tu francophone ?
Re: Refering to a specific form from a custom function declared in a module.
Merci! ca marche!!! oouais je suis francophone, moi tout tantot j'ai remarqué que tu venais de montréal... qc represente! heh! merci encore man!! en plus je fini ma journé a temps!!
autre chose un cooup parti... ma fonction nexecute pas ce que je lui demande cependant...
jai ca dans ma fonction, et le texte du menuitem et du texbox1 ne change pourtant pas ... mais quand je met le code direct dans mon load (sans me servir dune fonction, ca fonctionne parfaitement...)
VB Code:
Module _mod
Public main As frmMDI = New frmMDI
Function loadLanguageString(ByVal lang)
' create a connection string
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Documents and Settings\jeanca.DSD-LAN\Desktop\Signs PRO\Signs PRO\signspro.mdb"
Dim conn As OleDbConnection = New OleDbConnection
conn.ConnectionString = strConn
' set the string table to load
Dim tblString As String
If lang = "FRENCH" Then
tblString = "tbl_french_string"
ElseIf lang = "ENGLISH" Then
tblString = "tbl_english_string"
End If
' create a data adapter
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from tbl_french_string", conn)
' create a new dataset
Dim ds As DataSet = New DataSet
' fill dataset
da.Fill(ds, "string")
' write dataset contents to an xml file by calling WriteXml method
main.MenuItem1.Text = ds.Tables("string").Rows(1).Item(2)
'frmMain.MenuItem1.Text = ds.Tables("string").Select("STRING_MAIN_MENU_FILE")
main.textbox1.text = "kljhkljhkjh"
End Function
End Module
Re: Refering to a specific form from a custom function declared in a module.
OK mais à quel moment tu appel LoadLanguageString dans le load ?
J'ai eu un problème semblable justement cette semaine. je ne parvenais pas faire certain changement dans ma frmMain...
Parcontre une chose que tu pourrais faire, c'est passé ta form main en paramètre à la fonction LoadLanguageString.
Ti conseil ... je remarque que ta déclaration de function
Function loadLanguageString(ByVal lang) ne défini pas le type de variable à utilisé .... Très très très mauvaise habiture
tu devrais activé Option Explicit et même voir à aussi activer l'option strict, mais l'option strict c'est un peu plus compliqué ...
Bonne chance !!
Ps
Tu est de ou ?
Re: Refering to a specific form from a custom function declared in a module.
Merci beaucoup! Je suis de la beauce. Ma fonction je l'apelle à la premiere ligne du load de ma form. Cette fonction remplace tout les string dans la'application, pas seulement le text des menu, mais aussi le text des label, des bouton, de tout! sur toutes les forms. Alors j'aurais besoin de pouvoir la caller de partout et qu'elle effectue les changement sur tout les form.