Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I made a Console Application with C# in VS and i want to convert the code to window form but it was showing error can anyone fix it.

What I have tried:

<pre>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;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;

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

        private void Form1_Load(object sender, EventArgs e)
        {
             private const string cKey = "key";

        private const string cRegion = "region";


        public static async Task SpeechToTextAsync()

        {

            var config = SpeechConfig.FromSubscription(cKey, cRegion);

            using (var recognizer = new SpeechRecognizer(config))

                await Recognize(recognizer);

        }

        private static async Task Recognize(SpeechRecognizer recognizer)

        {

            var result = await recognizer.RecognizeOnceAsync();


            if (result.Reason == ResultReason.RecognizedSpeech)
            {

                Console.WriteLine($"Recognized: {result.Text}");

                SendKeys.SendWait(result.Text);
            }

            else if (result.Reason == ResultReason.NoMatch)

                Console.WriteLine("Speech could not be recognized.");

            else if (result.Reason == ResultReason.Canceled)

            {

                var cancellation =

                    CancellationDetails.FromResult(result);

                Console.WriteLine

                    ($"Cancelled due to reason={cancellation.Reason}");

                if (cancellation.Reason == CancellationReason.Error)

                {

                    Console.WriteLine

                        ($"Error code={cancellation.ErrorCode}");

                    Console.WriteLine

                        ($"Error details={cancellation.ErrorDetails}");

                    Console.WriteLine

                        ($"Did you update the subscription info?");

                }

            }

        }

        static void Main()

        {

            SpeechToTextAsync().Wait();
            Console.WriteLine("Please press enter to exit.");
            Console.ReadLine();

        }
    }
}
Posted
Updated 7-Nov-22 5:32am

Converting a console app to Windows GUI isn't simple: it's a total change in the way things work.

A console app does what it wants, and tells the user what to enter when it wants it.

A Windows app doesn't: it responds to what the user does when the user tells it to.

That's very different: and any attempt to convert a console app to windows is pretty much doomed from the start unless you are understand that and are prepared to effectively throw away most of your app and start again from scratch.

For example, the main method in a console app prints to the console, reads from the user and exists the app when it ends - you write the code that makes you whole app work in there, calling other methods as necessary.
In a Windows app, you pretty much never touch the Main method - and if you do you will make the whole app fall apart.

And in a Windows app, there is no Console - except in a debugging environment where you can write to it, bu7t reading from it is not going to work.

Instead, a Windows app interacts via messages, which in C# become Events which are Handled by your code. Changing a Textbox raises an Event, clicking a button raises an Event, moving the mouse over a control raises a whole bunch of Events!

What you've done is thrown a console app into a vaguely windows form class and expected it to get sorted out for you, and that isn't going to work, particularly when you start throwing async and threads in there.

Throw that lot away, think about what you are trying to achieve, and write a new WinForms app from scratch. Trying to bodge console code to work in a form is going to give you so much trouble it really isn't worth the effort.
 
Share this answer
 
Comments
Member 15706411 1-Aug-22 3:07am    
can you tell me I can make gui in console app
OriginalGriff 1-Aug-22 3:53am    
No, you can't: they are completely different paradigms.
What your are asking is "Can I make a bicycle that will carry 3000Kg at up to 200KPH and seat four?" when what you actually want is an estate car ...
[no name] 26-Mar-24 2:11am    
لدي مشروع تخرج نظام ويندوز سطح المكتب كيف يتم ربط مودل تنبّؤ بأسعار العقارات في
ml. net
حيث انه يحتوي على ملف مدخلات وملف مخرجات وملف المودل نفسه وملف الكونسول المطلوب اني اربط النموذج بفورم واجهة مستخدم سي شارب بحيث يتم ادخال البيانات في الفورم وليس في المودل ارجو المساعدة ولكم جزيل الشكر

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900