Hello world, I would be grateful if you helped me. I've built a text editing application and I would like it to load values/user settings from the Windows Registry. Here's my code but it is somehow wrong (I believe it whatsoever has to do with wrong syntax but it could be something else);

My form's load event. I could have also posted the code I used to save the settings but it doesn't matter anyway because I ensured it is correct. Still, if you'd like to see that code too, just reply back.

Const c_strKeyName As String = "HKEY_CURRENT_USER\Software\JimmyIliadisInc\NoteEditorPro\"
        RichTextBox1.Font = CStr(My.Computer.Registry.GetValue(c_strKeyName, "TextFont", ""))
        WordWrapToolStripMenuItem.CheckState = CStr(My.Computer.Registry.GetValue(c_strKeyName, "WordWrap", ""))
        RichTextBox1.ForeColor = CStr(My.Computer.Registry.GetValue(c_strKeyName, "TextColor", ""))
        Me.Text = "Untitled.txt - NoteEditor Pro"
        Saved = False

OK, here's a detailed explanation. I've got three settings that I want to load. The first one has to do with the font selected before the application was closed and the second has to do with the check state of a menu item (and specifically "Word wrap"). The problem is that this code throws an exception and the settings won't be loaded. Except when I change RichTextBox1.Font to RichTextBox1.Text so it will load ONLY the first setting in plain text (something that does not come in handy). About the third one, I think you can undestand what it's all about. Just wanting to load the text color that was used before the application was closed (doesn't work either). I hope I made things clear. Thanks in advance.

Recommended Answers

All 8 Replies

Font and Color property aren't text (string)

RichTextBox1.Font=new Font(fontName,10)
RichTextBox1.ForeColor=Color.fromName(txtColor)

Font and Color property aren't text (string)

RichTextBox1.Font=new Font(fontName,10)
RichTextBox1.ForeColor=Color.fromName(txtColor)

This wasn't quite what I was looking for. I don't need to set those values myself. Those values/properties should be loaded from the Windows Registry. For example, please take a look at the code I used to save the settings to the Registry;

Check state of "Word wrap" menu item;

If WordWrapToolStripMenuItem.Checked = True Then
            RichTextBox1.WordWrap = True
        ElseIf WordWrapToolStripMenuItem.Checked = False Then
            RichTextBox1.WordWrap = False
        End If
        My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\JimmyIliadisInc\NoteEditorPro\", "WordWrap", WordWrapToolStripMenuItem.CheckState)

Font settings;

Try
            If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                RichTextBox1.Font = FontDialog1.Font
                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\JimmyIliadisInc\NoteEditorPro\", "TextFont", RichTextBox1.Font)
            End If
        Catch objException As System.ArgumentException
            NotTrueType.ShowDialog()
        End Try

Color settings;

If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            RichTextBox1.ForeColor = ColorDialog1.Color
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\JimmyIliadisInc\NoteEditorPro\", "TextColor", RichTextBox1.ForeColor)
        End If

I hope this made things clear for you. Looking forward to someone that can actually help me.

Regards,
TSG

actually what adatapost posted is exactlly what you need.
You save these settings as strings in your registry!
to assign these strings to your code you have to cast it into the types.

iE: u save the color as "Red" in your registry. this is a string so you have to get this string from the registry and cast it to a valid color like Color.fromName("here the string from the registry")

Didn't work at last. Can you just send me the whole code with the strings as I set them? I would be really really grateful if you did that. Please, I need to finish this application. Thank you for your help anyway...

Somebody help me, I'm still doing this project!

>Can you just send me the whole code with the strings as I set them?

TechSupportGeek, post your code here. (you can attach your project zip).

Public Class Form1
    Dim Saved As Boolean = False

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If RichTextBox1.Text Is "" Then
            Application.Exit()
        ElseIf RichTextBox1.Text IsNot Nothing Then
            ExitDocumentWithoutSaving.ShowDialog()
        End If
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Const c_strKeyName As String = "HKEY_CURRENT_USER\Software\JimmyIliadisInc\NoteEditorPro\"
    End Sub

    Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
        RichTextBox1.Undo()
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        RichTextBox1.Redo()
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
        RichTextBox1.Cut()
    End Sub

    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem.Click
        RichTextBox1.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem.Click
        RichTextBox1.Paste()
    End Sub

    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub SelectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem.Click
        RichTextBox1.SelectAll()
    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
        RichTextBox1.DeselectAll()
    End Sub

    Private Sub FindToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindToolStripMenuItem.Click
        FindText.ShowDialog()
    End Sub

    Private Sub UndoToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem1.Click
        RichTextBox1.Undo()
    End Sub

    Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click
        RichTextBox1.Redo()
    End Sub

    Private Sub CutToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem1.Click
        RichTextBox1.Cut()
    End Sub

    Private Sub CopyToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyToolStripMenuItem1.Click
        RichTextBox1.Copy()
    End Sub

    Private Sub PasteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripMenuItem1.Click
        RichTextBox1.Paste()
    End Sub

    Private Sub DeleteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem1.Click
        RichTextBox1.Clear()
    End Sub

    Private Sub SelectAllToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectAllToolStripMenuItem1.Click
        RichTextBox1.SelectAll()
    End Sub

    Private Sub DeselectAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeselectAllToolStripMenuItem.Click
        RichTextBox1.DeselectAll()
    End Sub

    Private Sub TextDocumentToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextDocumentToolStripMenuItem.Click
        If RichTextBox1.Text Is "" Then
            Me.Text = "Untitled.txt - NoteEditor Pro"
            RichTextBox1.Clear()
        ElseIf RichTextBox1.Text IsNot Nothing Then
            If CreateNewProjectWithoutSaving.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.Text = "Untitled.txt - NoteEditor Pro"
                RichTextBox1.Clear()
            End If
        End If
        Saved = False
    End Sub

    Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
        Try
            If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                RichTextBox1.Font = FontDialog1.Font
                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\JimmyIliadisInc\NoteEditorPro\", "TextFont", RichTextBox1.Font)
                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\JimmyIliadisInc\NoteEditorPro\", "TextSize", RichTextBox1.Font.Size)
            End If
        Catch objException As System.ArgumentException
            NotTrueType.ShowDialog()
        End Try
    End Sub

    Private Sub TextColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextColorToolStripMenuItem.Click
        If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            RichTextBox1.ForeColor = ColorDialog1.Color
            My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\JimmyIliadisInc\NoteEditorPro\", "TextColor", RichTextBox1.ForeColor)
        End If
    End Sub

    Private Sub WordWrapToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WordWrapToolStripMenuItem.Click
        If WordWrapToolStripMenuItem.Checked = True Then
            RichTextBox1.WordWrap = True
        ElseIf WordWrapToolStripMenuItem.Checked = False Then
            RichTextBox1.WordWrap = False
        End If
        My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\JimmyIliadisInc\NoteEditorPro\", "WordWrap", WordWrapToolStripMenuItem.CheckState)
    End Sub

    Private Sub HourDateToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HourDateToolStripMenuItem.Click
        RichTextBox1.Text = RichTextBox1.Text + DateTime.Now
    End Sub

    Private Sub ReplaceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceToolStripMenuItem.Click
        ReplaceText.ShowDialog()
    End Sub

    Private Sub SettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SettingsToolStripMenuItem.Click
        SettingsForm.ShowDialog()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Application.Exit()
    End Sub

    Private Sub NoteEditorProProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NoteEditorProProjectToolStripMenuItem.Click
        If RichTextBox1.Text Is "" Then
            Me.Text = "Untitled.ntp - NoteEditor Pro"
            RichTextBox1.Clear()
        ElseIf RichTextBox1.Text IsNot Nothing Then
            If CreateNewProjectWithoutSaving.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.Text = "Untitled.ntp - NoteEditor Pro"
                RichTextBox1.Clear()
            End If
        End If
        Saved = False
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        If (OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
            RichTextBox1.LoadFile(OpenFileDialog1.FileName, RichTextBoxStreamType.PlainText)
            Me.Text = "" & OpenFileDialog1.FileName & " - NoteEditor Pro"
        End If
    End Sub

    Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsToolStripMenuItem.Click
        If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
            Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
            Saved = True
        End If
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        If Saved = False Then
            If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
                Me.Text = "" & SaveFileDialog1.FileName & " - NoteEditor Pro"
                Saved = True
            ElseIf Saved = True Then
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.TextTextOleObjs)
            End If
        End If
    End Sub
End Class

Just letting you know that I posted my code as requested. Hope you buddy can help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.