13 Replies - 3332 Views - Last Post: 26 October 2011 - 08:38 AM Rate Topic: -----

#1 mutsop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-October 11

bind list to textbox

Posted 18 October 2011 - 03:41 AM

Hi,

I'm creating a mindmapper application (as a test) and have a difficulty to manage created mindlabels and their childlabels.

So I thought of binding a list(Of textbox) to a textbox. But how can this be done?
Or do you have another idea? :)

Here is the code I have so far for the application:
Public Class Main

#Region "Methods"
    Private WithEvents labelMap As TextBox
    Private copyText As String
    Private menuPosition As Point

    Private Sub CreateLabel(Optional ByVal text As String = "")
        labelMap = New TextBox
        With labelMap
            .Location = New Point(menuPosition.X, menuPosition.Y)
            .Multiline = True
            .AutoSize = True
            .ContextMenuStrip = MenuLabel
            Me.Controls.Add(labelMap)
            AddHandler labelMap.TextChanged, AddressOf ChangeHeight
            AddHandler labelMap.MouseMove, AddressOf MoveLabel
            .Focus()
            .Text = text
        End With
    End Sub

    Private Sub DeleteLabel(ByVal sender As Object)
        labelMap = GetMenuControl(sender)
        labelMap.Dispose()
    End Sub

    Private Sub CopyLabel(ByVal sender As Object)
        labelMap = GetMenuControl(sender)
        copyText = labelMap.Text
        MenuPasteLabel.Enabled = True
    End Sub

    Private Sub PasteLabel()
        CreateLabel(copyText)
    End Sub

    Private Sub AddChild(ByVal sender As Object)
        CreateLabel()
        Dim startLocation As New Point(GetMenuControl(sender).Location)
        Dim endLocation As New Point(labelMap.Location)

        ControlPaint.DrawReversibleLine(startLocation, endLocation, Color.Blue)
    End Sub

    Private Sub ClearMap()
        For Each TextBox As Control In Me.Controls
            If TypeOf TextBox Is TextBox Then
                TextBox.Dispose()
            End If
        Next
    End Sub

    Private Sub ChangeHeight(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'nog aan te passen
        labelMap.Height = labelMap.Lines.Count * 20
    End Sub

    Private Sub MoveLabel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        menuPosition = New Point(Me.PointToClient(MousePosition))
        If e.Button = MouseButtons.Left Then
            labelMap.Location = New Point(menuPosition)
        End If
    End Sub

    Private Function GetMenuControl(ByVal sender As Object) As Control
        Dim Menu As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
        Dim MenuControl As ContextMenuStrip = CType(Menu.Owner, ContextMenuStrip)

        Return MenuControl.SourceControl
    End Function
#End Region

#Region "Event Handlers"
    Private Sub MenuNewLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuNewLabel.Click
        CreateLabel()
    End Sub

    Private Sub MenuDeleteLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuDeleteLabel.Click
        DeleteLabel(sender)
    End Sub

    Private Sub MenuCopyLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuCopyLabel.Click
        CopyLabel(sender)
    End Sub

    Private Sub MenuPasteLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuPasteLabel.Click
        PasteLabel()
    End Sub

    Private Sub MenuAddChild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuAddChild.Click
        AddChild(sender)
    End Sub

    Private Sub MenuClearMap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuClearMap.Click
        ClearMap()
    End Sub

    Private Sub Main_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        If e.Button = MouseButtons.Right Then
            menuPosition = New Point(Me.PointToClient(MousePosition))
        End If
    End Sub
#End Region
End Class



Requirements:
Form
2 ContextMenuStrips
-1: FormLabel: "New Label", "Clear Map"
-2: MenuLabel: "Add Child", "Copy", "Delete"


Thanks in advance
Peter

Is This A Good Question/Topic? 0
  • +

Replies To: bind list to textbox

#2 mutsop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-October 11

Re: bind list to textbox

Posted 19 October 2011 - 04:23 AM

Hi,

A few days ago I posted a thread about how to bind a list to textbox.
Now when I checked my code for a second time I was kind of ashamed of how bad it was written.

So to ease the coding I make use of business classes and an interface.

To recapitulate:
I'm trying to create a mindmapper application (to write a bunch of keywords onto an empty field)
It should be able to add child keywords to parents, parents to their own childs and a loop onto its own.

example:
Posted Image

Here are my classes:
Keyword.vb:
Public Class Keyword
    Private _Id As Int32
    Public Name As String
    Public Childs As IList(Of Keyword)

    Public Sub New(ByVal id As Int32)
        _Id = id
    End Sub

    Public ReadOnly Property Id As Int32
        Get
            Return _Id
        End Get
    End Property

    Public Sub addChild(ByVal child As Keyword)
        Childs.Add(child)
    End Sub
End Class



Map.vb:
Public Class Map
    Public keywords As IList(Of Keyword)

    Public Sub AddKeyword()
        keywords.Add(New Keyword(nextId))
    End Sub

    Public Sub RemoveKeyword(ByVal keyword As Keyword)
        keywords.Remove(keyword)
    End Sub

    Private Function nextId() As Int32
        Return keywords.Last().Id + 1
    End Function
End Class



Now how do I connect these business classes to an interface so Keyword is an at runtime created textbox and Map is a form?

Kind regards
Peter
Was This Post Helpful? 0
  • +
  • -

#3 mutsop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-October 11

Re: bind list to textbox

Posted 24 October 2011 - 04:43 AM

Anyone able to help me on this?
Was This Post Helpful? 0
  • +
  • -

#4 mutsop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-October 11

Re: bind list to textbox

Posted 25 October 2011 - 07:39 AM

Weird, no one knows?
I mean if the question isn't clear, please do tell me.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,289
  • Joined: 12-June 08

Re: bind list to textbox

Posted 25 October 2011 - 07:44 AM

Let's not create duplicate threads. If you cleaned up your code - congrats... but the problem is still the same keep it all together.

Additionally - I have no idea what you mean by this:

Quote

Now how do I connect these business classes to an interface so Keyword is an at runtime created textbox and Map is a form?


Would you clarify what you are trying to accomplish?
Was This Post Helpful? 0
  • +
  • -

#6 mutsop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-October 11

Re: bind list to textbox

Posted 26 October 2011 - 01:23 AM

Sorry for the second thread :)

I've been thinking a bit more on how I might approach this problem in a decent way.
Now I thought of creating (in the form) a ControlCollection.

Now I'm not sure if my next approach is any good but here it goes:
Using an Observer pattern which would create/update/delete a textbox when creating a new keyword-Object.

Why I thought Observer pattern? Well I got some information from this tutorial:
252769-design-patterns-observer-pattern

Quote

Typical problem domain:
- when an object needs to notify other object(s) that are interested in its state of a change


Any good? or do would you suggest me another approach?

Regards
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,289
  • Joined: 12-June 08

Re: bind list to textbox

Posted 26 October 2011 - 06:45 AM

I am still not sure what you are trying to accomplish. If you could explain that I might be of more help.
Was This Post Helpful? 0
  • +
  • -

#8 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 498
  • View blog
  • Posts: 3,317
  • Joined: 12-January 10

Re: bind list to textbox

Posted 26 October 2011 - 07:07 AM

are you trying to create a map in a new form load according to a keyword?
Was This Post Helpful? 0
  • +
  • -

#9 mutsop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-October 11

Re: bind list to textbox

Posted 26 October 2011 - 07:17 AM

K, let's start from the beginning :)

I'm trying to create a mindmap application (Check link)
Each of the keywords you'll be adding on an empty form will be textboxes (so you can easily change your keyword).

So what I did was create a keyword class and map class.
The keyword class will set the object (id, name and list of child keywords) of each keyword you add.
The map class will contain all the keywords, so you can easily save/load your whole mindmap.

But to connect the visual aspect of my application (textboxes and later on connectionlines between the parent keyword and child keyword), thats what I'm trying to figure out.

So for this, I thought of adding a ControlCollection to my form to keep track of my textboxes and a observer class to tell my keyword object when a textbox has changed (.text).


I hope this is clear :)
thanks in advance
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,289
  • Joined: 12-June 08

Re: bind list to textbox

Posted 26 October 2011 - 07:36 AM

Hmm.. so you are just recreating the 'tree view' object from the toolbox?

Treating it like you would a node structure like a linked list, queue, stack, or tree is about right. You have a node class that tracks the value and who the children are. Presumably it would also track the location of the textbox (to be drawn). Much like the binary tree tutorial I wrote, I would advocate the "map" class deal with the more high level (and not say a collection of all keywords). It would handle inserting, deleting, traversing, saving, and loading. Drawing the lines and what not might be best on the node level.

Loading and saving of the tree might be best in an XML file... as long as you have a consistent traversal path (in order vs preorder vs post order etc) getting the data out and in isn't bad.. and you would save the value and the textbox location since the order of the xml file takes care of making the children.

I think I might fiddle with this for a bit and see how far I can run with it.
I would start off as a binary tree first and go from there.
Was This Post Helpful? 0
  • +
  • -

#11 mutsop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-October 11

Re: bind list to textbox

Posted 26 October 2011 - 07:50 AM

You are right it does actually fit as a node structure, I should have thought about that :)

The map class will have more functions later on as you said... But I haven't implemented them as I didn't know how to keep the connection between my textboxes and keyword class. For example you have 5 textboxes on the form, how do you make sure that each of these textboxes will change a specific keyword object?

as for saving and loading I first thought of serializing the list of keywords with their properties. But Xml might do the trick too, although I haven't worked with xml before.

But when loading, I should recreate each of the object again I suppose?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,289
  • Joined: 12-June 08

Re: bind list to textbox

Posted 26 October 2011 - 08:12 AM

Quote

connection between my textboxes and keyword class.

Each node would have a textbox control.. you would need to traverse each node (after load) to add each textbox control to the 'me.controls' in the location you have specified.. and also that each have their own text value set.

Quote

But when loading, I should recreate each of the object again I suppose?

That sort of goes with out saying when you are moving data in and out of a file. ;)

With minor modifications ot my binary tree I am able to specify a root node and location... and each subsequent insert creates a location based off right/left movement and a set down ward location... and then draws each string... also it calculates the top center and bottom center of each drawn item and draws a line to the children.

Nifty!

Attached Image

Though the tweaking on how far to push each left and right may need to occur to stop visual overlaps.. heh..

Yeah.. I would have to calculate the max levels and multiply out appropriately.. a task for another day.
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,289
  • Joined: 12-June 08

Re: bind list to textbox

Posted 26 October 2011 - 08:33 AM

With a bit more fiddling I was able to drop unique textboxes in each node... perculate the 'me.controls' to each insert (so the form gui has access to them), and modify the draw lines for the height and width change. Go me! This was all modifications of my binary tree tutorial... so it shouldn't be that hard for you to push and pull it around.

Attached Image
Was This Post Helpful? 0
  • +
  • -

#14 mutsop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 11-October 11

Re: bind list to textbox

Posted 26 October 2011 - 08:38 AM

Great thx!

I'll check your code and let you know how it went next week.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1