|
-
Mar 7th, 2006, 07:41 PM
#1
Thread Starter
Hyperactive Member
-
Mar 7th, 2006, 07:55 PM
#2
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:
Dim secondFrm as New Form2
secondFrm.Show()
HoraShadow
I do like the reward system. If you find that my post was useful, rate it.
-
Mar 7th, 2006, 08:02 PM
#3
Lively Member
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.
-
Mar 7th, 2006, 08:12 PM
#4
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
-
Mar 7th, 2006, 09:38 PM
#5
Thread Starter
Hyperactive Member
Re: Code Question's
Ok, Thank you for your help.
-
Mar 7th, 2006, 10:40 PM
#6
Fanatic Member
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:
#Region " InitiateSystemShutdownEX and related API Info "
' OpenProcessToken Constants
Private Const TOKEN_ADJUST_PRIVILEGES As Short = &H20S
Private Const TOKEN_QUERY As Short = &H8S
' LookupPrivilegeValue Constants
Private Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
Private Const SE_PRIVILEGE_ENABLED As Short = &H2S
' LookupPrivilegeValue Structures
Private Structure LUID
Dim UsedPart As Integer
Dim IgnoredForNowHigh32BitPart As Integer
End Structure
' AdjustTokenPrivileges Structures
Private Structure TOKEN_PRIVILEGES
Dim PrivilegeCount As Integer
Dim pLuid As LUID
Dim Attributes As Integer
End Structure
' InitiateSystemShutdownEx Constants
Private Const SHTDN_REASON_MAJOR_OPERATINGSYSTEM As Integer = &H20000
Private Const SHTDN_REASON_MINOR_RECONFIG As Integer = &H4
Private Const SHTDN_REASON_FLAG_PLANNED As Integer = &H80000000
Private Declare Function InitiateSystemShutdownEx Lib "advapi32" Alias "InitiateSystemShutdownExA" _
(ByVal lpMachineName As String, _
ByVal lpMessage As String, _
ByVal dwTimeout As Integer, _
ByVal bForceAppsClosed As Boolean, _
ByVal bRebootAfterShutdown As Boolean, _
ByVal dwReason As Integer) As Integer
Private Declare Function GetCurrentProcess Lib "kernel32" () As Integer
Private Declare Function OpenProcessToken Lib "advapi32" _
(ByVal ProcessHandle As Integer, _
ByVal DesiredAccess As Integer, _
ByRef TokenHandle As Integer) As Integer
Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, _
ByVal lpName As String, _
ByRef lpLuid As LUID) As Integer
Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
(ByVal TokenHandle As Integer, _
ByVal DisableAllPrivileges As Integer, _
ByRef NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Integer, _
ByRef PreviousState As TOKEN_PRIVILEGES, _
ByRef ReturnLength As Integer) As Integer
#End Region
Shutdown routines:
VB Code:
Friend Sub ForceReboot(Optional ByVal ForceShutdown As Boolean = False, Optional ByVal RestartMachine As Boolean = False)
' Force the computer to reboot
Try
' For the application to force the computer to reboot, SeShutdownPrivilege
' must be given to this application's process
If EnableShutdownPrivileges() Then
' Force the reboot
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)
End If
Catch ex As Exception
MessageBox.Show("An error occurred while attempting to restart the computer." & _
vbCrLf & vbCrLf & "The error was: " & ex.Message, "Error Restarting Computer", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Function EnableShutdownPrivileges() As Boolean
Dim hdlProcessHandle As Integer
Dim hdlTokenHandle As Integer
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Integer
' Get the current process
hdlProcessHandle = GetCurrentProcess()
' Get the Token for the current process
OpenProcessToken(hdlProcessHandle, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hdlTokenHandle)
' Get the Locally Unique Identifier (LUID)
LookupPrivilegeValue("", SE_SHUTDOWN_NAME, tmpLuid)
With tkp
.PrivilegeCount = 1
.pLuid = tmpLuid
.Attributes = SE_PRIVILEGE_ENABLED
End With
' Enable the shutdown privilege
AdjustTokenPrivileges(hdlTokenHandle, 0, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
Return True
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|