Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有没有办法使用按钮发送器将文本(按钮上的)直接发送到列表框中_C# - Fatal编程技术网

C# 有没有办法使用按钮发送器将文本(按钮上的)直接发送到列表框中

C# 有没有办法使用按钮发送器将文本(按钮上的)直接发送到列表框中,c#,C#,我尝试了两种解决方案,但都不起作用,第一种是直接将其发送到列表框,但都不起作用 1.将其直接发送到列表框中 private void button_Click(object sender, EventArgs e) { Button b = (Button)sender; lbItems = b.Text; } 2.我尝试的另一种方法是先将其发送到texbox,然后将字符串从texbox发送到listbox,但一个按钮后代码就会

我尝试了两种解决方案,但都不起作用,第一种是直接将其发送到列表框,但都不起作用

1.将其直接发送到列表框中

    private void button_Click(object sender, EventArgs e)
    {
      Button b = (Button)sender;
               lbItems = b.Text;
    }
2.我尝试的另一种方法是先将其发送到texbox,然后将字符串从texbox发送到listbox,但一个按钮后代码就会中断

    private void button_Click(object sender, EventArgs e)
    {
     Button b = (Button)sender;
           lbItems = b.Text;

      if (txtPre.Text == " ")
        {

        }
        else
        {
            lbItems.Items.Add(txtPre.Text);
            txtPre.Clear();
        }
    }

lbItems.Items.Add(b.Text)
lbItems=b.文本是非法代码
lbItems.Items.Add(b.Text)
lbItems=b.文本是非法代码
lbItems.Items.Add((sender as Button).Text);