Solved

saving and recalling text in note card script

Posted on 2004-09-14
8
224 Views
Last Modified: 2010-04-23
Two different solutions for the question below  in    Q_21123047

 **********************************************
I'm trying to create a personal reminder feature as part of a program. Need help on

Double click on form which
produces a message box with a textbox for title
giving a small box on the form with the title
double click on title brings up a text area to make notes related to subject

all the small boxes visible on the form with capabality of being dragged and having text edited.
 **************************************************************


Next question.  How do I save to notes1.txt, then rerun with notes1.txt as input,add more
notes, save as  notes2.txt and so on and so on.
0
Comment
Question by:aijohn
  • 5
  • 2
8 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
sorry it took so long aijohn, had to work too :-)

There a few alterations to make this work. Here is the code.

the module is the same as is the mynote class

<Serializable()> Public Class notescontainer

    'Public notes As ArrayList
    Public notes As New Hashtable
    Public Sub AddNote(ByVal TT As String, ByVal ttt As String)
        Dim s As New mynote(TT, ttt)
        notes.Add(s.Title, s)
    End Sub

    Public Function ReturnNoteText(ByVal tt As String) As String
            Return CType(notes.Item(tt), mynote).Notetext
    End Function

    Public Sub SaveXml(ByVal Path As String)
        Dim fStream As System.IO.FileStream
        Dim myFormat As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter
        Try
            fStream = New System.IO.FileStream(Path, IO.FileMode.Create, IO.FileAccess.Write)
            myFormat.Serialize(fStream, notes)
            fStream.Close()
        Catch ex As Exception
            MsgBox("Error saving: " & ex.Message)
        Finally
            If Not (fStream Is Nothing) Then fStream.Close()
        End Try
    End Sub

    Public Shared Function LoadXML(ByVal Path As String) As Hashtable
        Dim fStream As System.IO.FileStream
        Dim myFormat As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter
        Dim tmp As Hashtable
        Try
            fStream = New System.IO.FileStream(Path, IO.FileMode.Open, _
                IO.FileAccess.Read)
            tmp = CType(myFormat.Deserialize(fStream), Hashtable)
        Catch ex As Exception
            MsgBox("Error opening: " & ex.Message)
            tmp = Nothing
        Finally
            If Not (fStream Is Nothing) Then fStream.Close()
        End Try
        Return tmp

    End Function
End Class

Add a load button to your form that containes the notes and but this code in it

   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim tmp As Hashtable
        tmp = notescontainer.LoadXML("c:\ttt.xml")
        For Each x As String In tmp.Keys
            Dim lb As New Label
            lb.SendToBack()
            lb.BorderStyle = BorderStyle.Fixed3D

            lb.Text = x
            lb.Top = 10
            lb.Left = 10
            lb.Height = 50
            lb.Width = 50
            AddHandler lb.DoubleClick, AddressOf labelDclick
            AddHandler lb.MouseDown, AddressOf lbmousedown
            AddHandler lb.MouseUp, AddressOf lbmouseup
            AddHandler lb.MouseMove, AddressOf lbmousemove
            Me.Controls.Add(lb)
            c.notes = tmp
        Next
    End Sub

Add a save button to your form that containes the notes and but this code in it

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        c.SaveXml("c:\ttt.xml")
    End Sub

Then you can save and load notes


If you need any additional help with this let me know
0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Ofcourse it still needs some bells and whistles but I leave that up to you.

One thing for instance is when you load the xml, all the notes will be on top of each other. so you need to alter the left and top properties of your labels in the loop
0
 
LVL 25

Assisted Solution

by:RonaldBiemans
RonaldBiemans earned 250 total points
Comment Utility
sorry one other thing, these should be changed aswell

 Private Sub Form2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
        Dim str As String = InputBox("give title", "Title")
        Dim lb As New Label
        lb.SendToBack()
        lb.BorderStyle = BorderStyle.Fixed3D

        lb.Text = str
        lb.Top = 10
        lb.Left = 10
        lb.Height = 50
        lb.Width = 50
        AddHandler lb.DoubleClick, AddressOf labelDclick
        AddHandler lb.MouseDown, AddressOf lbmousedown
        AddHandler lb.MouseUp, AddressOf lbmouseup
        AddHandler lb.MouseMove, AddressOf lbmousemove
        c.AddNote(lb.Text, "")
        Me.Controls.Add(lb)
    End Sub

   Private Sub labelDclick(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim tb As New Form3
        tb.Top = CType(sender, Label).Top + 10
        tb.Left = CType(sender, Label).Top + 10
        tb.Text = CType(sender, Label).Text
        tb.RichTextBox1.Text = c.ReturnNoteText(CType(sender, Label).Text)
        tb.Show()
    End Sub
0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Pff, I think I just wrote you a complete application ;-)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
Comment Utility
Here is my take on it.  Just paste ALL of the code below over the default form code in a new project.

Regards,

Idle_Mind


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.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.AutoScroll = True
        Me.ClientSize = New System.Drawing.Size(400, 302)
        Me.Name = "Form1"
        Me.Text = "Notecard System"

    End Sub

#End Region

    <Serializable()> Private Class Note

        <NonSerialized()> Public sourceLabel As Label
        Public name As String
        Public message As String
        Public labelSize As Size
        Public labelLocation As Point

    End Class

    Private clickX As Integer
    Private clickY As Integer
    Private Notes As ArrayList = New ArrayList
    Private notesFile As String = Application.StartupPath

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not notesFile.EndsWith("\") Then
            notesFile = notesFile & "\"
        End If
        notesFile = notesFile & "Notes.dat"

        If System.IO.File.Exists(notesFile) Then
            Dim fs As New System.IO.FileStream(notesFile, System.IO.FileMode.OpenOrCreate)
            Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

            Try
                Notes = DirectCast(formatter.Deserialize(fs), ArrayList)
                Dim l As Label
                Dim n As Note
                For Each n In Notes
                    l = New Label
                    l.Size = n.labelSize
                    l.Location = n.labelLocation
                    l.Text = n.name
                    n.sourceLabel = l
                    l.BorderStyle = BorderStyle.FixedSingle
                    l.TextAlign = ContentAlignment.MiddleCenter
                    l.Tag = n
                    AddHandler l.DoubleClick, AddressOf Me.Label1_DoubleClick
                    AddHandler l.MouseDown, AddressOf Me.Label1_MouseDown
                    AddHandler l.MouseDown, AddressOf Me.Label1_MouseUp
                    AddHandler l.MouseMove, AddressOf Me.Label1_MouseMove
                    Me.Controls.Add(l)
                Next
            Catch x As System.Runtime.Serialization.SerializationException
                MsgBox(x.Message, MsgBoxStyle.Critical, "Notes Deserialize Failed")
            End Try

            fs.Close()
        End If
    End Sub

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        clickX = e.X
        clickY = e.Y
    End Sub

    Private Sub Form1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
        Dim title As String = InputBox("Notecard Title:", "New Notecard", "Reminder")
        If title.Length > 0 Then
            Dim l As New Label
            Dim g As Graphics = l.CreateGraphics
            Dim s As SizeF = g.MeasureString("_" & title & "_", l.Font)
            g.Dispose()
            l.Text = title
            l.TextAlign = ContentAlignment.MiddleCenter
            l.Width = s.Width
            l.Height = s.Height
            l.BorderStyle = BorderStyle.FixedSingle
            l.Location = New Point(clickX, clickY)
            AddHandler l.DoubleClick, AddressOf Me.Label1_DoubleClick
            AddHandler l.MouseDown, AddressOf Me.Label1_MouseDown
            AddHandler l.MouseDown, AddressOf Me.Label1_MouseUp
            AddHandler l.MouseMove, AddressOf Me.Label1_MouseMove
            Me.Controls.Add(l)
            Dim n As Note = New Note
            n.sourceLabel = l
            n.labelSize = l.Size
            n.labelLocation = l.Location
            n.name = l.Text
            n.message = ""
            Notes.Add(n)
            l.Tag = n
            Label1_DoubleClick(l, Nothing)
        End If
    End Sub

    Private Sub Label1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
        If TypeOf sender.tag Is Note Then
            Dim f As New Form
            f.Size = New Size(300, 300)
            f.Text = sender.Text
            Dim tb As New TextBox
            tb.Name = "Notes"
            tb.Location = New Point(0, 0)
            tb.Dock = DockStyle.Fill
            tb.Multiline = True
            tb.ScrollBars = ScrollBars.Both
            Dim n As Note = CType(sender.tag, Note)
            tb.Text = n.message
            tb.SelectionStart = tb.Text.Length
            sender.tag = f
            f.Tag = n
            f.Controls.Add(tb)
            AddHandler f.Closing, AddressOf Me.f_Closing
            f.Show()
        Else
            Dim f As Form = CType(sender.tag, Form)
            If f.WindowState = FormWindowState.Minimized Then
                f.WindowState = FormWindowState.Normal
            End If
            f.Focus()
        End If
    End Sub

    Private Sub Label1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        clickX = e.X
        clickY = e.Y
    End Sub

    Private Sub Label1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = MouseButtons.Right Then
            Dim n As Note
            If TypeOf sender.tag Is Form Then
                Dim f As Form = CType(sender.tag, Form)
                n = CType(f.Tag, Note)
                f.Close()
                f.Dispose()
            Else
                n = CType(sender.tag, Note)
            End If
            Notes.Remove(n)
            Me.Controls.Remove(sender)
        End If
    End Sub

    Private Sub Label1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = MouseButtons.Left Then
            Dim l As Label = CType(sender, Label)
            l.Location = New Point(l.Left + (e.X - clickX), l.Top + (e.Y - clickY))
            Dim n As Note
            If TypeOf sender.tag Is Form Then
                Dim f As Form = CType(sender.tag, Form)
                n = CType(f.Tag, Note)
            Else
                n = CType(sender.tag, Note)
            End If
            n.labelLocation = l.Location
        End If
    End Sub

    Private Sub f_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Dim f As Form = CType(sender, Form)
        Dim ctl As Control
        Dim tb As TextBox
        For Each ctl In f.Controls
            If ctl.Name.Equals("Notes") Then
                tb = CType(ctl, TextBox)
                Dim n As Note = CType(f.Tag, Note)
                n.message = tb.Text
                Dim l As Label = CType(n.sourceLabel, Label)
                l.Tag = n
                Exit Sub
            End If
        Next
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim fs As New System.IO.FileStream(notesFile, System.IO.FileMode.Create)
        Dim formatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter

        Dim l As Label
        Dim f As Form
        Dim n As Note
        For Each n In Notes
            l = CType(n.sourceLabel, Label)
            If TypeOf l.Tag Is Form Then
                f = CType(l.Tag, Form)
                f.Close()
            End If
        Next

        Try
            formatter.Serialize(fs, Notes)
        Catch x As System.runtime.Serialization.SerializationException
            MsgBox(x.Message, MsgBoxStyle.Critical, "Notes Serialization Failed")
        End Try

        fs.Close()
    End Sub

End Class
0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Hi Idle_mind,
Correction, I think WE just wrote him a complete application ;-)
0
 

Author Comment

by:aijohn
Comment Utility
Thanks again to both of you for a rapid intro to vb.net.  Unfortunately I will probably be spending
most of the day on something else not half as interesting so it will probably be later tonight before
I have a chance to mess up your beautiful code.

You are doing a great job  of accelerating the learning curve.

aijohn
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
It was a fun little app to write.

I'm sure many would be appalled at my use of the Tag property, an inner private class and *gasp* binary serialization.

I've never actually tried XML serialization...need to try that out...   =)

I think the code is easy enough to understand though and between your submission and mine, aijohn has gotten a good look at two different approaches to the same problem.

Regards,

{pseudocode} / {Time} ± ¼*ƒ(Me.Thoughts÷3)^² = Idle »(°_°)« Mind
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now