Fazer uma PerguntaFazer uma Pergunta
 

RespondidoHow to use Application.Restart

  • segunda-feira, 2 de novembro de 2009 15:00KatlynD Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    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

Respostas

  • segunda-feira, 2 de novembro de 2009 15:03Tamer OzMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    Hi,

    You dont need to write a sub named Restart. Just call Application.Restart() at your button click.
    • Marcado como RespostaKatlynD quarta-feira, 4 de novembro de 2009 18:25
    •  
  • quarta-feira, 4 de novembro de 2009 14:39Heslacher Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     RespondidoContém Código
    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/

Todas as Respostas

  • segunda-feira, 2 de novembro de 2009 15:03Tamer OzMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Respondido
    Hi,

    You dont need to write a sub named Restart. Just call Application.Restart() at your button click.
    • Marcado como RespostaKatlynD quarta-feira, 4 de novembro de 2009 18:25
    •  
  • segunda-feira, 2 de novembro de 2009 15:17KatlynD Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Contém Código
    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'. 
  • segunda-feira, 2 de novembro de 2009 17:09Tamer OzMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Is your application .net framework 1.1 application.

    Application.Restart method is in framework since 2.0 version.
  • segunda-feira, 2 de novembro de 2009 19:00KatlynD Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    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.
  • segunda-feira, 2 de novembro de 2009 20:16Tamer OzMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Contém Código
    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
    
  • terça-feira, 3 de novembro de 2009 17:49Tergiver Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    restart is not a member of Application, but Restart is.

    Sounds like a caseing problem?
  • quarta-feira, 4 de novembro de 2009 13:37KatlynD Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     Contém Código
    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.)
  • quarta-feira, 4 de novembro de 2009 14:00Heslacher Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    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/
  • quarta-feira, 4 de novembro de 2009 14:34KatlynD Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    I am using Visual Studio 2005
  • quarta-feira, 4 de novembro de 2009 14:39Heslacher Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     RespondidoContém Código
    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/
  • quarta-feira, 4 de novembro de 2009 16:12KatlynD Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    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?
  • quarta-feira, 4 de novembro de 2009 18:09Tamer OzMVPMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    Hi,

    It's better to ask there.

    Btw hope this helps.

    http://www.codeproject.com/KB/mobile/WiMoAutostart.aspx
  • quarta-feira, 4 de novembro de 2009 22:34JaedenRuiner Medalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuárioMedalhas de usuário
     
    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."