Results 1 to 6 of 6

Thread: [RESOLVED] Code Question's

  1. #1

    Thread Starter
    Hyperactive Member FireKnox101's Avatar
    Join Date
    Aug 2005
    Location
    Snohomish,Washington
    Posts
    301

    Resolved [RESOLVED] Code Question's

    I'm using Visual Basics.net 2003 and i wanted to know what code line to put to make a new window pop up for example,

    If I hit button A then a New window will open but the for that button A is on will stay open.
    And I also wanted to know what line of code tells the computer to shut down? Because I wanted a shutdown Button on my program.

    I hope you guys or girls can understand what I mean.

    Im currently using: VB.NET 2003, And VB 2005 Express
    My Projects
    Form Them Show Keypress In App
    Simple Ping Control

  2. #2
    Fanatic Member
    Join Date
    May 2005
    Posts
    608

    Re: Code Question's

    You create both forms Form1 and Form2 and add a button to Form1.

    On the button's handler, you type:
    VB Code:
    1. Dim secondFrm as New Form2
    2. secondFrm.Show()

    HoraShadow
    I do like the reward system. If you find that my post was useful, rate it.

  3. #3
    Lively Member Venus's Avatar
    Join Date
    Aug 2005
    Posts
    78

    Re: Code Question's

    To add to Horashadow's answer you'd use the .showdialog instead if you want to force them to stay on the new form until they close it.

    The .show() lets users be able to click back and forth between forms whereas .showdialog() locks them into that new form and they can't go anywhere until they exit it.

  4. #4
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Code Question's

    Check out this must-read 3 part article that goes over working with multiple forms in .NET . It is essential information for anyone programming in .NET

    http://www.devcity.net/Articles/94/1/multipleforms.aspx

  5. #5

    Thread Starter
    Hyperactive Member FireKnox101's Avatar
    Join Date
    Aug 2005
    Location
    Snohomish,Washington
    Posts
    301

    Re: Code Question's

    Ok, Thank you for your help.

    Im currently using: VB.NET 2003, And VB 2005 Express
    My Projects
    Form Them Show Keypress In App
    Simple Ping Control

  6. #6
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: Code Question's

    To ShutDown the computer, you will first need to adjust the privileges for the currently running thread to give that thread the SeShutdownPrivilege permissions.

    API and related
    VB Code:
    1. #Region " InitiateSystemShutdownEX and related API Info "
    2.   ' OpenProcessToken Constants
    3.   Private Const TOKEN_ADJUST_PRIVILEGES As Short = &H20S
    4.   Private Const TOKEN_QUERY As Short = &H8S
    5.  
    6.   ' LookupPrivilegeValue Constants
    7.   Private Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
    8.   Private Const SE_PRIVILEGE_ENABLED As Short = &H2S
    9.  
    10.   ' LookupPrivilegeValue Structures
    11.   Private Structure LUID
    12.     Dim UsedPart As Integer
    13.     Dim IgnoredForNowHigh32BitPart As Integer
    14.   End Structure
    15.  
    16.   ' AdjustTokenPrivileges Structures
    17.   Private Structure TOKEN_PRIVILEGES
    18.     Dim PrivilegeCount As Integer
    19.     Dim pLuid As LUID
    20.     Dim Attributes As Integer
    21.   End Structure
    22.  
    23.   ' InitiateSystemShutdownEx Constants
    24.   Private Const SHTDN_REASON_MAJOR_OPERATINGSYSTEM As Integer = &H20000
    25.   Private Const SHTDN_REASON_MINOR_RECONFIG As Integer = &H4
    26.   Private Const SHTDN_REASON_FLAG_PLANNED As Integer = &H80000000
    27.  
    28.   Private Declare Function InitiateSystemShutdownEx Lib "advapi32" Alias "InitiateSystemShutdownExA" _
    29.     (ByVal lpMachineName As String, _
    30.     ByVal lpMessage As String, _
    31.     ByVal dwTimeout As Integer, _
    32.     ByVal bForceAppsClosed As Boolean, _
    33.     ByVal bRebootAfterShutdown As Boolean, _
    34.     ByVal dwReason As Integer) As Integer
    35.  
    36.   Private Declare Function GetCurrentProcess Lib "kernel32" () As Integer
    37.  
    38.   Private Declare Function OpenProcessToken Lib "advapi32" _
    39.     (ByVal ProcessHandle As Integer, _
    40.     ByVal DesiredAccess As Integer, _
    41.     ByRef TokenHandle As Integer) As Integer
    42.  
    43.   Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" _
    44.     (ByVal lpSystemName As String, _
    45.     ByVal lpName As String, _
    46.     ByRef lpLuid As LUID) As Integer
    47.  
    48.   Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
    49.     (ByVal TokenHandle As Integer, _
    50.     ByVal DisableAllPrivileges As Integer, _
    51.     ByRef NewState As TOKEN_PRIVILEGES, _
    52.     ByVal BufferLength As Integer, _
    53.     ByRef PreviousState As TOKEN_PRIVILEGES, _
    54.     ByRef ReturnLength As Integer) As Integer
    55. #End Region

    Shutdown routines:
    VB Code:
    1. Friend Sub ForceReboot(Optional ByVal ForceShutdown As Boolean = False, Optional ByVal RestartMachine As Boolean = False)
    2.  
    3.     ' Force the computer to reboot
    4.  
    5.     Try
    6.       ' For the application to force the computer to reboot, SeShutdownPrivilege
    7.       ' must be given to this application's process
    8.       If EnableShutdownPrivileges() Then
    9.         ' Force the reboot
    10.         InitiateSystemShutdownEx("", "MESSAGE TEXT TO PUT INTO EVENT LOG.", 5, ForceShutdown, RestartMachine, SHTDN_REASON_MAJOR_OPERATINGSYSTEM Or SHTDN_REASON_MINOR_RECONFIG Or SHTDN_REASON_FLAG_PLANNED)
    11.       End If
    12.     Catch ex As Exception
    13.       MessageBox.Show("An error occurred while attempting to restart the computer." & _
    14.         vbCrLf & vbCrLf & "The error was: " & ex.Message, "Error Restarting Computer", MessageBoxButtons.OK, MessageBoxIcon.Error)
    15.     End Try
    16.   End Sub
    17.  
    18.   Private Function EnableShutdownPrivileges() As Boolean
    19.     Dim hdlProcessHandle As Integer
    20.     Dim hdlTokenHandle As Integer
    21.     Dim tmpLuid As LUID
    22.     Dim tkp As TOKEN_PRIVILEGES
    23.     Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    24.     Dim lBufferNeeded As Integer
    25.  
    26.     ' Get the current process
    27.     hdlProcessHandle = GetCurrentProcess()
    28.  
    29.     ' Get the Token for the current process
    30.     OpenProcessToken(hdlProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hdlTokenHandle)
    31.  
    32.     ' Get the Locally Unique Identifier (LUID)
    33.     LookupPrivilegeValue("", SE_SHUTDOWN_NAME, tmpLuid)
    34.  
    35.     With tkp
    36.       .PrivilegeCount = 1
    37.       .pLuid = tmpLuid
    38.       .Attributes = SE_PRIVILEGE_ENABLED
    39.     End With
    40.  
    41.     ' Enable the shutdown privilege
    42.     AdjustTokenPrivileges(hdlTokenHandle, 0, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
    43.  
    44.     Return True
    45.   End Function

    To shutdown the computer, make a call to the ForceReboot function. Note the two boolean parameters.

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