✅ console.writeline not working on Winforms application
I attempted to use this post to help me https://stackoverflow.com/questions/4362111/how-do-i-show-a-console-output-window-in-a-forms-application use a console on a WinForms application but the output will not show, I have read over the "Duplicate questions" page and they all don't answer my question
I've tried using Debug. WriteLine but it doesn't work either. I'm assuming I'm missing a setting of some sort but they all say "use Managed Compatibility Mode" but its been gone for years, I'm having it open the console on a button press, and it opens the console it just won't output anything I've also tried "FreeConsole()" but it doesn't even open.
Stack Overflow
How do I show a console output/window in a forms application?
To get stuck in straight away, a very basic example:
using System;
using System.Windows.Forms;
class test
{
static void Main()
{
Console.WriteLine("test");
MessageBox.Sh...
46 Replies
This is Visual studio 2022
C# winforms
i just relized that the output is going to this part under my workspace
the debug output console
how could i fix it?
Why are you trying to write to the console in a windows forms applications anyways?
The
Debug.WriteLine
invocations would write to this.Console.Writeline does the same thing
That's fine
So what's the issue then?
Are you just trying to open a dedicated console window from your form?
look here is the code right
uh i think thats how to explain it
i want the text to show up on the console
but it doesnt
Well, move the
AllocConsole
invocation to your form load method as the post describes for starters, otherwise you're allocating a console every time you click the button.
That could be part of the problemyea doesnt work
and i want it to open on a button press anyway
Then add some logic to determine if it was already opened so you don't keep reallocating:
That's a good start in trying to isolate the problem
it just opens like normal when i hit the button
Share your full code file
When I do exactly as the SO answer describes it works for me just fine
this is all i ahve
@tacosontitan
am i missing something
Yeah, you're not defining
_consoleAllocated
anywhere.oh mb that was above button1
i ahve it
Are you excluding code?
not on purpose
thats my fault
this is just a test application
What version of .NET are you targeting?
should i be using more upto date
Is there a reason you're targeting that specific version?
IMHO, absolutely unless you have a solid reason not to
not really
its just what i made it with
my harddrive corrupted a few days ago so my settings are off
im using a old one
I would target .NET 6 or 7 at a minimum
ok
what framework are you on
when you made the test
When I created the project it defaulted to
net6.0-windows
.hm ok
how come the framework isnt showing up
i installed it
7.0
ill just google it
You can't directly upgrade from .NET Framework to .NET or .NET Core
You can either upgrade it by hand, with an upgrade tool, or just recreate the project with a modern template.
I prefer the by hand if it's a small project that I just chose the wrong template for
how do i upgrade it byhand
could you tell me a upgrade tool
I can drop a link for the upgrade tool in a moment, but you said it's just a basic test project right?
So you're just getting started?
yea but for the future
yea is it
No added packages or dependencies?
not currently
Right click the project -> Unload Project
Right click the project -> Edit
Copy contents to notepad for sanity check
Replace with:
Save.
Right click the project -> reload
I can drop the upgrade tool if you're not comfortable with that approach
oh alright
ill try it
Upgrade a Windows Forms app to .NET 7 - Windows Forms .NET
Learn how to upgrade a .NET Framework (or previous .NET) Windows Forms application to .NET 7.
The upgrade tool takes longer IMHO
Its a better fit for an established project.
I upgraded my project to 6.0 and it worked thanks
@tacosontitan
No problem