Ask a questionAsk a question
 

AnswerHow to use Application.Restart

  • Monday, November 02, 2009 3:00 PMKatlynD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi everyone,

    I have an application that has a Sign Off button. Is the application.restart suitable for this purpse? Can I make it work with the click event of a button? I have a link to some information but I am not sure how to implement this in my code.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.application.restart.aspx

    So to use this all I need is a

    Public Shared Sub Restart

    and then at the click event I code Application.Restart?

    But I am not sure what goes in the Restart Sub.

    Can someone help please??

    Thanks,

    Katlyn

Answers

  • Monday, November 02, 2009 3:03 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    You dont need to write a sub named Restart. Just call Application.Restart() at your button click.
    • Marked As Answer byKatlynD Wednesday, November 04, 2009 6:25 PM
    •  
  • Wednesday, November 04, 2009 2:39 PMHeslacher Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi again,

    then it should really work, because its implemented since NET 2.0. Try to create a new windows forms project, add one button on the form. Then paste the code below:

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

    i am also using VB 2005 and it works like a charme.

    Edit: Also search in the Object browser for Restart you should find 1 method

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/

All Replies

  • Monday, November 02, 2009 3:03 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi,

    You dont need to write a sub named Restart. Just call Application.Restart() at your button click.
    • Marked As Answer byKatlynD Wednesday, November 04, 2009 6:25 PM
    •  
  • Monday, November 02, 2009 3:17 PMKatlynD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi,

    If I do the following:

    Private Sub MenuItemSignOff_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MenuItemSignOff.Click
      Application.restart()
    End Sub
    
    I get this error:

    'restart' is not a member of 'System.Windows.Forms.Application'. 
  • Monday, November 02, 2009 5:09 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is your application .net framework 1.1 application.

    Application.Restart method is in framework since 2.0 version.
  • Monday, November 02, 2009 7:00 PMKatlynD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I believe its version 2.0.

    I am using Visual Studio 2005 and when I go to Project Properties > References Tab, each item listed says 2.0 under version except for the last item, which is another Project called VControls that is being referenced and it is version 1.0. Would that matter?

    The System.Windows.Forms says version 2.0.
  • Monday, November 02, 2009 8:16 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    This is my form and it works fine,

    Could you try like this,

    Imports System.IO
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           
    
        End Sub
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            System.Windows.Forms.Application.Restart()
        End Sub
    End Class
    
  • Tuesday, November 03, 2009 5:49 PMTergiver Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    restart is not a member of Application, but Restart is.

    Sounds like a caseing problem?
  • Wednesday, November 04, 2009 1:37 PMKatlynD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    So at the top of my form I have

    Imports System.IO
    
    included and then here is my button click.

    Private Sub MenuItemSignOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemSignOff.Click
        System.Windows.Forms.Application.Restart()
    End Sub
    
    And it is still giving me the message: Error 5 'Restart' is not a member of 'System.Windows.Forms.Application'. 


    In my project properties, under the references tab, does it have to say System.IO in the top section under References? On the bottom portion where it says Imported Namespaces, System.IO is in the list, but is not checked. Would any of this be creating the problem?

    (I can't seem to add System.IO, if I check the box, the Add User Import does not become enabled.)
  • Wednesday, November 04, 2009 2:00 PMHeslacher Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi KatlynD,

    which version of VB do you use ?
    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
  • Wednesday, November 04, 2009 2:34 PMKatlynD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am using Visual Studio 2005
  • Wednesday, November 04, 2009 2:39 PMHeslacher Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    Hi again,

    then it should really work, because its implemented since NET 2.0. Try to create a new windows forms project, add one button on the form. Then paste the code below:

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

    i am also using VB 2005 and it works like a charme.

    Edit: Also search in the Object browser for Restart you should find 1 method

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
  • Wednesday, November 04, 2009 4:12 PMKatlynD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Ok, so that worked, and then I thought of something. I am creating an application for a mobile device. This could be the problem perhaps?

    Sorry I didn't think of this sooner. I never thought to mention it. Do you know if there would be a way around this?? Or should I post to the Windows Mobile Development forum?
  • Wednesday, November 04, 2009 6:09 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    It's better to ask there.

    Btw hope this helps.

    http://www.codeproject.com/KB/mobile/WiMoAutostart.aspx
  • Wednesday, November 04, 2009 10:34 PMJaedenRuiner Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Actually, I'll let ya'll in on a little secret that is very annoying with VB versus C#.  VB uses the My extensions application model, as well as little annoyance of Namespace inference. 

    So.  Say a C# App is like this:

    using System;
    using System.IO;
    using System.Data;

    VB
    Imports System;
    Imports System.IO;
    Imports System.Data;

    Now.  Because of Inference this is legal in VB:

    dim Path as String = "C:\Program Files\MyFileName.txt"
    dim FileName as String = IO.Path.GetFileName(Path)

    In C# this is ILLEGAL
    string Path = @"C:\Program Files\MyFileName.txt";
    string FileName = IO.Path.GetFileName(Path);
    however you can do:  string FileName = System.IO.Path. GetFileName(Path);
    and then it will work.   C# does not infer the System namespace, same as vb you can also do SqlClient.SqlCommand.  for C# you'd either have to explicitly declare using System.Data.SqlClient, or you'd have explicitly execute System.Data.SqlClient.SqlCommand.<whatever> 

    This is where that inference comes into play with the My namespace application model, especially when you are in a Windows Forms application just using:

    Application.<WhateverMethodHere>

    This causes VB to AUTOMATICALLY infer the My namespace.  And therefore My.Application.Restart does not exist.  There is a Static Class in the System.Windows.Forms Namespace that contains other applicaiton wide methods which are available from C# and VB, but as VB developers many times we assume the Application is always the My.Application class, which is actually a tricky link up in VB back end.  (Similar is how the My.Settings is actually a Hidden Module in the Settings.Designer.vb which has a single shared property "Settings".  This points to the MySettings module with the shared property [Default] which points to the shared field member defaultInstance. 

    thus when you access My.Settings you are actually referencing:

    Global.ApplicationName.MySettingsModule.Settings
       {Returns MySettings.[Default]}

    The Same is True with My.Application, thus it is imperative when assisting VB programmers to explicitly point out the System.Windows.Form.Application class, to not be confusing to the Compiler which infers My.Application when Application is not prefixed with any namespace, and Application.<Blah> will defaultly point to My.Application where <Blah> is not a member.

    Hope this helps
    Jaeden "Sifo Dyas" al'Raec Ruiner







    "Never Trust a computer. Your brain is smarter than any micro-chip."