如何使用C#中的递归在列表框中打印1到n?

如何使用C#中的递归在列表框中打印1到n?,c#,recursion,listbox,C#,Recursion,Listbox,我试图编写一个程序,使用递归循环给定的值,并将其按顺序打印到列表框中。当我用5表示n时,输出应该是这样的: 123445 然而,我无法找到一种使我的程序工作的方法,而我得到的任何其他资源都只使用控制台函数 下面是我到目前为止的代码。如果能得到任何帮助,我将不胜感激 private void button1_Click(object sender, EventArgs e) { int n; n = Convert.ToInt32(textBox1.Text); int p

我试图编写一个程序,使用递归循环给定的值,并将其按顺序打印到列表框中。当我用5表示n时,输出应该是这样的:

123445

然而,我无法找到一种使我的程序工作的方法,而我得到的任何其他资源都只使用控制台函数

下面是我到目前为止的代码。如果能得到任何帮助,我将不胜感激

private void button1_Click(object sender, EventArgs e)
{
    int n;
    n = Convert.ToInt32(textBox1.Text);
    int print = Print(1, n);
    listBox1.Items.Add(print);
}
       
private static int Print(int order, int n)
{
    if (n < 1)
    {
        return order;
    }
    n--;
    return Print(order + 1, n);
}
private void按钮1\u单击(对象发送者,事件参数e)
{
int n;
n=转换为32(textBox1.Text);
int print=print(1,n);
列表框1.Items.Add(打印);
}
私有静态整数打印(整数顺序,整数n)
{
if(n<1)
{
退货单;
}
n--;
返回打印(订单+1,n);
}

嗯,我现在无法测试这个,但我希望我已经把概念讲清楚了。
像这样的东西应该能起作用:

private void button1_Click(object sender, EventArgs e)
{
    int n;
    n = Convert.ToInt32(textBox1.Text);
    PrintToListBox(listBox1, 1, n);
}
    
private static void PrintToListBox(ListBox listBox, int frm, int to)
{
    listBox1.Items.Add(frm);
    if (frm + 1 <= to)
    {
        PrintToListBox(listBox1, frm + 1, n);
    }
    return;
}
private void按钮1\u单击(对象发送者,事件参数e)
{
int n;
n=转换为32(textBox1.Text);
打印列表框(列表框1、1、n);
}
专用静态无效打印列表框(列表框列表框,int-frm,int-to)
{
列表框1.Items.Add(frm);
如果(frm+1我会写为:

private void button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    PrintToListBox(listBox1, 1, 5);
}

private void PrintToListBox(ListBox lb, int n, int max)
{
    if (n <= max)
    {
        lb.Items.Add(n);
        PrintToListBox(lb, n+1, max);
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
listBox1.Items.Clear();
打印列表框(列表框1、1、5);
}
专用void打印列表框(列表框lb、int n、int max)
{

谢谢你,洛伦佐,这正是我需要的