❔ How to make this program into GUI application?
Hello guys! I have the following code:
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?
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
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
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
This is winforms
Look like a win 7 version thou
like so?
close
but you can't use
Console.ReadLine
and Console.WriteLine
since you have no console 🙂right
what should I use instead of console?
richTextBox1? That's how the second textbox is called
yeah you'll use the textboxes instead.
did it but then it shows these errors
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
calculator app, ok
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.not yet
still working
Can you show us what you have so far?
this is my code for the button. And It works when I start the form.
If the button is pressed, the second textbox shows a result. Now I need to do something about the user input
You need to read the input from the textbox, not
Console.ReadLine()
this is a GUI application, you have no consoleyeah, right. So instead of console I'll write the name of the first textbox, where the input is entered right?
Yes
ah hold on that doesn't work, the textbox doesn't contain definition of Readline
must use something else probably
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);
it's called textBox1
textBox1.text then
i'll try it
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
ok I wrote this
You need to understand how classes and those components work. Follow some simple tutorial online first. Documentation might be confusing to start with
and it shows this error:
"Text" uppercase probably
Because it's a property
ah thank you, works now
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
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
ok so now when I enter a number it shows a multiplication
the problem is, it doesn't show all combinations.
It must also show 6x1
you are assigning the value, not adding to it
assigning the value
a variable can only hold a single value
what does
name
contain now?hmm... a value?
and name must be variable
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 ?i wrote "read textbox1.text" in the first answer...
He asked the question: "in this example what is the value of the variable "name"?
it must be the name he wrote
And that name is... ?
John
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?that one will be a bit harder to answer
Why?
Im just asking, is it the same as the Steve/John example, or is it different?
I'll have to guess here
no?
incorrect. its exactly the same.
but it doesn't have any strings here
so every time that line of code runs, it overwrites previous value
thats why you only get one line
ok
so... if I want the rest of the lines I must do something else?
Yeah, you can't overwrite the current text
you want to append new text
now, luckily for you
RichTextBox.AppendText
exists.
richTextBox1.AppendText("Hello");
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.no, hold on...
let me check something...
yes, works now. Sort of. The problem was AppendText didn't paint properly at first
you'll want to add a newline to your string
yes I'm trying now
$"{a} x {b}\n"
it works wonders!
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
Thats easily fixed.
easily you say?
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
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. (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.
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__ 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.
interesting...
so I added this code in the newly created section for the first textbox:
.. 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...
is enough
after this block of code, you can safely use number
as the parsed numberyes, right. And to replace the console
I'll replace it with the name of the textbox, but what shall I use for ReadLine?
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
I got confused now.
is this how it's supposed to look?
what even is this? this isnt remotely your original code
show me the code you had when you were at this point here
yeah, I got a little confused and it became messy
now this code right here is for the section of the button:
the code I showed now is for the first textbox
you dont want any code on the first textbox
you just want the button to check if the value is valid before running
thats it.
ok I'll put the code in the button's secion then
I only changed the first line of code.
from
to
ok
that's how the whole code looks:
but now the Form started to look like this
you removed a bound eventhandler
you need to go into the designer code and remove the connection
clicking here should do that
thanks
let's see
Ah NICE! The program works now! 🙂
Just one small thing left:
If I enter a new number the previous result stays. Do you have any advice for that?
you can clear the rich text box at the top of the click handler
I can't? So to close and run it again is the only way?
no
I suggest you clear it using code.
at the top of the handler
at the top of the handler, ok
or after you have confirmed that the input is valid, that works too
so you dont clear unless there will be new results
yes that's the plan
okay, so clear the textbox directly after you have done
TryParse
I'll try to use this
That should work.
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 😄
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.