【问题标题】:How do I print multiple lines on a label in win forms如何在winforms的标签上打印多行
【发布时间】:2020-12-30 13:47:57
【问题描述】:
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private int n;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            string scrambledWord = textBox1.Text;

            if (e.KeyCode == Keys.Enter)
            {
                label3.Text   = scrambledWord;
                int n = scrambledWord.Length;
                permute(scrambledWord, 0, n - 1);
            }
              

        }
        private void permute(String scrambledWord,
                            int l, int r)
        {
            if (l == r)
                label3.Text = scrambledWord;
            else
            {
                for (int i = l; i <= r; i++)
                {
                    scrambledWord = swap(scrambledWord, l, i);
                    permute(scrambledWord, l + 1, r);
                    scrambledWord = swap(scrambledWord, l, i);
                }
            }
        }

        /** 
        * Swap Characters at position 
        * @param a string value 
        * @param i position 1 
        * @param j position 2 
        * @return swapped string 
        */
        public static String swap(String a,
                                  int i, int j)
        {
            char temp;
            char[] charArray = a.ToCharArray();
            temp = charArray[i];
            charArray[i] = charArray[j];
            charArray[j] = temp;
            string s = new string(charArray);
            return s;
        }

    }
}

它只打印排列的第一行,我希望它打印所有排列。我是 WinForms 的新手。请帮忙,谢谢! ‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎‏‎ ‎ ‎

【问题讨论】:

  • 多行?使用文本框。并将属性 multiline 设置为 true。类似stackoverflow.com/questions/8536958/…
  • Youe Label 可以很好地显示多行,并且是一个比 TextBox 简单得多的控件。当然,您需要插入换行符和/或换行符。它不会自行缠绕;并且用户将能够从标签复制。
  • @TaW 我该怎么做?
  • 真的吗?如何将字符或字符串插入到正在构建的字符串中?在为下一行添加数据之前添加"\n""\r""\r\n"..!
  • @TaW 我试过它不起作用。不过还是谢谢你。对不起,我是一个菜鸟,我 14 岁,刚接触 C#

标签: c# string visual-studio winforms permutation


【解决方案1】:

您可以尝试以下代码来使用 Environment.Newline() 显示标签中的所有排列。

首先,我们需要将 label.AutoSize 设置为 false。

其次,这里有一个代码示例,你可以参考一下。

 private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            string scrambledWord = textBox1.Text;

            if (e.KeyCode == Keys.Enter)
            {
                label1.Text ="";
                char[] arr = scrambledWord.ToCharArray();
                GetPer(arr);
            }
        }

        public  void GetPer(char[] list)
        {
            int x = list.Length - 1;
            GetPer(list, 0, x);
        }

        private  void GetPer(char[] list, int k, int m)
        {
            if (k == m)
            {
                Console.WriteLine(list);
                var text = new string(list);
                label1.Text = label1.Text + text + Environment.NewLine;
           
            }
            else
                for (int i = k; i <= m; i++)
                {
                    Swap(ref list[k], ref list[i]);
                    GetPer(list, k + 1, m);
                    Swap(ref list[k], ref list[i]);
                }
        }
        private  void Swap(ref char a, ref char b)
        {
            if (a == b) return;

            var temp = a;
            a = b;
            b = temp;
        }

        private void label1_TextChanged(object sender, EventArgs e)
        {
            label1.Height = label1.Height + 10;
        }

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 2016-03-25
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多