0

So I'm creating a LinkedList in C# winforms, and now I want to display the list visually when adding, removing and searching for node values. I use 3 button controls, one for adding nodes, removing nodes and the last for searching for a nodes value. Then, I have a textbox that should read in a int value from user (ex. 10), and then display that value as a linked list, in a Listbox. So, Ive created a Node class and a linkedlist class. The Node class holds a value and a reference to the next node:

class Node
{
    public int value;
    public Node next;

    public int Value
    {
        get { return this.value; }
        set { this.value = value; } 
    }

    public Node Next
    {
        get;
        set;
    }

    public Node()
    {
        this.Next = null;
        this.value = 0;
    }
}

and a LinkedList class with all my methods (I will only write my add method, so It wont be too much code)

public void Add(int val)
    {
       Node newNode = new Node();
       newNode.value = val;

       if (head == null)
           head = newNode;

       if (current.Next == null)
       {
           current.Next = newNode;
           current = newNode;
       }
       size++;
   }

So, Now what imm trying to Do is to write some code so that when I input a int number in the textbox, it should appear in the Listbox as a LinkedList (with my add method from my LinkedList class)

I tried to do this on the event handler for my Add button:

LinkedList l1 = new LinkedList();
l1.Add(textBox1);

But clearly that is not even logical. Could someone please point me to the right direction here?

2 Answers 2

1

If your ListBox is called listBox1 you add to it like this:

listBox1.Items.Add(something);

You can add strings directly but you probably want to add one of your LinkedList objects. You can add it also like above..

listBox1.Items.Add(l1 );

..but it won't show anything useful until you add a ToString method to your LinkedList class:

public override string ToString()
{
    return "value : " + this.value; // maybe you want to display more here?
}

It is up to you to decide what you want displayed hee; maybe indicators if the links are set..? I also suggest adding one or more constructors to your LinkedList class to set at least the value right away:

public Node(int value_)
{
    this.Next = null;
    this.value = value_;
}

Then your above code could turn to:

LinkedList l1 = new LinkedList(textBox1.Text);
listView1.Items.Add(l1);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot for a perfect answer! But since im only handling integers on my list, will I still need to use the override ToString function?
The ToString method is always needed when you want to get control over how instances of a class should be displayed. This is independent of the content; the Listbox can't even display the numbers, let alone format them, unless you help it out with a ToString method. (Unless you simply add the numbers (turnerd to strings behind the scene), but these then are not objects you could use for much..) - If you are happy with the answer you may want to accept it (by clicking on the checkmark at the top left.. If you have more questions, feel free to ask!
1

You probably want

l1.Add(Int32.Parse(textBox1.Text))

textBox1 is an object representing the textbox, not the value stored in it (which is the Text property). TextBoxs store their value in string format, hence the use of Int32.Parse() (which converts from string (or other values) to int.

It should be noted that Int32.Parse is not the safest way to do this, and if you want to verify that the value is truly an int you should use Int32.TryParse()

5 Comments

And to show that value on my listbox, should i then Write some code on my listbox event handler or in my button event handler? im confused...
This answer turned up in the low quality review queue, presumably because you didn't explain the code. If you do explain it (in your answer), you are far more likely to get more upvotes—and the questioner is more likely to learn something!
I hope this is better - @TheGuywithTheHat
@Use13343 - Are you trying to bind the entire linked list to the ListBox or only add one item at a time?
Im adding one item at a time. So for each value the user put in, it would be inserted in the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.