|
Subject:
|
vb 2003 to 2005 convert gone wrong
|
|
Posted By:
|
mr_kane
|
Post Date:
|
8/5/2007 12:19:12 PM
|
I have a program that used to work with my old version of visual studio but now i have upgraded to 2005 and Vista, but the programs stopped working.
The error i am getting is: System.InvalidCastException was unhandled Conversion from string "" to type 'Boolean' is not valid.
Here's the code
Public Class Form1 Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New() MyBase.New()
'This call is required by the Windows Form Designer. InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub
'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents btnMain As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.btnMain = New System.Windows.Forms.Button Me.SuspendLayout() ' 'btnMain ' Me.btnMain.Location = New System.Drawing.Point(24, 16) Me.btnMain.Name = "btnMain" Me.btnMain.Size = New System.Drawing.Size(240, 23) Me.btnMain.TabIndex = 0 Me.btnMain.Text = "Give me my Task Manager beee-ach" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 54) Me.Controls.Add(Me.btnMain) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False)
End Sub
#End Region
Private Sub btnMain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMain.Click 'read registry If CBool(ReadWriteCURegistry("Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", False)) = False Then 'change registry ReadWriteCURegistry("Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", True, "1") btnMain.Text = "Enable task manager" Else ReadWriteCURegistry("Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", True, "00") btnMain.Text = "Disable task manager" End If
End Sub Private Function ReadWriteCURegistry(ByVal subkey As String, Optional ByVal value As String = "", Optional ByVal openToWrite As Boolean = False, Optional ByVal writetokey As String = "") As String Dim regKey As Microsoft.Win32.RegistryKey Dim key As Boolean
Try regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(subkey, openToWrite) If openToWrite Then regKey.SetValue(value, writetokey) End If key = regKey.GetValue(value) regKey.Close() Return key Catch ex As Exception Return Nothing End Try End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If CBool(ReadWriteCURegistry("Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", False)) = False Then btnMain.Text = "Disable task manager" Else btnMain.Text = "Enable task manager" End If End Sub End Class
I am getting this error on this line: If CBool(ReadWriteCURegistry("Software\Microsoft\Windows\CurrentVersion\Policies\System", "DisableTaskMgr", False)) = False Then
I'm unsure how to deal with the issue, i've heard about the convert class but don't think i can use this here. Any help is greatly accepted.
Kane
|
|
Reply By:
|
Imar
|
Reply Date:
|
8/5/2007 2:14:13 PM
|
Hi there,
You may want to set Option Explicit and Option Strict on to have the compiler tell you about these problems at compile time. Take a look at this:
Private Function ReadWriteCURegistry(ByVal subkey As String, Optional ByVal value As String = "", Optional ByVal openToWrite As Boolean = False, Optional ByVal writetokey As String = "") As String
Dim key As Boolean
End Function
You declare the method as a String, yet to have it return a Boolean and finally you treat the result as a Boolean. Why not declare the method as a Boolean method?
Private Function ReadWriteCURegistry(ByVal subkey As String, Optional ByVal value As String = "", Optional ByVal openToWrite As Boolean = False, Optional ByVal writetokey As String = "") As Boolean
Imar
--------------------------------------- Imar Spaanjaars http://Imar.Spaanjaars.Com Everyone is unique, except for me. Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
|
|
Reply By:
|
mr_kane
|
Reply Date:
|
8/5/2007 4:35:08 PM
|
Many thanks Imar, i can't believe it was that simple.
Kane
|
|