locked
Opening a module from a form RRS feed

  • Question

  • Hi I'm trying to make a text based game using a module but I want a start up screen and inventory screen in separate forms. I got the module part down, I just need help linking the forms and module
    • Edited by Chanoun Tuesday, December 11, 2012 6:21 PM Spelling
    Tuesday, December 11, 2012 6:20 PM

Answers

  • Read this thread:

    http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/9ab88733-d17d-48d2-9d3c-ec55ed1f8687


    If you want something you've never had, you need to do something you've never done.

    Everyone should take the time to mark helpful posts and propose answers!

    Answer an interesting question?
    Create a wiki article about it!

    My Technet Wiki Articles

    Tuesday, December 11, 2012 8:21 PM
    Moderator
  • Thanks Paul, that helps a lot. Now I have to figure out how my button can display the text xD

    Make it a function that returns a string value, then it would be assignable, and you wouldn't need to know the button in reference. Unless you wanted to pass the control where you want to display the text as a param, otherwise you'll have to pass the instance of that class to the module, or use VB's default instance. Remember that most of what you're dealing with resides in an instance of the class Form1 (or whatever else it might be named). So you need to use the instance to say what we are editing for which instance of that class.

    Module TestMod
    	Public Sub DoSomething()
    		Form1.Button1.Text = "something"
    	End Sub
    End Module
    If you're talking about the button click assigning this string value elsewhere, then same principle behind defining Form1. Otherwise, it'll infer something else instead.


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    Visit the Forum: TechLifeForum



    Wednesday, December 12, 2012 3:30 AM
  • Hi Chanoun,

    I'm a little confused about this thread. One side you talk about a text based game, other side you talk about controls like button.

    Do you like to start a separate application with a button of your new (own) programm?
    If so, you can use the process.Start ("PathOfApplication") Event.
    The code sample below shall explain how to modify a Button Text and how to start an application by using process.start event.

    Be aware your application needs the correct and complet path of the application to run.
    Also note that the application you like to start may not be installed on other computers.
    A good method in this case is including the regarding programm as ressource to your own application and start it as ressorce.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Displays "Start Paint" as Text on Botton1
            Me.Button1.Text = "Start Paint"
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Runs appliction "mspaint.exe"
            'note, other applications might requirere complete path
            Process.Start("mspaint.exe")
        End Sub
    End Class

    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 and VB 2010 Express Be a good forum member - mark posts that contain the answers to your questions or those that are helpful. c# in vb Translator: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Thursday, December 13, 2012 8:48 AM

All replies

  • You don't give a lot of details to work from, but from a form (such as your startup form), you can call Public Subs or Functions that are in your module directly. For example, if your module is called RunGame and contains a Public Sub called StartGame, then you can call it from your startup form like this:

    Public Class StartupForm
        Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
            StartGame()
        End Sub
    
        'Lots of other code here
    End Class
    
    Public Module RunGame
        Sub StartGame()
            'Code to start the game
        End Sub
    End Module


    • Edited by Blackwood Tuesday, December 11, 2012 7:50 PM
    Tuesday, December 11, 2012 7:49 PM
  • Hi I tried that and I can't get it to work some how here's my form (called form1.vb) 

    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Rungame()
        End Sub
    End Class

     And heres my Module called Rungame.vb (I want it to come up with a CMD prompt)

    Public Module Rungame
    
        Private Property Answer As String
    
        Sub Rungame()
            Console.WriteLine("Hello world")
            Console.WriteLine("1. Fuck you Visual Basic")
            Console.WriteLine("2. Hello")
            Answer = Console.ReadLine()
            If Answer = 1 Then
                Console.WriteLine("A gash appears on your right side and you die")
                Console.ReadLine()
            ElseIf Answer = 2 Then
                Console.WriteLine("Nice weather right?")
                Console.ReadLine()
            Else
                Console.WriteLine("Not a choice")
                Console.ReadLine()
    
            End If
        End Sub
    
    End Module
    

    Tuesday, December 11, 2012 8:03 PM
  • Read this thread:

    http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/9ab88733-d17d-48d2-9d3c-ec55ed1f8687


    If you want something you've never had, you need to do something you've never done.

    Everyone should take the time to mark helpful posts and propose answers!

    Answer an interesting question?
    Create a wiki article about it!

    My Technet Wiki Articles

    Tuesday, December 11, 2012 8:21 PM
    Moderator
  • Thanks Paul, that helps a lot. Now I have to figure out how my button can display the text xD
    Tuesday, December 11, 2012 8:28 PM
  • Thanks Paul, that helps a lot. Now I have to figure out how my button can display the text xD
    Are you talking about Button.Text property, or clicking the button to make it display into console? Please explain clearly.

    If you want something you've never had, you need to do something you've never done.

    Everyone should take the time to mark helpful posts and propose answers!

    Answer an interesting question?
    Create a wiki article about it!

    My Technet Wiki Articles

    Tuesday, December 11, 2012 11:02 PM
    Moderator
  • Thanks Paul, that helps a lot. Now I have to figure out how my button can display the text xD

    Make it a function that returns a string value, then it would be assignable, and you wouldn't need to know the button in reference. Unless you wanted to pass the control where you want to display the text as a param, otherwise you'll have to pass the instance of that class to the module, or use VB's default instance. Remember that most of what you're dealing with resides in an instance of the class Form1 (or whatever else it might be named). So you need to use the instance to say what we are editing for which instance of that class.

    Module TestMod
    	Public Sub DoSomething()
    		Form1.Button1.Text = "something"
    	End Sub
    End Module
    If you're talking about the button click assigning this string value elsewhere, then same principle behind defining Form1. Otherwise, it'll infer something else instead.


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    Visit the Forum: TechLifeForum



    Wednesday, December 12, 2012 3:30 AM
  • Hi Chanoun,

    I'm a little confused about this thread. One side you talk about a text based game, other side you talk about controls like button.

    Do you like to start a separate application with a button of your new (own) programm?
    If so, you can use the process.Start ("PathOfApplication") Event.
    The code sample below shall explain how to modify a Button Text and how to start an application by using process.start event.

    Be aware your application needs the correct and complet path of the application to run.
    Also note that the application you like to start may not be installed on other computers.
    A good method in this case is including the regarding programm as ressource to your own application and start it as ressorce.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Displays "Start Paint" as Text on Botton1
            Me.Button1.Text = "Start Paint"
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Runs appliction "mspaint.exe"
            'note, other applications might requirere complete path
            Process.Start("mspaint.exe")
        End Sub
    End Class

    Liebe Grüße Stefan | Cheers Stefan I'm using VB 2008 and VB 2010 Express Be a good forum member - mark posts that contain the answers to your questions or those that are helpful. c# in vb Translator: http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Thursday, December 13, 2012 8:48 AM