Results 1 to 7 of 7

Thread: Refering to a specific form from a custom function declared in a module.

  1. #1

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    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:
    1. Module _mod
    2.     Public parent As frmMDI
    3.  
    4.  
    5.  
    6.     Function loadLanguageString(ByVal lang)
    7.  
    8.         ' create a connection string
    9.         Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    10.                                 "Data Source=C:\Documents and Settings\jeanca.DSD-LAN\Desktop\Signs PRO\Signs PRO\signspro.mdb"
    11.         Dim conn As OleDbConnection = New OleDbConnection
    12.  
    13.  
    14.         conn.ConnectionString = strConn
    15.  
    16.         ' set the string table to load
    17.         Dim tblString As String
    18.         If lang = "FRENCH" Then
    19.             tblString = "tbl_french_string"
    20.         ElseIf lang = "ENGLISH" Then
    21.             tblString = "tbl_english_string"
    22.         End If
    23.  
    24.         ' create a data adapter
    25.         Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from " & tblString, conn)
    26.  
    27.         ' create a new dataset
    28.         Dim ds As DataSet = New DataSet
    29.  
    30.         ' fill dataset
    31.         da.Fill(ds, "string")
    32.  
    33.  
    34.         ' write dataset contents to an xml file by calling WriteXml method
    35.  
    36.         [COLOR=Sienna]parent.MenuItem1.Text = ds.Tables("string").Rows(0).Item(2)[/COLOR]
    37.     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???

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    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

  3. #3

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    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!!!

  4. #4
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    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:
    1. Module _Mod
    2.     Public parent As frmMDI [B]= New FrmMDI[/B]
    3.     'Blabla ...


    and it should just work fine !!

    PS.
    Es tu francophone ?

  5. #5

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    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:
    1. Module _mod
    2.  
    3.     Public main As frmMDI = New frmMDI
    4.  
    5.  
    6.     Function loadLanguageString(ByVal lang)
    7.  
    8.         ' create a connection string
    9.         Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    10.                                 "Data Source=C:\Documents and Settings\jeanca.DSD-LAN\Desktop\Signs PRO\Signs PRO\signspro.mdb"
    11.         Dim conn As OleDbConnection = New OleDbConnection
    12.  
    13.  
    14.         conn.ConnectionString = strConn
    15.  
    16.         ' set the string table to load
    17.         Dim tblString As String
    18.         If lang = "FRENCH" Then
    19.             tblString = "tbl_french_string"
    20.         ElseIf lang = "ENGLISH" Then
    21.             tblString = "tbl_english_string"
    22.         End If
    23.  
    24.         ' create a data adapter
    25.         Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from tbl_french_string", conn)
    26.  
    27.         ' create a new dataset
    28.         Dim ds As DataSet = New DataSet
    29.  
    30.         ' fill dataset
    31.         da.Fill(ds, "string")
    32.  
    33.  
    34.         ' write dataset contents to an xml file by calling WriteXml method
    35.  
    36.         main.MenuItem1.Text = ds.Tables("string").Rows(1).Item(2)
    37.         'frmMain.MenuItem1.Text = ds.Tables("string").Select("STRING_MAIN_MENU_FILE")
    38.         main.textbox1.text = "kljhkljhkjh"
    39.  
    40.  
    41.     End Function
    42.  
    43.  
    44. End Module
    Last edited by jcavard; Jul 26th, 2005 at 03:45 PM.

  6. #6
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    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 ?

  7. #7

    Thread Starter
    Fanatic Member jcavard's Avatar
    Join Date
    Jul 2005
    Location
    Quebec, CANADA
    Posts
    534

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width