Results 1 to 4 of 4

Thread: Select Cases from External location.

  1. #1
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Select Cases from External location.

    Hello,
    I'd like to select my cases from an external location.
    Here is an example of what i want
    Code:
    My.Computer.Network.DownloadFile("https://dl.dropbox.com/u/35848813/Minecraft/MineRevolution/Servers/RANDOM.txt", Application.StartupPath & "\RANDOM.txt")
            Dim key As Integer
            key = Int(Rnd() * 5)
                Select Case key 
                   My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\RANDOM.txt")
                End Select
    Code:
    Inside Random.txt
    Case 1
                        TextBox1.Text = "" ' it shows this in textbox1
                    Case 2
                        TextBox1.Text = ""
    If you still didn't understand here is a further Explanation.
    I want my users, when they click a button, to download a file from my Dropbox(a .txt file). Then, the file will read all of the code, and add it to the code..So for example if in the .txt it was something like
    Code:
    Inside Random.txt
    Case 1
                        TextBox1.Text = "" 
                    Case 2
                        TextBox1.Text = ""
    Then in the program source it will be something like this

    Code:
    Dim key As Integer
            key = Int(Rnd() * 5)
                Select Case key 
    Case 1
    TextBox1.Text = "" ' it shows this in textbox1
                    Case 2
                        TextBox1.Text = ""
                End Select
    Thanks.
    If there is any other way to do it please tell me

  2. #2
    Member
    Join Date
    Sep 10
    Posts
    38

    Re: Select Cases from External location.

    I hope you at least try to understand the code...,there is already comment to explain some of them... so here it goes....first Import required references...


    Code:
    Imports System.IO
    Imports System.Reflection
    Imports System.CodeDom
    Imports System.CodeDom.Compiler
    Imports Microsoft.VisualBasic

    and then in your Button_click:

    Code:
      'Download File
            My.Computer.Network.DownloadFile("https://dl.dropbox.com/u/35848813/Minecraft/MineRevolution/Servers/RANDOM.txt", Application.StartupPath & "\RANDOM.txt")
    
            'First read the code in textfile
            Dim input As String = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\RANDOM.txt")
    
            ' Create "code" literal to pass to the compiler.  
            '<% = input % > will fill up the code fragment
            Dim code = <code>  
                             Imports System  
                             Imports System.Windows.Forms 
                             Imports Microsoft.VisualBasic 
    
                             Public Class TempClass  
                                 Public Sub UpdateText(ByVal txtOutput As TextBox)  
                            Dim key As Integer
                            key = Int(Rnd() * 5)
                                Select Case key 
                                   <%= input %>  
                                End Select
    
                                 End Sub  
                             End Class  
                         </code>
    
            ' Create the VB.NET compiler.  
            Dim vbProv = New VBCodeProvider()
            ' Create parameters to pass to the compiler.  
            Dim vbParams = New CompilerParameters()
            ' Add referenced assemblies.  
            vbParams.ReferencedAssemblies.Add("mscorlib.dll")
            vbParams.ReferencedAssemblies.Add("System.dll")
            vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
            vbParams.GenerateExecutable = False
            ' Ensure we generate an assembly in memory and not as a physical file.  
            vbParams.GenerateInMemory = True
    
            ' Compile the code and get the compiler results (contains errors, etc.)  
            Dim compResults = vbProv.CompileAssemblyFromSource(vbParams, code.Value)
    
            ' Check for compile errors  
            If compResults.Errors.Count > 0 Then
    
                ' Show each error.  
                For Each er In compResults.Errors
                    MessageBox.Show(er.ToString())
                Next
    
            Else
    
                ' Create instance of the temporary compiled class.  
                Dim obj As Object = compResults.CompiledAssembly.CreateInstance("TempClass")
                ' An array of object that represent the arguments to be passed to our method (UpdateText).  
                Dim args() As Object = {Me.txtOutput}
                ' Execute the method by passing the method name and arguments.  
                Dim t As Type = obj.GetType().InvokeMember("UpdateText", BindingFlags.InvokeMethod, Nothing, obj, args)
    
            End If

    I used txtOutput as name instead of Textbox1...
    Please Rate me If I helped.

    Do not just copy paste codes, try to understand and learn from it.

  3. #3
    Addicted Member
    Join Date
    Jun 12
    Posts
    152

    Re: Select Cases from External location.

    Quote Originally Posted by palautot View Post
    I hope you at least try to understand the code...,there is already comment to explain some of them... so here it goes....first Import required references...


    Code:
    Imports System.IO
    Imports System.Reflection
    Imports System.CodeDom
    Imports System.CodeDom.Compiler
    Imports Microsoft.VisualBasic

    and then in your Button_click:

    Code:
      'Download File
            My.Computer.Network.DownloadFile("https://dl.dropbox.com/u/35848813/Minecraft/MineRevolution/Servers/RANDOM.txt", Application.StartupPath & "\RANDOM.txt")
    
            'First read the code in textfile
            Dim input As String = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\RANDOM.txt")
    
            ' Create "code" literal to pass to the compiler.  
            '<% = input % > will fill up the code fragment
            Dim code = <code>  
                             Imports System  
                             Imports System.Windows.Forms 
                             Imports Microsoft.VisualBasic 
    
                             Public Class TempClass  
                                 Public Sub UpdateText(ByVal txtOutput As TextBox)  
                            Dim key As Integer
                            key = Int(Rnd() * 5)
                                Select Case key 
                                   <%= input %>  
                                End Select
    
                                 End Sub  
                             End Class  
                         </code>
    
            ' Create the VB.NET compiler.  
            Dim vbProv = New VBCodeProvider()
            ' Create parameters to pass to the compiler.  
            Dim vbParams = New CompilerParameters()
            ' Add referenced assemblies.  
            vbParams.ReferencedAssemblies.Add("mscorlib.dll")
            vbParams.ReferencedAssemblies.Add("System.dll")
            vbParams.ReferencedAssemblies.Add("System.Windows.Forms.dll")
            vbParams.GenerateExecutable = False
            ' Ensure we generate an assembly in memory and not as a physical file.  
            vbParams.GenerateInMemory = True
    
            ' Compile the code and get the compiler results (contains errors, etc.)  
            Dim compResults = vbProv.CompileAssemblyFromSource(vbParams, code.Value)
    
            ' Check for compile errors  
            If compResults.Errors.Count > 0 Then
    
                ' Show each error.  
                For Each er In compResults.Errors
                    MessageBox.Show(er.ToString())
                Next
    
            Else
    
                ' Create instance of the temporary compiled class.  
                Dim obj As Object = compResults.CompiledAssembly.CreateInstance("TempClass")
                ' An array of object that represent the arguments to be passed to our method (UpdateText).  
                Dim args() As Object = {Me.txtOutput}
                ' Execute the method by passing the method name and arguments.  
                Dim t As Type = obj.GetType().InvokeMember("UpdateText", BindingFlags.InvokeMethod, Nothing, obj, args)
    
            End If

    I used txtOutput as name instead of Textbox1...
    Ok thanks, I will use it when i understand the code. How can i rate you?

  4. #4
    Member
    Join Date
    Sep 10
    Posts
    38

    Re: Select Cases from External location.

    you could thank me by simply clicking the "Rate this post" in bottom left of my post...you could see it when you have logged in..

    It always feels good when your help is being appreciated. =)
    Last edited by palautot; Jun 21st, 2012 at 05:00 AM.
    Please Rate me If I helped.

    Do not just copy paste codes, try to understand and learn from it.

Posting Permissions

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