How can I refactor the code for WinForms?

Hello, experts in C#!
I have code like this, it's for console, but I remake for Windows Forms:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
 class Program
{
 static void Main(string[] args)
{
 int n = 10, k = 0;
 bool key = false;
 string[] str = new string[k];
 while (n < 1 || n > 5)
{
Console.Clear();
 Console.Write("Enter number of variables: ");
 n = Convert.ToInt32(Console.ReadLine());
}
 Console.WriteLine("function Value: ");
 string znach = Console.ReadLine();
 if (znach.Length != (int)Math.Pow(2, n))
{
 Console.WriteLine("incorrect number of function values");
 goto end;
}
 for (int i = 0; i < (int)Math.Pow(2, n); i++)
 if (Convert.ToInt32(znach[i]) < 49 && Convert.ToInt32(znach[i]) > 50)
{
 Console.WriteLine("you Typed an invalid value of a function");
 goto end;
}
 for (int i = 0; i < (int)Math.Pow(2, n); i++)
 if (znach[i] == '1')
{
k++;
 Array.Resize<string>(ref str, k);
 char[] chars = Convert.ToString(i, 2).PadLeft(n, '0').ToCharArray();
 for (int j = 0; j < n; j++)
{
 if (chars[j] == '0')
 chars[j] = Convert.ToChar(97 + j);
 if (chars[j] == '1')
 chars[j] = Convert.ToChar(65 + j);
}
 string strr = new string(chars);
 str[k - 1] = strr;
}
 foreach (string str in strng)
{
 Console.Write(strng + " ");
}
Console.WriteLine();
 while (!key)
{
 key = true;
 string[] ptr = new string[0]; //array for gluing at this stage
 int[] reuse = new int[str.Length]; //array of labels
 for (int l = 0; l < reuse.Length; l++) //use label (was it involved a piece of DNF at this iteration)
{
 reuse[l] = 0;
}
 for (int i = 0; i < str.Length; i++) //compare the pairs
{
 for (int j = i + 1; j < str.Length; j++)
{
 if (str[i].Length == str[j].Length) //if equal then forward dling
{
 int sbnr = 20, sbcnt = 0, sbcntt = 0; //variables for finding the number of different signs and number sign with a difference
 char[] charsi = str[i].ToCharArray(); //converted in arrays
 char[] charsj = str[j].ToCharArray();
 for (int l = 0; l < charsi.Length; l++)
{
 if (Convert.ToInt32(charsi[l]) - Convert.ToInt32(charsj[l]) == 32)
{
 sbnr = l;
sbcnt++;
}
 if (charsi[l] == charsj[l])
{
sbcntt++;
}
}
sbcntt++;
 if (sbcnt == 1 && sbcntt == charsi.Length) //if the pieces DNF differ by 1 sign
{
 for (int u = sbnr; u < charsi.Length - 1; u++)
{
 charsi[u] = charsi[u + 1];
}
 reuse[i] = 1;
 reuse[j] = 1;
 key = false;
 Array.Resize<char>(ref charsi, charsi.Length - 1); //then delete it
}
 string strr = new string(charsi);
 if (ptr.Length == 0 && !key) //the resulting gluing recorded in ptr
{
 Array.Resize<string>(ref ptr, 1);
 ptr[0] = strr;
 key = true;
}
 if (ptr.Length != 0 && !key) //if the ptr already contains pieces
{
 for (int e = 0; e < ptr.Length; e++)
{
 if (ptr[e] == strr)
{
 key = true;
}
}
 if (!key)
{
 Array.Resize<string>(ref ptr, ptr.Length + 1);
 ptr[ptr.Length - 1] = strr;
 key = true;
}
}
 } //end gluing
}
}
 for (int i = 0; i < reuse.Length; i++) //add pieces not involved in bonding
{
 if (reuse[i] == 0)
{
 Array.Resize<string>(ref ptr, ptr.Length + 1);
 ptr[ptr.Length - 1] = str[i];
}
 if (reuse[i] == 1)
{
 key = false;
}
}
 Array.Resize<string>(ref str, ptr.Length); //move ptr to str
 for (int i = 0; i < ptr.Length; i++)
{
 str[i] = ptr[i];
}
 foreach (string str in strng)
{
 Console.Write(strng + " ");
}
Console.WriteLine();
 } //go back to while on
 Console.WriteLine("your answer: ");
 for (int i = 0; i < str.Length; i++)
{
 if (!key)
 Console.Write(" V ");
 key = false;
Console.Write(str[i]);
}
end:
Console.Read();
}
}
}

And my question is: I need to alter the conclusion of this program so that it(the result of the program) can be written as a single string variable, how do you implement this action?
April 4th 20 at 00:47
2 answers
April 4th 20 at 00:49
You also advised I

All occurrences

Console.Write("Enter number of variables: ");
Replace

var sb = new StringBuilder();
sb.AppendLine("Enter number of variables: ");
var result = sb.ToString();
File.WriteAllBytes(@"c:\temp\result.txt", result);
April 4th 20 at 00:51
@Mitchell.Ondricka37, what you showed me here I have altered, I'm only interested in the output, like here:
Console.WriteLine("your answer: ");
 for (int i = 0; i < str.Length; i++)
{
 if (!key)
 Console.Write(" V ");
 key = false;
Console.Write(str[i]);
}
end:
Console.Read();
 }

where is the output
Read
https://docs.microsoft.com/ru-ru/dotnet/standard/b... - Mitchell.Ondricka37 commented on April 4th 20 at 00:54
@Mitchell.Ondricka37, I did it like this:
int n = 10, k = 0;
 bool key = false;
 string[] str = new string[k];
 var sb = new StringBuilder();
 var result = sb.ToString();
 while (n < 1 || n > 5)
{
 n = Convert.ToInt32(FunctionAmount.Text);
}
 string znach = FunctionValues.Text;
 if (znach.Length != (int)Math.Pow(2, n))
{
 MessageBox.Show("incorrect number of function values");
 goto end;
}
 for (int i = 0; i < (int)Math.Pow(2, n); i++)
 if (Convert.ToInt32(znach[i]) < 49 && Convert.ToInt32(znach[i]) > 50)
{
 MessageBox.Show("invalid value functions");
 goto end;
}
 for (int i = 0; i < (int)Math.Pow(2, n); i++)
 if (znach[i] == '1')
{
k++;
 Array.Resize<string>(ref str, k);
 char[] chars = Convert.ToString(i, 2).PadLeft(n, '0').ToCharArray();
 for (int j = 0; j < n; j++)
{
 if (chars[j] == '0')
 chars[j] = Convert.ToChar(97 + j);
 if (chars[j] == '1')
 chars[j] = Convert.ToChar(65 + j);
}
 string strr = new string(chars);
 str[k - 1] = strr;
}
 foreach (string str in strng)
{
 sb.AppendLine(strng + " ");
}
sb.AppendLine();
 while (!key)
{
 key = true;
 string[] ptr = new string[0]; //array for gluing at this stage
 int[] reuse = new int[str.Length]; //array of labels
 for (int l = 0; l < reuse.Length; l++) //use label (was it involved a piece of DNF at this iteration)
{
 reuse[l] = 0;
}
 for (int i = 0; i < str.Length; i++) //compare the pairs
{
 for (int j = i + 1; j < str.Length; j++)
{
 if (str[i].Length == str[j].Length) //if equal then forward dling
{
 int sbnr = 20, sbcnt = 0, sbcntt = 0; //variables for finding the number of different signs and number sign with a difference
 char[] charsi = str[i].ToCharArray(); //converted in arrays
 char[] charsj = str[j].ToCharArray();
 for (int l = 0; l < charsi.Length; l++)
{
 if (Convert.ToInt32(charsi[l]) - Convert.ToInt32(charsj[l]) == 32)
{
 sbnr = l;
sbcnt++;
}
 if (charsi[l] == charsj[l])
{
sbcntt++;
}
}
sbcntt++;
 if (sbcnt == 1 && sbcntt == charsi.Length) //if the pieces DNF differ by 1 sign
{
 for (int u = sbnr; u < charsi.Length - 1; u++)
{
 charsi[u] = charsi[u + 1];
}
 reuse[i] = 1;
 reuse[j] = 1;
 key = false;
 Array.Resize<char>(ref charsi, charsi.Length - 1); //then delete it
}
 string strr = new string(charsi);
 if (ptr.Length == 0 && !key) //the resulting gluing recorded in ptr
{
 Array.Resize<string>(ref ptr, 1);
 ptr[0] = strr;
 key = true;
}
 if (ptr.Length != 0 && !key) //if the ptr already contains pieces
{
 for (int t = 0; t < ptr.Length; t++)
{
 if (ptr[t] == strr)
{
 key = true;
}
}
 if (!key)
{
 Array.Resize<string>(ref ptr, ptr.Length + 1);
 ptr[ptr.Length - 1] = strr;
 key = true;
}
}
 } //end gluing
}
}
 for (int i = 0; i < reuse.Length; i++) //add pieces not involved in bonding
{
 if (reuse[i] == 0)
{
 Array.Resize<string>(ref ptr, ptr.Length + 1);
 ptr[ptr.Length - 1] = str[i];
}
 if (reuse[i] == 1)
{
 key = false;
}
}
 Array.Resize<string>(ref str, ptr.Length); //move ptr to str
 for (int i = 0; i < ptr.Length; i++)
{
 str[i] = ptr[i];
}
 foreach (string str in strng)
{
 sb.AppendLine(strng + " ");
}
 } //go back to while on
 for (int i = 0; i < str.Length; i++)
{
 if (!key)
 sb.AppendLine(" V ");
 key = false;
sb.AppendLine(str[i]);
 textbox1.Text = str[i];

But when you output the answer, consisting of several elements it displays only the last that is shown on the drawings: In the first the output of the console version of the program
5e11fd782e2b6065022682.png
the second is my redone version
5e11fda92c92a663547624.png
but I can't determine what I missed and where - Emery.Botsford commented on April 4th 20 at 00:57
@Emery.Botsford, I have to be honest too, my advice to break the code for basic functions and to make every function check for the return value.

And avoid nested loops, they are just mind-blowing - Mitchell.Ondricka37 commented on April 4th 20 at 01:00

Find more questions by tags C#Windows Forms