AlexBB

I have a Windows Forms app with Console playing a part. I now run an almost debugged code from an exec that is generated after the Build command. I need to prevent Console from accidentally being closed by clicking on the tiny button located at the right upper corner. I cannot figure out if it is possible to do.

The corresponding button on the form is blocked but it does not prevent the form from being closed by clicking on the button on the Console. I want to immobilize it too. I do not the app being closed accidentally in any event.

Thanks.




Re: Visual C# General Blocking Console closure?

frederikm

Hi

is it necessary for the console to be shown

you could just hide the entire window...

Hope this helps, please close the thread if it does






Re: Visual C# General Blocking Console closure?

Oscarfh

How can I open a console in a windows app




Re: Visual C# General Blocking Console closure?

AlexBB

frederikm wrote:

Hi

is it necessary for the console to be shown

you could just hide the entire window...

Hope this helps, please close the thread if it does

No, it does not help. I do need the Console. I get some information at runtime, short messages from various threads. I may not need it in the future but I do not know for sure.

Thanks.






Re: Visual C# General Blocking Console closure?

AlexBB

Oscarfh wrote:
How can I open a console in a windows app

This should go on top of your code in the form:

[DllImport ( "kernel32.dll", SetLastError = true )]

[return: MarshalAs ( UnmanagedType.Bool )]

static extern bool AllocConsole ( );

[DllImport ( "kernel32.dll", SetLastError = true )]

[return: MarshalAs ( UnmanagedType.Bool )]

static extern bool FreeConsole ( );

Then in form1_load:

AllocConsole ( );






Re: Visual C# General Blocking Console closure?

Figo Fei - MSFT

Hi, Alex

If you want to hide the X button at right top on the form. Just set the form.controlbox to false. And about the console, you can start it in another thread and try to hide it by backgroundworker, here is the reference: http://msdn2.microsoft.com/en-us/library/hybbz6ke(VS.80).aspx

http://www.codeproject.com/csharp/winconsole.asp df=100&forumid=16021&exp=0&select=1458374

Otherwise, you can choose other ways, anyway, it is just my idea.

Thank you






Re: Visual C# General Blocking Console closure?

Ernst Kuschke

Why don't you log these messages to a textbox rather Will make your life so much easier.





Re: Visual C# General Blocking Console closure?

AlexBB

Ernst Kuschke wrote:
Why don't you log these messages to a textbox rather Will make your life so much easier.

Using textbox is not an option. It won't work--it is not thread safe.






Re: Visual C# General Blocking Console closure?

AlexBB

Figo Fei - MSFT wrote:

Hi, Alex

If you want to hide the X button at right top on the form. Just set the form.controlbox to false. And about the console, you can start it in another thread and try to hide it by backgroundworker, here is the reference: http://msdn2.microsoft.com/en-us/library/hybbz6ke(VS.80).aspx

http://www.codeproject.com/csharp/winconsole.asp df=100&forumid=16021&exp=0&select=1458374

Otherwise, you can choose other ways, anyway, it is just my idea.

Thank you

I do not want to hide the console. I want to see it all the time. I read those messages. It is a very dynamic situation when the app runs. I want to prevent it from accidentally shutting the whole app down.






Re: Visual C# General Blocking Console closure?

AlexBB

AlexBB wrote:
Figo Fei - MSFT wrote:

Hi, Alex

If you want to hide the X button at right top on the form. Just set the form.controlbox to false. And about the console, you can start it in another thread and try to hide it by backgroundworker, here is the reference: http://msdn2.microsoft.com/en-us/library/hybbz6ke(VS.80).aspx

http://www.codeproject.com/csharp/winconsole.asp df=100&forumid=16021&exp=0&select=1458374

Otherwise, you can choose other ways, anyway, it is just my idea.

Thank you

I do not want to hide the console. I want to see it all the time. I read those messages. It is a very dynamic situation when the app runs. I want to prevent it from accidentally shutting the whole app down.

Figo, setting the form's ControlBox to false had no effect on the close button of the Console. Sorry. I think there is no connection between them.

Thanks.






Re: Visual C# General Blocking Console closure?

Figo Fei - MSFT

What i meant is if you want to hide the close button of the form you'd set the form's controlbox.

And about console, I think you'd get the handler of it, then modify it, but it is likely to be done in C++ instead of C#. That's my idea.






Re: Visual C# General Blocking Console closure?

Mark Betz

Alex, are you using System.Diagnostics.Process to set up and start the console application The only thing I can think of, aside from Figo's suggestion to hook the message loop for cmd.exe, is to create the process with ProcessStartInfo.CreateNoWindow set to true, and redirect stdout back to the spawning application using an anonymous pipe. You would then have the console process running windowless, with the output coming back so you can display it in a textbox or do whatever else you want to with it. This KB article gives an example of how to do it in unmanaged code.

http://support.microsoft.com/default.aspx scid=kb;en-us;190351





Re: Visual C# General Blocking Console closure?

AlexBB

Figo Fei - MSFT wrote:

What i meant is if you want to hide the close button of the form you'd set the form's controlbox.

And about console, I think you'd get the handler of it, then modify it, but it is likely to be done in C++ instead of C#. That's my idea.

Thank you, Figo, for all your suggestions. I will work on them. May come back with new questions.






Re: Visual C# General Blocking Console closure?

AlexBB

Mark Betz wrote:

Alex, are you using System.Diagnostics.Process to set up and start the console application The only thing I can think of, aside from Figo's suggestion to hook the message loop for cmd.exe, is to create the process with ProcessStartInfo.CreateNoWindow set to true, and redirect stdout back to the spawning application using an anonymous pipe. You would then have the console process running windowless, with the output coming back so you can display it in a textbox or do whatever else you want to with it. This KB article gives an example of how to do it in unmanaged code.

http://support.microsoft.com/default.aspx scid=kb;en-us;190351

Mark, thank you. No I am using this statement: Application.Run(new Form1());

Also it is not a Console app. It is a Windows Forms application. I invoke Console in the form_load event.

It is an interesting and appealing idea you are suggesting. I may take a shot at it. I have done similar things before: have some experience.






Re: Visual C# General Blocking Console closure?

Ernst Kuschke

Errr... that's exactly what I suggested a while ago Stick out tongue