简体   繁体   English

显示从文本框到列表框的输出,C#

[英]Displaying output from a textbox to a listbox, C#

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. 因此,我正在C#winforms中创建一个LinkedList,现在我想在添加,删除和搜索节点值时直观地显示该列表。 I use 3 button controls, one for adding nodes, removing nodes and the last for searching for a nodes value. 我使用3个按钮控件,一个用于添加节点,删除节点,最后一个用于搜索节点值。 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. 然后,我有一个文本框,应该从用户(例如10)中读取一个int值,然后在列表框中将该值显示为链接列表。 So, Ive created a Node class and a linkedlist class. 因此,我创建了一个Node类和一个链表类。 The Node class holds a value and a reference to the next node: 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) 还有一个带有我所有方法的LinkedList类(我只会写我的add方法,所以代码不会太多)

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) 因此,现在我要尝试编写一些代码,以便当我在文本框中输入int编号时,该编号应作为LinkedList出现在列表框中(使用我的LinkedList类中的add方法)

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? 有人可以在这里指出我正确的方向吗?

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). textBox1是代表文本框的对象,而不是存储在其中的值(这是Text属性)。 TextBox s store their value in string format, hence the use of Int32.Parse() (which converts from string (or other values) to int. TextBox以字符串格式存储其值,因此使用Int32.Parse() (将字符串(或其他值)转换为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() 应当注意, Int32.Parse不是执行此操作的最安全方法,如果要验证该值确实是int ,则应使用Int32.TryParse()

If your ListBox is called listBox1 you add to it like this: 如果您的ListBox名为listBox1您可以这样添加:

listBox1.Items.Add(something);

You can add strings directly but you probably want to add one of your LinkedList objects. 您可以直接添加字符串,但是可能要添加一个LinkedList对象。 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: ..但在您向LinkedList类添加ToString方法之前,它不会显示任何有用的信息:

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: 我还建议向您的LinkedList类添加一个或多个构造函数,以至少立即设置该值:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM