简体   繁体   English

帮助使用TextBox和RichTextBox

[英]Help using TextBox and RichTextBox

I'm making a menu type of all kinds of sorting algorithm. 我正在制作各种排序算法的菜单类型。 the user will input ten numbers in the TextBox, select a RadioButton, then click the generate button. 用户将在文本框中输入十个数字,选择一个单选按钮,然后单击生成按钮。 the output must show each line on how the algorithm works. 输出必须显示有关算法工作原理的每一行。

(Selection Sort) (选择排序)

sample input from TextBox: 9 6 8 7 5 2 3 1 10 4 来自TextBox的示例输入: 9 6 8 7 5 2 3 1 10 4

output: 输出:

1 6 8 7 5 2 3 9 10 4 \n
1 2 8 7 5 6 3 9 10 4 \n
1 2 3 7 5 6 8 9 10 4 \n
1 2 3 4 5 6 8 9 10 7 \n
1 2 3 4 5 6 7 9 10 8 \n
1 2 3 4 5 6 7 8 10 9 \n
1 2 3 4 5 6 7 8 9 10 \n

I made a program like these on Java, but I only used JOptionPane. 我在Java上编写了这样的程序,但仅使用JOptionPane。 I do not have any ideas on how to convert it to c# and by using TextBox and RichTextBox. 我对如何将其转换为c#以及使用TextBox和RichTextBox没有任何想法。

Here's my codes so far. 到目前为止,这是我的代码。 My output always show a lot of zeros. 我的输出总是显示很多零。

   int[] nums = new int[10]; 
   int i, s, min, temp;

   private void EnterNum_TextChanged(object sender, EventArgs e)
   {
       string[] nums = EnterNum.Text.Split(' '); 
       //int[] nums = new int[] { int.Parse(EnterNum.Text) };
   } 

   private void GenerateButton_Click(object sender, EventArgs e)
   {
       if (SelectionRadio.Checked == true)
       {
           for (i = 0; i < nums.Length - 1; i++)
           {
               min = i; 
               // In each iteration, find the smallest number
               for (s = i + 1; s < nums.Length; s++)
               {
                   if (nums[min] > nums[s])
                   {
                       min = s;
                   }
               }
               if (min != i)
               {
                   temp = nums[i];
                   nums[i] = nums[min];
                   nums[min] = temp;
               }
               Display();
           }
       }
       Display();
   }

   private void ClearButton_Click(object sender, EventArgs e)
   {  
       richTextBox1.Clear();
   }

   public void Display()
   {
       int i;
       String numbers = "";
       for (i = 0; i < 10; i++)
         numbers += Convert.ToInt32(nums[i]).ToString() + " ";
       richTextBox1.AppendText(numbers);
   }

before starting algorithm you need to fill your nums array from textbox, you see zeros because by default array is filled with zeros and you just displaying default array: 在开始算法之前,您需要从文本框中填充nums数组,您会看到零,因为默认情况下该数组会填充零,并且您只显示默认数组:

string[] numsInString = EnterNum.Text.Split(' ');
nums = new int[numsInString.Length];
for (int j = 0; j < numsInString.Length; j++)
{
    nums[j] = int.Parse(numsInString[j]);
}  
if (SelectionRadio.Checked == true)
//...

and to display nicely add "\\n" when appending text: 并在添加文本时显示很好的添加“ \\ n”:

richTextBox1.AppendText(numbers+"\n");

also as Matt mentioned use .ToString() , your nums are integers already so you dont need to convert int to int : 就像Matt提到的使用.ToString() ,您的nums已经是整数,因此您无需将int转换为int

numbers += nums[i].ToString() + " ";

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

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