C
C#2y ago
Name

❔ How to make this program into GUI application?

Hello guys! I have the following code:
Console.WriteLine("Enter number: ");
int input = Convert.ToInt32(Console.ReadLine());
int a = 0; int b = 0;

while (a <= input)
{
var mul = a * b;

if (mul == input)
{
if (a > b)
break;

Console.WriteLine(a + " x " + b);
}

if (b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
Console.WriteLine("Enter number: ");
int input = Convert.ToInt32(Console.ReadLine());
int a = 0; int b = 0;

while (a <= input)
{
var mul = a * b;

if (mul == input)
{
if (a > b)
break;

Console.WriteLine(a + " x " + b);
}

if (b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
It's for finding all possible multiplications of a given number which the user enters. For example: if I enter 2, the program shows: 2x1, if I enter 4, it shows 2x2 and 4x1 and so on... This program works in the C# console application but I want to try to make it work in GUI. My idea is to have 2 textboxes and 1 button. I enter the number in the first textbox and after I press the button, it must show the result in the second textbox.
However, I have never worked with GUI and I don't know what exactly do to. Does anybody have any idea where I should start?
95 Replies
Omnissiah
Omnissiah2y ago
if you double click on Generate in design mode vs will create the click event routine for you that's a start then you have to recover the text from the left textbox, i guess it would be textbox1.text or textbox2.text then you do your thing and then you assign text of the other textbox you could either concat a string where you console.writeline, or you could use a list instead
Pobiega
Pobiega2y ago
this looks like winforms. Winforms is entirely event-driven so as suggested above, you'll double-click the button to create an "OnClick" event handler for that button in that handler, you write the code that should happen when the button is clicked. This will be more or less all your code from the console app, minus the console stuff since the controls belong in the same form, you will be able to access them via code by just using their names. Assuming the "input" field here is called "textbox1", you can access the content with textbox1.Text
gamer50082
gamer500822y ago
This is winforms Look like a win 7 version thou
Name
NameOP2y ago
like so?
Pobiega
Pobiega2y ago
close but you can't use Console.ReadLine and Console.WriteLine since you have no console 🙂
Name
NameOP2y ago
right what should I use instead of console? richTextBox1? That's how the second textbox is called
Pobiega
Pobiega2y ago
yeah you'll use the textboxes instead.
Name
NameOP2y ago
did it but then it shows these errors
Shinigami
Shinigami2y ago
Remove the writeline You can search YouTube for basic winforms calculator app You'll get all the info you need If you have any doubts you can come back here anytime
Name
NameOP2y ago
calculator app, ok
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Name
NameOP2y ago
not yet still working
Pobiega
Pobiega2y ago
Can you show us what you have so far?
Name
NameOP2y ago
this is my code for the button. And It works when I start the form.
Name
NameOP2y ago
If the button is pressed, the second textbox shows a result. Now I need to do something about the user input
Pobiega
Pobiega2y ago
You need to read the input from the textbox, not Console.ReadLine() this is a GUI application, you have no console
Name
NameOP2y ago
yeah, right. So instead of console I'll write the name of the first textbox, where the input is entered right?
Shinigami
Shinigami2y ago
Yes
Name
NameOP2y ago
ah hold on that doesn't work, the textbox doesn't contain definition of Readline must use something else probably
Shinigami
Shinigami2y ago
See this is winforms Readline and writeline is for console What's the name of the box you're entering your number? Whatever it is This how you'd do Int number = convert.toint32(yourboxname.text);
Name
NameOP2y ago
it's called textBox1
Shinigami
Shinigami2y ago
textBox1.text then
Name
NameOP2y ago
i'll try it
Shinigami
Shinigami2y ago
Also just a suggestion, whatever is the requirement try to do a simpler version of it For example in your case you wanted the multiples You could just do something like give an int and output would be input X 10 If that is working then you can work on your original logic and replace the initial simpler logic with it
Name
NameOP2y ago
ok I wrote this
int input = Convert.ToInt32(textBox1.text);
int input = Convert.ToInt32(textBox1.text);
abcyz
abcyz2y ago
You need to understand how classes and those components work. Follow some simple tutorial online first. Documentation might be confusing to start with
Name
NameOP2y ago
and it shows this error:
abcyz
abcyz2y ago
"Text" uppercase probably Because it's a property
Name
NameOP2y ago
ah thank you, works now
abcyz
abcyz2y ago
But you need to understand that using the Console class here doesn't make sense, since you aren't using a Console now. You are able to access the TextBox because you have access to it using the variable "textBox1" which references the Text Box in your GUI
Name
NameOP2y ago
yeah. doesn't make sense using console class when I'm not even using a console when I think about it. This code was from console application, though. I just needed to change few things for it to work with gui
Name
NameOP2y ago
ok so now when I enter a number it shows a multiplication
Name
NameOP2y ago
the problem is, it doesn't show all combinations. It must also show 6x1
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
you are assigning the value, not adding to it
Name
NameOP2y ago
assigning the value
Pobiega
Pobiega2y ago
a variable can only hold a single value
string name = "Steve";
name = "John";
string name = "Steve";
name = "John";
what does name contain now?
Name
NameOP2y ago
hmm... a value? and name must be variable
Pobiega
Pobiega2y ago
Pobiega
Pobiega2y ago
I don't know if there is some kind of barrier here, its a fairly straight-forward question. What is the value of the variable called name after these two lines of code have run? @Name ?
Omnissiah
Omnissiah2y ago
i wrote "read textbox1.text" in the first answer...
abcyz
abcyz2y ago
He asked the question: "in this example what is the value of the variable "name"?
Name
NameOP2y ago
it must be the name he wrote
Pobiega
Pobiega2y ago
And that name is... ?
Name
NameOP2y ago
John
Pobiega
Pobiega2y ago
Correct. Now, when you do richTextBox.Text = $"{a} x {b}"; several times in a loop, what does that do? is it like the Steve/John example, or is it different?
Name
NameOP2y ago
that one will be a bit harder to answer
Pobiega
Pobiega2y ago
Why? Im just asking, is it the same as the Steve/John example, or is it different?
Name
NameOP2y ago
I'll have to guess here no?
Pobiega
Pobiega2y ago
incorrect. its exactly the same.
Name
NameOP2y ago
but it doesn't have any strings here
Pobiega
Pobiega2y ago
so every time that line of code runs, it overwrites previous value thats why you only get one line
Name
NameOP2y ago
ok so... if I want the rest of the lines I must do something else?
Pobiega
Pobiega2y ago
Yeah, you can't overwrite the current text you want to append new text now, luckily for you RichTextBox.AppendText exists. richTextBox1.AppendText("Hello");
Name
NameOP2y ago
Pobiega
Pobiega2y ago
let me guess, you tried richTextBox1.AppendText = "your new value"; thus ignoring the message you actually replied to, which shows exactly how to use it.
Name
NameOP2y ago
no, hold on... let me check something...
Name
NameOP2y ago
yes, works now. Sort of. The problem was AppendText didn't paint properly at first
Pobiega
Pobiega2y ago
you'll want to add a newline to your string
Name
NameOP2y ago
yes I'm trying now
Pobiega
Pobiega2y ago
$"{a} x {b}\n"
Name
NameOP2y ago
it works wonders!
Name
NameOP2y ago
Now the only thing left to worry about is that when I enter anything different than a number or If I don't enter anything the program will break
Pobiega
Pobiega2y ago
Thats easily fixed.
Name
NameOP2y ago
easily you say?
Pobiega
Pobiega2y ago
yes. Im going to invoke a bot command that will paste a really long message dont be afraid, and read the entire message, okay? it will tell you how to solve this problem $tryparse
MODiX
MODiX2y ago
The TryParse pattern is considered best practice of parsing data from a string: - a TryParse method returns true or false to inform you if it succeeded or not, so you can use it directly in a condition, - since C# 7 you can declare a variable that will be used as an out argument inline in an argument list, - it forces you to check if the out argument contains valid data afterwards, Avoid: Convert.ToInt32 — it's a bad choice for parsing an int. It exists only for backwards compatibility reasons and should be considered last resort.
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
(Note: Convert does contain useful conversion methods: To/FromBase64String, To/FromHexString, ToString(X value, int toBase), ToX(string? value, int fromBase)) Avoid: int.Parse — you have to use a try/catch statement to handle invalid input, which is a less clean solution.
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Name
NameOP2y ago
interesting... so I added this code in the newly created section for the first textbox:
int n;
string output;


do
{

output = Console.ReadLine();
} while (int.TryParse(output, out n) == false);
int n;
string output;


do
{

output = Console.ReadLine();
} while (int.TryParse(output, out n) == false);
Pobiega
Pobiega2y ago
.. what I thought we had established you can't use Console.ReadLine in a winforms app? all you need to do is check if the input was a valid number before continuing the method you can "stop" a method early by using return so in this case...
if(!int.TryParse(textBox1.Text, out var number))
{
return;
}
if(!int.TryParse(textBox1.Text, out var number))
{
return;
}
is enough after this block of code, you can safely use number as the parsed number
Name
NameOP2y ago
yes, right. And to replace the console I'll replace it with the name of the textbox, but what shall I use for ReadLine?
Pobiega
Pobiega2y ago
Come on dude You already had that bit working. That hasn't changed. reading the input from the user is exactly the same as before, all we need to do is change how we parse it and I even wrote it in my example code 😦 I get a very strong feeling you dont really read the code I write
Name
NameOP2y ago
I got confused now. is this how it's supposed to look?
int n;
string output;


do
{

output = Console.ReadLine();
} while (int.TryParse(output, out n) == false);

if (!int.TryParse(textBox1.Text, out var number))
{
return;
}
int n;
string output;


do
{

output = Console.ReadLine();
} while (int.TryParse(output, out n) == false);

if (!int.TryParse(textBox1.Text, out var number))
{
return;
}
Pobiega
Pobiega2y ago
what even is this? this isnt remotely your original code show me the code you had when you were at this point here
Name
NameOP2y ago
yeah, I got a little confused and it became messy now this code right here is for the section of the button:
int input = Convert.ToInt32(textBox1.Text);
int a = 0; int b = 0;

while (a <= input)
{
var mul = a * b;

if (mul == input)
{
if (a > b)
break;

richTextBox1.AppendText($"{a} x {b}\n");

}

if (b == input)
{
b = 0;
a++;
}
else
{
b++;
}
int input = Convert.ToInt32(textBox1.Text);
int a = 0; int b = 0;

while (a <= input)
{
var mul = a * b;

if (mul == input)
{
if (a > b)
break;

richTextBox1.AppendText($"{a} x {b}\n");

}

if (b == input)
{
b = 0;
a++;
}
else
{
b++;
}
the code I showed now is for the first textbox
Pobiega
Pobiega2y ago
you dont want any code on the first textbox you just want the button to check if the value is valid before running
if (!int.TryParse(textBox1.Text, out int input))
{
return;
}

int a = 0;
int b = 0;

while (a <= input)
{
var mul = a * b;

if (mul == input)
{
if (a > b)
break;

richTextBox1.AppendText($"{a} x {b}\n");
}

if (b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
if (!int.TryParse(textBox1.Text, out int input))
{
return;
}

int a = 0;
int b = 0;

while (a <= input)
{
var mul = a * b;

if (mul == input)
{
if (a > b)
break;

richTextBox1.AppendText($"{a} x {b}\n");
}

if (b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}
thats it.
Name
NameOP2y ago
ok I'll put the code in the button's secion then
Pobiega
Pobiega2y ago
I only changed the first line of code. from
int input = Convert.ToInt32(textBox1.Text);
int input = Convert.ToInt32(textBox1.Text);
to
if (!int.TryParse(textBox1.Text, out int input))
{
return;
}
if (!int.TryParse(textBox1.Text, out int input))
{
return;
}
Name
NameOP2y ago
ok that's how the whole code looks:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GUIProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void button1_Click(object sender, EventArgs e)
{



if (!int.TryParse(textBox1.Text, out int input))
{
return;
}

int a = 0;
int b = 0;

while (a <= input)
{
var mul = a * b;

if (mul == input)
{
if (a > b)
break;

richTextBox1.AppendText($"{a} x {b}\n");
}

if (b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}

}


}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GUIProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void button1_Click(object sender, EventArgs e)
{



if (!int.TryParse(textBox1.Text, out int input))
{
return;
}

int a = 0;
int b = 0;

while (a <= input)
{
var mul = a * b;

if (mul == input)
{
if (a > b)
break;

richTextBox1.AppendText($"{a} x {b}\n");
}

if (b == input)
{
b = 0;
a++;
}
else
{
b++;
}
}

}


}
}
Name
NameOP2y ago
but now the Form started to look like this
Pobiega
Pobiega2y ago
you removed a bound eventhandler you need to go into the designer code and remove the connection
Pobiega
Pobiega2y ago
clicking here should do that
Name
NameOP2y ago
thanks let's see Ah NICE! The program works now! 🙂 Just one small thing left:
Name
NameOP2y ago
Name
NameOP2y ago
If I enter a new number the previous result stays. Do you have any advice for that?
Pobiega
Pobiega2y ago
you can clear the rich text box at the top of the click handler
Name
NameOP2y ago
I can't? So to close and run it again is the only way?
Pobiega
Pobiega2y ago
no I suggest you clear it using code. at the top of the handler
Name
NameOP2y ago
at the top of the handler, ok
Pobiega
Pobiega2y ago
or after you have confirmed that the input is valid, that works too so you dont clear unless there will be new results
Name
NameOP2y ago
yes that's the plan
Pobiega
Pobiega2y ago
okay, so clear the textbox directly after you have done TryParse
Name
NameOP2y ago
I'll try to use this
richTextBox1.Text = String.Empty
richTextBox1.Text = String.Empty
Pobiega
Pobiega2y ago
That should work.
Name
NameOP2y ago
Yes! It works. Ok and that's a wrap. Thanks very much for the help, really appreciate it, I can sure say I learned a thing or 2 out of this situation. 🙂 I'm definitely going to spend some time learning the basics of C# at least 😄 Come to think of it, I may even start right now 😄
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server