Results 1 to 27 of 27

Thread: File Association Help

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    File Association Help

    I am trying to create an association for my vb.net application.

    I've got two forms, of which the startup form is called "frmMain" and the other which has a rich textbox that displays results from a file (.tbx) is called "frmResultsViewer"

    What I want is that when a person wishes to open a .tbx file on his pc, it should open into "frmResultsViewer".

    I am using this code at the moment, but it open into "frmMain". I have placed this code into the "frmResultsViewer" code but still open with the startup form.

    Code:
        Public Sub CheckAssociation()
            My.Computer.Registry.ClassesRoot.CreateSubKey(".tbx").SetValue("", "Tiny Toolbox", Microsoft.Win32.RegistryValueKind.String)
            My.Computer.Registry.ClassesRoot.CreateSubKey("Tiny Toolbox\shell\open\command").SetValue("", Application.ExecutablePath & " ""%1"" ", Microsoft.Win32.RegistryValueKind.String)
            My.Computer.Registry.ClassesRoot.CreateSubKey("Tiny Toolbox\DefaultIcon").SetValue("", Application.StartupPath & "\TBX.ico")
        End Sub

    FORM LOAD:

    Code:
    CheckAssociation()
    
            Dim file As String = Command$()
            If Not file = "" Then
                file = Replace(file, Chr(34), "")
                Me.rtxtResult.LoadFile(file)
            End If

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: File Association Help

    presumably frmMain is the main start up object... so when the app launches, that's where it's going to go... since you want to open a different form... you'll need to make some changes...

    First turn off the application framework (it's in the project propertieS)... then add a module, and in it add a public sub main ... just like that... "public sub main()" ... in it, you'll want to check the commandline arguments... if the file argument exists, then re-start the app using the correct form, if it doesn't then re-start the app with frmMain instead...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    Quote Originally Posted by techgnome View Post
    presumably frmMain is the main start up object... so when the app launches, that's where it's going to go... since you want to open a different form... you'll need to make some changes...

    First turn off the application framework (it's in the project propertieS)... then add a module, and in it add a public sub main ... just like that... "public sub main()" ... in it, you'll want to check the commandline arguments... if the file argument exists, then re-start the app using the correct form, if it doesn't then re-start the app with frmMain instead...

    -tg
    Thanks for the reply.

    How do i "check the command line arguments"?

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: File Association Help

    You're doing that already... aren't you?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    Quote Originally Posted by techgnome View Post
    You're doing that already... aren't you?

    -tg
    Umm... I don't think so...
    A simple example would be helpful to understanding this, as I don't think I've ever used Command line arguments...

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: File Association Help

    vb.net Code:
    1. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    2.         For Each arg In My.Application.CommandLineArgs ' splits on space everything that follows the command/filename
    3.             ListBox1.Items.Add(arg)
    4.         Next
    5.     End Sub
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: File Association Help

    then what's this doing?
    Code:
            Dim file As String = Command$()
            If Not file = "" Then
                file = Replace(file, Chr(34), "")
                Me.rtxtResult.LoadFile(file)
            End If
    But Dunfiddlin's code is probably better...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    Quote Originally Posted by dunfiddlin View Post
    vb.net Code:
    1. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    2.         For Each arg In My.Application.CommandLineArgs ' splits on space everything that follows the command/filename
    3.             ListBox1.Items.Add(arg)
    4.         Next
    5.     End Sub
    I still don't get it..
    :'(
    I ran the code and the listbox is empty..


    Quote Originally Posted by techgnome View Post
    then what's this doing?
    Code:
            Dim file As String = Command$()
            If Not file = "" Then
                file = Replace(file, Chr(34), "")
                Me.rtxtResult.LoadFile(file)
            End If
    But Dunfiddlin's code is probably better...

    -tg
    To be honest, I got that piece of code from a Tutorial that I used to do the association.

    I made use of it since it worked perfectly in the tutorial, except for the fact that he/she didn't have multiple forms...

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: File Association Help

    Well, yes it will be empty if you just 'run the code'. They're called commandline arguments because they're part of a commandline. Run the compiled application from the Run dialog or Command prompt or a shortcut like so .....

    C:\MyDir\MyApplication.exe Arg1 Arg2 Arg3

    ... and your listbox will have something to show.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    Quote Originally Posted by dunfiddlin View Post
    Well, yes it will be empty if you just 'run the code'. They're called commandline arguments because they're part of a commandline. Run the compiled application from the Run dialog or Command prompt or a shortcut like so .....

    C:\MyDir\MyApplication.exe Arg1 Arg2 Arg3

    ... and your listbox will have something to show.
    ok. so I tried as you said.
    I ran the app from "RUN", The list box contains whatever i wrote after [appname.exe]

    I still don't get it, how does this relate to what I want to achieve?
    I'm sorry, but I ain't that good at vb.net. am still learning and haven't yet discovered about command line arguements.

  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: File Association Help

    If the application is opened via the file association there will be an argument, namely the filename. If it is opened directly then there will not. You can use this to direct the execution to the correct sub/form.

    If My.Application.CommandLineArgs.Count > 0
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  12. #12

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    I made a few changes (included the "Command line arguements"). Now the code is:

    In frmMain (The main/ Startup form):

    Code:
        Public Sub CheckAssociation()
            My.Computer.Registry.ClassesRoot.CreateSubKey(".tbx").SetValue("", "Tiny Toolbox", Microsoft.Win32.RegistryValueKind.String)
            My.Computer.Registry.ClassesRoot.CreateSubKey("Tiny Toolbox\shell\open\command").SetValue("", Application.ExecutablePath & " ""%1"" ", Microsoft.Win32.RegistryValueKind.String)
            My.Computer.Registry.ClassesRoot.CreateSubKey("Tiny Toolbox\DefaultIcon").SetValue("", Application.StartupPath & "\TBX.ico")
        End Sub
    When FrmMain is Loaded:

    Code:
    	CheckAssociation()
    
            If My.Application.CommandLineArgs.Count > 0 Then
                frmResultsViewer.Show()
            End If


    I get this error just before frmResultsViewer is Loaded.

    Unhandled exception.
    File Format is not valid.


    Code:
    ************** Exception Text **************
    System.ArgumentException: File format is not valid.
       at System.Windows.Forms.RichTextBox.StreamIn(Stream data, Int32 flags)
       at System.Windows.Forms.RichTextBox.LoadFile(Stream data, RichTextBoxStreamType fileType)
       at System.Windows.Forms.RichTextBox.LoadFile(String path, RichTextBoxStreamType fileType)
       at DevComponents.DotNetBar.Controls.RichTextBoxEx.LoadFile(String path)
       at Tiny_Toolbox.frmResultsViewer.frmResultsViewer_Load(Object sender, EventArgs e) in C:\Users\AMK\Documents\Visual Studio 2010\Projects\Tiny Toolbox\Tiny Toolbox\frmResultsViewer.vb:line 33
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at DevComponents.DotNetBar.RibbonForm.OnLoad(EventArgs e)
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at DevComponents.DotNetBar.RibbonForm.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

  13. #13
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: File Association Help

    Er .. that's nothing to do with this code. That's saying that you're trying to read a non RTF file into an RTF textbox.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  14. #14

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    Quote Originally Posted by dunfiddlin View Post
    Er .. that's nothing to do with this code. That's saying that you're trying to read a non RTF file into an RTF textbox.

    What the rich text box contains initially is RTF, then I save the file as .tbx (My own File association) and the rtf is deleted.
    Any time the user loads the file it's in .tbx format, and it loads correctly without any errors. This happens only when the file is opened (when the app is not open).

    Also, How can I hide the main start-up form (frmMain)?
    I tried
    Code:
    me.hide()
    but it hides the other form as well.

    Thanks for the Help Guys.

  15. #15

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    < bump >

  16. #16
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: File Association Help

    Project Menu > Application Properties > Application Tab > ShutDown Mode > When last form closes
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  17. #17

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    Quote Originally Posted by dunfiddlin View Post
    Project Menu > Application Properties > Application Tab > ShutDown Mode > When last form closes
    Ok. Thanks, but what about the Error with the Rich Text Box, that's the main issue...

  18. #18
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: File Association Help

    The error message states "File format is not valid." Are you sure the file path you are supplying to the RichTextBoxEx.LoadFile(String path) method is the path to a valid RTF file? Have you checked the value of the string you are passing by using a breakpoint on the line that calls the LoadFile method (or by using a messagebox to show its value just before that line)?

    You are leaving a lot to the imagination here. Can you show the code for your frmResultsViewer_Load Sub?

  19. #19

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    I am really sorry for replying very late, but here's the code for the frmResultsViewer_Load Sub:

    Code:
            Left = (SystemInformation.WorkingArea.Size.Width - Size.Width)
            Top = (SystemInformation.WorkingArea.Size.Height - Size.Height)
    
            If rtxtResult.Text = "" Then
                rtxtResult.Text = vbNewLine & vbNewLine & "Click on Load to Open an already Saved Log File"
                btnClear.Enabled = False
                btnSavetotxt.Enabled = False
                btnCopyAll.Enabled = False
            Else
                btnClear.Enabled = True
                btnSavetotxt.Enabled = True
                btnCopyAll.Enabled = True
            End If
    
            Dim file As String = Command$()
            If Not file = "" Then
                file = Replace(file, Chr(34), "")
                Me.rtxtResult.LoadFile(file)
            End If

  20. #20
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: File Association Help

    I can't see anything there that would cause an error, if you tried to launch your App by double clicking on a file with a valid RTF format (renamed as a .tbx file, of course).

    So, from the code you have shown, I still think you may be trying to load a non RTF formatted file. In fact, using your code in the Load event handler of a Form and double clicking on a plain .txt file (renamed as .tbx), then I get a very similar error message:

    ******* Exception Text **************
    System.ArgumentException: File format is not valid.
    at System.Windows.Forms.RichTextBox.StreamIn(Stream data, Int32 flags)
    at System.Windows.Forms.RichTextBox.LoadFile(Stream data, RichTextBoxStreamType fileType)
    at System.Windows.Forms.RichTextBox.LoadFile(String path, RichTextBoxStreamType fileType)
    at System.Windows.Forms.RichTextBox.LoadFile(String path)
    at TemplateNewest1.frmResultsViewer.frmResultsViewer_Load(Object sender, EventArgs e) in C:\Users\Steve\AppData\Local\Temporary Projects\TemplateNewest1\frmResultsViewer.vb:line 21
    at System.Windows.Forms.Form.OnLoad(EventArgs e)
    at System.Windows.Forms.Form.OnCreateControl()
    at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
    at System.Windows.Forms.Control.CreateControl()
    at System.Windows.Forms.Control.WmShowWindow(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
    at System.Windows.Forms.ContainerControl.WndProc(Message& m)
    at System.Windows.Forms.Form.WmShowWindow(Message& m)
    at System.Windows.Forms.Form.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    Obviously, I am not using a DotNetBar control, but I can't see that making any difference.


    Another thought: when you load your .tbx file from a Button click, are you using a different overload of the RichTextBox.LoadFile Method, perhaps specifying a specific StreamType argument?


    Given that I don't have access to the rest of your code, nor to the .tbx file you are trying to launch your App with, the best I can say is to modify your code slightly to give some feedback as to what is going on.

    The following should catch the error, tell you the name of the file it is trying to load (so you can check the App's command line arguments are as expected), and allow the code to continue. You should then be able to try opening the same errant file from a Button click and see what happens. (check that you are using the same .FileLoad method in the Button_Click as you use in the Form_Load).

    vb.net Code:
    1. Private Sub frmResultsViewer_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    2.     Left = (SystemInformation.WorkingArea.Size.Width - Size.Width)
    3.     Top = (SystemInformation.WorkingArea.Size.Height - Size.Height)
    4.  
    5.     If rtxtResult.Text = "" Then
    6.         rtxtResult.Text = vbNewLine & vbNewLine & "Click on Load to Open an already Saved Log File"
    7.         btnClear.Enabled = False
    8.         btnSavetotxt.Enabled = False
    9.         btnCopyAll.Enabled = False
    10.     Else
    11.         btnClear.Enabled = True
    12.         btnSavetotxt.Enabled = True
    13.         btnCopyAll.Enabled = True
    14.     End If
    15.  
    16.  
    17.     Dim file As String = Command$()
    18.     Try
    19.         If Not file = "" Then
    20.             file = Replace(file, Chr(34), "")
    21.             Me.rtxtResult.LoadFile(file)
    22.         End If
    23.     Catch ex As Exception
    24.         MessageBox.Show(String.Format("Error while attempting to load file {0}{1}{0}{0}{2}", _
    25.                                       Environment.NewLine, file, ex.Message))
    26.     End Try
    27. End Sub

  21. #21

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    Thank you for replying..

    I edited the code as you advised..

    The Try Catch block catches this error:
    Code:
    The file format is not valid.

    The Button_click open up an Open File Dialog, and the code for the openfiledialog_fileok is:

    Code:
        Private Sub OFDloadresults_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles OFDloadresults.FileOk
            Dim LoadFileLocation As String = OFDloadresults.FileName
    
            Dim objreader As New IO.StreamReader(LoadFileLocation)
            rtxtResult.Text = objreader.ReadToEnd()
            objreader.Close()
        End Sub
    It uses StreamReader to load the file... that said, i changed the code for frmResultsViewer_Load Sub to this:


    Code:
            Dim file As String = Command$()
            Try
                If Not file = "" Then
                    Dim objreader As New IO.StreamReader(file)
                    rtxtResult.Text = objreader.ReadToEnd()
                    objreader.Close()
                End If
            Catch ex As Exception
                MessageBox.Show(String.Format("Error while attempting to load file {0}{1}{0}{0}{2}", _
                                              Environment.NewLine, file, ex.Message))
            End Try
    And now it says, Illegal Characters in path

    I'm having doubts with this part
    Code:
    Dim file As String = Command$()
    What does it do?


  22. #22
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: File Association Help

    Quote Originally Posted by TheThinker View Post
    I'm having doubts with this part
    Code:
    Dim file As String = Command$()
    What does it do?



    From the MSDN: The Command Function "Returns the argument portion of the command line used to start ..... an executable program developed with Visual Basic."

    The point of adding the Try...Catch block with the MessageBox was to display the contents of those command line arguments (that you then assign to the variable 'file') when you got the error. So you should have seen something like:


    Error while attempting to load file
    C:\Test\test.tbx
    File format is not valid


    Thinking about it, though, if what is returned is something unexpected and also contains non-printable characters, the file path name may not have shown in the MessageBox. I'm hoping that you did see a file path there, though.

    The latest error you are getting should look something like:


    Error while attempting to load file
    "C:\Test\test.tbx"
    Illegal Characters in path


    Note the quotation marks around the file path name. It is the quatation marks that are the illegal characters, and they are there because you left out the line:
    Code:
    file = Replace(file, Chr(34), "")
    So the following should work in theory:
    Code:
    Dim file As String = Command$()
    Try
        If Not file = "" Then
            file = Replace(file, Chr(34), "")
            Dim objreader As New IO.StreamReader(file)
            rtxtResult.Text = objreader.ReadToEnd()
            objreader.Close()
        End If
    
    Catch ex As Exception
        MessageBox.Show(String.Format("Error while attempting to load file {0}file = {1}{0}{0}{2}", _
                                      Environment.NewLine, file, ex.Message))
    End Try

    As an alternative to the Command Function, check out the CommandLineArgs Property. This splits the command line up and returns all the separate arguments as a ReadOnlyCollection(Of String), without the quotation marks. So, in this particular instance, you could replace the above with:
    Code:
    Dim file = My.Application.CommandLineArgs(0)
    Try
        If Not file = "" Then
            Dim objreader As New IO.StreamReader(file)
            rtxtResult.Text = objreader.ReadToEnd()
            objreader.Close()
        End If
    
    Catch ex As Exception
        MessageBox.Show(String.Format("Error while attempting to load file {0}file = {1}{0}{0}{2}", _
                                      Environment.NewLine, file, ex.Message))
    End Try

    Finally, I'm still leaning towards the initial error arising from you having saved your .tbx file as plain text. If that is so, then instead of using a StreamReader, you could use an overload of the RichTextBox.LoadFile Method and specify PlainText as the StreamType for the LoadFile Method:

    Code:
    Dim file = My.Application.CommandLineArgs(0)
    Try
        If Not file = "" Then
            Me.rtxtResult.LoadFile(file, RichTextBoxStreamType.PlainText)
        End If
    
    Catch ex As Exception
        MessageBox.Show(String.Format("Error while attempting to load file {0}file = {1}{0}{0}{2}", _
                                      Environment.NewLine, file, ex.Message))
    End Try
    If you use the above to try to load a .tbx file containing RTF formatted data, then it shouldn't produce an error, but you will end up displaying RTF format codes in your RichTextBox.


    Anyway, lots of options to play with there. Good luck

  23. #23
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: File Association Help

    you do know that the RTB comes with both a SAVEFile and a LOADFile method... seems to me using the built-in methods would be soooo much easier here... you can save to plain text or to RTF.... and same with the load.... load from plain text or RTF...


    http://msdn.microsoft.com/en-us/libr....savefile.aspx
    http://msdn.microsoft.com/en-us/libr....loadfile.aspx


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  24. #24

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    Works Perfectly...

    My final code:

    Load Sub:
    Code:
            Dim file = My.Application.CommandLineArgs(0)
            Try
                If Not file = "" Then
                    Me.rtxtResult.LoadFile(file, RichTextBoxStreamType.PlainText)
                    'Dim objreader As New IO.StreamReader(file)
                    'rtxtResult.Text = objreader.ReadToEnd()
                    'objreader.Close()
                End If
    
            Catch ex As Exception
                MessageBox.Show(String.Format("Error while attempting to load file {0}file = {1}{0}{0}{2}", _
                                              Environment.NewLine, file, ex.Message))
            End Try

    I also replaced streamreader (as advised) with this:

    Code:
            Me.rtxtResult.LoadFile(LoadFileLocation, RichTextBoxStreamType.PlainText)

    Thanks a lot everyone who contributed to helping me to fix this issue...

  25. #25

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    The association problem has been fixed, the files open successfully, but a new Problem has arisen.

    All the buttons on my application that invoke the frmResultsViewer to be shown and display the collected log throw an Unhandled exception error...

    Code:
    Index was out of range. Must be non-negative and less than the the size of the collection.
    
    Parameter name: index
    
    ************** Exception Text **************
    System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
       at System.ThrowHelper.ThrowArgumentOutOfRangeException()
       at System.SZArrayHelper.get_Item[T](Int32 index)
       at System.Collections.ObjectModel.ReadOnlyCollection`1.get_Item(Int32 index)
       at Tiny_Toolbox.frmResultsViewer.frmResultsViewer_Load(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at DevComponents.DotNetBar.RibbonForm.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at DevComponents.DotNetBar.RibbonForm.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    It seems related to the frmResultsViewer Load Sub, but quite surprisingly I don't find anything that would cause this error:

    All that I have is:

    Code:
            Left = (SystemInformation.WorkingArea.Size.Width - Size.Width)
            Top = (SystemInformation.WorkingArea.Size.Height - Size.Height)
    
    
            Dim file = My.Application.CommandLineArgs(0)
            Try
                If Not file = "" Then
                    rtxtResult.Clear()
                    Me.rtxtResult.LoadFile(file, RichTextBoxStreamType.PlainText)
                End If
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

  26. #26
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: File Association Help

    Aye, I overlooked the case where frmResultsViewer is launched from a Button Click, without double clicking a .tbx file.

    Now you're using My.Application.CommandLineArgs(0) you need to use a different assertion to check if there are in fact any command line arguments. (if there are none, CommandLineArgs(0) won't exist and you get an error).

    Something along the lines of:
    Code:
    Left = (SystemInformation.WorkingArea.Size.Width - Size.Width)
    Top = (SystemInformation.WorkingArea.Size.Height - Size.Height)
    
    Try
        '  check if there are any command line arguments
        '  before trying to read them
        If My.Application.CommandLineArgs.Count > 0 Then
            Dim tbxFile = My.Application.CommandLineArgs(0)
    
            ' check if command line argument is an
            '  existing file exists before trying to load it
            If IO.File.Exists(tbxFile) Then
                rtxtResult.Clear()
                Me.rtxtResult.LoadFile(tbxFile, RichTextBoxStreamType.PlainText)
            End If
        End If
    
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    I can think of another possible glitch at this point, though. If you open the log viewer by double clicking a .tbx file and then close your frmResultsViewer Form, any attempt to reopen it from a Button on your main form will pick up the previous command line arguments and reload the originally clicked .tbx file. I don't know if that will be a problem for you?

  27. #27

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    61

    Re: File Association Help

    Wow...
    Thanks man..

    Saved me...
    Works just as I needed 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
  •  



Click Here to Expand Forum to Full Width