简体   繁体   中英

Can anyone convert C# console app code to Windows Forms?

I wrote a console application with C# in VS.

I want to make a button, which runs the same code. In this case, what I wrote in the console application. I want to convert to Windows Forms. How would one go about that?

How would I do this? How can I take that code I created earlier and have a button execute it?

Thanks in advance.

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
    
namespace ConsoleApp1
{
        class Program
        {
            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();
            }
    }
}

You can convert a console application to a Windows Form one but the way depends on the type of console app you have:

1- The console app is a .NET Framework one:

  • Go to project proprieties and change the output type in Windows Application

  • Add a reference to the assembly System.Windows.Forms

  • Add a Windows Form object to your project

  • Change the Main function as follow:

     static void Main(string[] args) { Form1 frm = new Form1(); Application.Run(frm); }

Move your code to Form1 code behind

2- The console app is a.NOT core one

  • Go to project proprieties and change the output type in Windows Application

  • Change target OS to Windows

  • Enable Windows Forms for this project

  • Add a Windows Form object to your project

  • Change the Main function as follow:

     static void Main(string[] args) { Form1 frm = new Form1(); Application.Run(frm); }

Move your code to Form1 code behind

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM