1024programmer Asp.Net A brief introduction to developing desktop applications in C#

A brief introduction to developing desktop applications in C#

A brief introduction to developing desktop applications with C#

1. Introduction to C# usage scenarios

C# is an object-oriented programming language derived from C and C++ released by Microsoft. It is a high-level programming language that runs on the .NET Framework and .NET Core (completely open source, cross-platform).

2. Development process

 1. Create a project: Open Visual Studio and select “Create New Project” on the right, then select “C# Windows Forms Application” to create a desktop program

 2. Create a form: After creation, a Form form will be automatically created as the main window. You can use drag and drop for project development. Open the built-in toolbox through “View” –> “Toolbar” in the navigation bar. Function

3. Start development: There are two modes during development, visual development and writing code. In visual development, a property bar will pop up on the right side of the selected form or element where you can set styles and define events; code development can be logically developed through the corresponding mapping file.

4. Run the program: After the development is completed, click the green arrow button at the top of the screen. The “Start” button runs the program and enters the Debug mode at the same time. It supports functional debugging. The “Start Run” button only runs the program and does not enter the Debug mode.

5. Generate solution: Click “Generate” -> “Generate Solution” on the navigation bar to generate the project file

3. Commonly used functions

 1. Pop-up boxes and automatic closing pop-up boxes: Pop-up boxes and closing pop-up boxes are often needed in project development. It is recommended to encapsulate unified components according to different levels and call them

// Prompt pop-up box  
 public static void PopWarning(string tip,  string title = "  Prompt")
 {
     MessageBox.Show(tip, title);
 }

 // Error popup  box
 public static void PopError(string tip,  string title = "  Error")
 {
     MessageBox.Show(tip, title);
 }

 // Automatically close  Pop-up box
 public void BoxAutoClose(  string tip, int  time = 3000)
 {
     Form msg = new  Form();
     Task.Run(new Action(() =>
     {
         Thread.Sleep(time);
         msg.Invoke((Action)(() =>
         {
             msg.Close();
         }));
     }));
     MessageBox.Show(msg, tip, "Prompt");
 }

2. Use of serial port and encapsulation of instructions: During project development, data interaction with the firmware needs to be carried out through the serial port. The built-in Serialport component of C# can be used, but it is recommended not to drag and drop. It is best to use a new one, and then send instructions and receive Instructions and timeout mechanisms are used together

// Command class,  Store instructions and time objects
 class Command {
     // Storage instructions  Dataset
     public Dictionary<string, Hashtable> data =   new Dictionary<string, Hashtable>();

     //Default timeout  The time is 7 seconds
     public void Set(  string id, int  timeUpper = 7)
     {
         if (data.  ContainsKey(id))
         {
             data[id]["time"] =DateTime.Now;
         }
         else
         {
             Hashtable ht = new  Hashtable();
             ht.Add("time",  DateTime.Now);
             ht.Add("timeUpper",  timeUpper);
             data[id] = ht;
         }
     }

     public void  span> Delete(string id)
     {
         data.Remove(id);
     }

     public void  span> DeleteAll()
     {
         data.Clear();
     }

     /**
      * Identify timed out commands
      */
     public string[] CheckOverTime()
     {
         if (data == null)
         {
             return new  span> string[0] { };
         }
         string[] coms = new string[  data.Count];

         for (int i = 0; i < data.Count; i++)
         {
             DateTime val = (DateTime)data.ElementAt(i).Value["time"];
             int timeUpper = (  int)data.ElementAt(i).Value["timeUpper"];
             if (new TimeSpan(DateTime.Now.Ticks - val.Ticks).TotalSeconds > timeUpper)
             {
                 coms[i] = data.ElementAt(i).Key;
             }
         }

         return coms.Where(e => e != null).ToArray();
     }
 }

Create a Command class, which mainly stores the sending instruction set and determines whether the instruction has timed out

class Serial {
     private SerialPort port;
     Form main;
     Command cmd;
    
     public Serial(Form  main)
     {
         this.main =  main;
         cmd = new Command  ();
         this.Init(  );
         this.CreateTimer(  );
     }

     /**
      * Create a timer and query whether there is a timeout command every second
      */
     public void CreateTimer()
     {
         System.Timers.Timer cTimer = newSystem.Timers.Timer();
         cTimer.Interval = 1000;
         cTimer.Enabled = true;
         cTimer.AutoReset = true;
         cTimer.Elapsed += new System.Timers.ElapsedEventHandler((Object source, ElapsedEventArgs e) =>
         {
             string[] cmds = cmd.CheckOverTime();
             for (int i = 0; i < cmds.Length; i++)
             {
                 cmd.Delete(cmds[i]);
                 if (cmds[i] != "FF")
                 {
                     // Return to main  Window timeout flag
                     main.DealRes(new byte  span>[] { (byte)Convert.ToInt32(cmds[i], 16) }, 1);
                 }
             }
         });
     }

     /**
      * Initialize and create serial port components
      */
     void Init() {

         port = new SerialPort  ();
         port.BaudRate = 9600;
         port.Parity = Parity.None;
         port.StopBits = StopBits.One;
         port.DataBits = 8;
         port.DataReceived += new SerialDataReceivedEventHandler(ReceiveData);
     }

     /**
      * Convert character array to string
      */
     public static string</b\net45\ICSharpCode.SharpZipLib.dll
     
     <Reference Include="MathNet.Numerics  , Version=4.15.0.0, Culture=neutral, PublicKeyToken=cd8b63ad3d691a37, processorArchitecture=MSIL">
       .\packages\MathNet.Numerics.Signed.4.15.0\lib\net461\MathNet.Numerics.dll
     
     <Reference Include="Microsoft.IO  .RecyclableMemoryStream, Version=2.3.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
       .\packages\Microsoft.IO.RecyclableMemoryStream.2.3.2\lib\net462\Microsoft.IO.RecyclableMemoryStream.dll
     
     <Reference Include="NPOI.Core  , Version=2.6.2.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
       .\packages\NPOI.2.6.2\lib\net472\NPOI.Core.dll
     
     <Reference Include="NPOI.OOXML  , Version=2.6.2.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
       .\packages\NPOI.2.6.2\lib\net472\NPOI.OOXML.dll
     
     <Reference Include="NPOI.OpenXml4Net  , Version=2.6.2.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
       .\packages\NPOI.2.6.2\lib\net472\NPOI.OpenXml4Net.dll
     
     <Reference Include="NPOI.OpenXmlFormats  , Version=2.6.2.0, Culture=neutral, PublicKeyToken=0df73ec7942b34e1, processorArchitecture=MSIL">
       .\packages\NPOI.2.6.2\lib\net472\NPOI.OpenXmlFormats.dll
     
     <Reference Include="SixLabors.Fonts  , Version=1.0.0.0, Culture=neutral, PublicKeyToken=d998eea7b14cab13, processorArchitecture=MSIL">
       .\packages\SixLabors.Fonts.1.0.0\lib\netstandard2.0\SixLabors.Fonts.dll
     
     <Reference Include="SixLabors.ImageSharp  , Version=2.0.0.0, Culture=neutral, PublicKeyToken=d998eea7b14cab13, processorArchitecture=MSIL">
       .\packages\SixLabors.ImageSharp.2.1.4\lib\net472\SixLabors.ImageSharp.dll
     
     <Reference Include="System.ComponentModel  .DataAnnotations" />
     <Reference Include="System.Configuration  " />
     <Reference Include="System.Core  " />
     <Reference Include="System.DirectoryServices  " />
     <Reference Include="System.Memory  , Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
       .\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll
     
     <Reference Include="System.Numerics  " />
     <Reference Include="System.Numerics  .Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
       .\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll
     
     <Reference Include="System.Runtime  .CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"  >
       .\NPOISystem.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll
     
     <Reference Include="System.Runtime  .Serialization" />
     <Reference Include="System.Security  " />
     <Reference Include="System.Security  .AccessControl, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
       .\packages\System.Security.AccessControl.6.0.0\lib\net461\System.Security.AccessControl.dll
     
     <Reference Include="System.Security  .Cryptography.Xml, Version=6.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL"  >
       .\packages\System.Security.Cryptography.Xml.6.0.1\lib\net461\System.Security.Cryptography.Xml.dll
     
     <Reference Include="System.Security  .Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"  >
       .\packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll
     
     <Reference Include="System.Text  .Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"  >
       .\packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll
     

Configure reference content

 d. There are many online cases for exporting Excel and reading Excel. You can expand it later.

, 0, 1)”>” />
<Reference Include=System.Memory , Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL>
.\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll

<Reference Include=System.Numerics />
<Reference Include=System.Numerics .Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL>
.\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll

<Reference Include=System.Runtime .CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL >
.\NPOISystem.Runtime.CompilerServices.Unsafe.5.0.0\lib\net45\System.Runtime.CompilerServices.Unsafe.dll

<Reference Include=System.Runtime .Serialization />
<Reference Include=System.Security />
<Reference Include=System.Security .AccessControl, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL>
.\packages\System.Security.AccessControl.6.0.0\lib\net461\System.Security.AccessControl.dll

<Reference Include=System.Security .Cryptography.Xml, Version=6.0.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL >
.\packages\System.Security.Cryptography.Xml.6.0.1\lib\net461\System.Security.Cryptography.Xml.dll

<Reference Include=System.Security .Principal.Windows, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL >
.\packages\System.Security.Principal.Windows.5.0.0\lib\net461\System.Security.Principal.Windows.dll

<Reference Include=System.Text .Encoding.CodePages, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL >
.\packages\System.Text.Encoding.CodePages.5.0.0\lib\net461\System.Text.Encoding.CodePages.dll

Configure reference content

 d. There are many online cases for exporting Excel and reading Excel. You can expand it later.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/811205

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索