Examples of QuickStart C#

Keywords: C#

Recently, we need to use C# and javascript to build and learn the virtual platform, so we can only say that we can get started quickly, and the later improvement needs to be supplemented by the project.
First of all, what is the virtual reality platform? In short, it is to simulate an operation on the web page, such as computer hardware disassembly experiment or coding operation experiment and so on. The following figure shows the virtual experiment platform in our school, which has many experiment libraries.
All the following basic knowledge about C# will not be repeated. For example, loop structure and object-oriented design will not be described. If you are interested, you can see my previous java Tutorial (I believe you can!) here you can directly start the application example.
Your third company is the biggest driving force for me to update.
All the code links below are attached. There is still a lot of work in the later stage. Take this as an example, such as schema database, establishment of back-end data, etc.
(Baidu online disk) link: link
Extraction code: el1j


My current work is to build such a platform, which requires Unity 2020,Csharp,Node.js,MySQL and other knowledge. So we will study here in the future.
Csharp is an object-oriented programming language, which is very similar to Java( java notes this time ), it has many powerful programming functions, so it is favored by the majority of programmers.
Time is limited, so the detailed content here will not be repeated, and the code is directly attached here for description.

Basic program structure

Nothing else, just look at the frame.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
        }
    }
}

Many syntax structures, object-oriented and other knowledge are no longer repeated (see the java section for details, and the syntax is almost the same). Here you can directly start the form application.

Windows Applications

First summary procedure

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;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //Set the title text of this form
            this.Text = "first Windows program";
            //Create a label control and set properties
            Label lblshow = new Label();
            lblshow.Location = new Point(50, 60);
            lblshow.AutoSize = true;
            lblshow.Text = "Hello world";
            this.Controls.Add(lblshow);
        }
    }
}

Operation effect diagram

Win32 program with input function

private void label1_Click(object sender, EventArgs e)
        {
            //Define string constants
            string strResult;
            //Extract text in text box
            strResult = textBox1.Text + ",Hello, welcome to this program!";
            //Display results
            label2.Text = strResult;
        }

Some instructions on the procedure

using System represents a class introduced into the system namespace. System namespace is the most basic namespace of. NET, which contains the most basic classes and declarations. If you do not use the system namespace, the source code cannot be compiled.
C # is a completely object-oriented language, which must be placed in classes. A program contains at least one class.

Some application examples

Simple text editor

Main method code

	private void btnFont_Click(object sender, EventArgs e)
        {
            fontDialog1.Font = richTextBox1.Font;
            if (fontDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.Font = fontDialog1.Font;
            }
        }

        private void btnColor_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = richTextBox1.ForeColor;
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.ForeColor = colorDialog1.Color;
            }
        }

Simple editor diagram

User login

Main methods

		private void btnLogin_Click(object sender, EventArgs e)
        {
            string userName = txtName.Text;
            string password = txtPwd.Text;
            if (userName == "admin" && password == "123")
            {
                MessageBox.Show("Welcome to the personal finance system!", "Login succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("The user name or password you entered is incorrect!", "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPwd.Text = "";
            txtName.Focus();
        }

Operation result diagram

Add personal revenue and expenditure details

Main methods

 private void rdoExpenditure_CheckedChanged(object sender, EventArgs e)
        {
            cboCategory.Items.Clear();//Clear all items in the combo box
            if (rdoExpenditure.Checked == true)
            {
                cboCategory.Items.Add("Living consumption");
                cboCategory.Items.Add("fixed assets");
                cboCategory.Items.Add("Recreation & Entertainment");
                cboCategory.Items.Add("Medical drugs");
                cboCategory.Items.Add("Education and training");
                cboCategory.Items.Add("Other expenses");
            }
            else
            {
                cboCategory.Items.Add("Work income");
                cboCategory.Items.Add("income from investment");
                cboCategory.Items.Add("Other income");
            }
            cboCategory.SelectedIndex = 0;
        }

        private void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstItem.Items.Clear();
            switch (cboCategory.SelectedItem.ToString())
            {
                case "Living consumption":
                    lstItem.Items.Add("Restaurant");
                    lstItem.Items.Add("articles for daily use");
                    break;
                case "fixed assets":
                    lstItem.Items.Add("clothing");
                    lstItem.Items.Add("Household Electric Appliances");
                    break;
                case "Recreation & Entertainment":
                    lstItem.Items.Add("Travel");
                    break;
                case "Medical drugs":
                    lstItem.Items.Add("drugs");
                    break;
                case "Education and training":
                    lstItem.Items.Add("tuition");
                    break;
                case "Work income":
                    lstItem.Items.Add("wages");
                    break;
                case "income from investment":
                    lstItem.Items.Add("interest");
                    break;
            }
            cboCategory.SelectedIndex = 0;
        }

btnSave is saved in the text box and subsequently written to the database

private void btnSave_Click(object sender, EventArgs e)
        {
            rtxtRemarks.Clear();
            rtxtRemarks.AppendText("The information to save is:\n");
            rtxtRemarks.AppendText(cboCategory.SelectedItem.ToString());
            rtxtRemarks.AppendText("-");
            rtxtRemarks.AppendText(lstItem.SelectedItem.ToString());
            rtxtRemarks.AppendText("\n date");
            rtxtRemarks.AppendText(dtpDate.Value.ToLongDateString());
            rtxtRemarks.AppendText("\n explain");
            rtxtRemarks.AppendText(txtExplain.Text);
            rtxtRemarks.AppendText("\n Payee");
            if (chkOwn.Checked) rtxtRemarks.AppendText(chkOwn.Text);
            if (chkFamily.Checked) rtxtRemarks.AppendText(","+chkFamily.Text);
            if (chkFriend.Checked) rtxtRemarks.AppendText("," + chkFriend.Text);
            if (chkColleague.Checked) rtxtRemarks.AppendText("," + chkColleague.Text);
            if (chkRelative.Checked) rtxtRemarks.AppendText("," + chkRelative.Text);
            if (chkOther.Checked) rtxtRemarks.AppendText("," + chkOther.Text);
            rtxtRemarks.AppendText("\n amount of money");
            rtxtRemarks.AppendText(numAmount.Value.ToString());
        }

Schematic diagram of operation results

About our interface

Main method code

private void btnYes_Click(object sender, EventArgs e)
        {
            this.Close();
        }

Add revenue expense item

Main method code:

private void rdoExpenditure_CheckedChanged(object sender, EventArgs e)
        {
            cboCategory.Items.Clear();
            cboCategory.Items.Add("Class I");
            if (rdoExpenditure.Checked == true)
            {
                cboCategory.Items.Add("Living consumption");
                cboCategory.Items.Add("fixed assets");
                cboCategory.Items.Add("Recreation & Entertainment");
                cboCategory.Items.Add("Medical drugs");
                cboCategory.Items.Add("Education and training");
                cboCategory.Items.Add("Other income");
            }
            else
            {
                cboCategory.Items.Add("Work income");
                cboCategory.Items.Add("income from investment");
                cboCategory.Items.Add("Other income");
            }
            cboCategory.SelectedIndex = 0;
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Please fill in the name of revenue and expenditure item!", "Incomplete information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else tabControl1.SelectedTab = tabPage2;
        }

        private void cboCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
            {
                if (txtName.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Please fill in the name of revenue and expenditure item!", "Incomplete information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);MessageBox.Show("Please fill in the name of revenue and expenditure item!", "Incomplete information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    rtxtMsg.Clear();
                    rtxtMsg.AppendText("The revenue expense items to be added are:");
                    rtxtMsg.AppendText(txtName.Text);
                    rtxtMsg.AppendText("\n Category:" + cboCategory.SelectedItem.ToString());
                    if (rdoExpenditure.Checked == true) rtxtMsg.AppendText("\n Is an expense type item");
                    else rtxtMsg.AppendText("\n It is an item of income type");
                }
            }
        }

Schematic diagram of operation results

SDI and MDI applications

Main method code attached

        private void sign out XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {

        }

        private void Add revenue expense item IToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddItems AddItemsFrm = new AddItems();
            AddItemsFrm.MdiParent = this;
            AddItemsFrm.Show();
        }

        private void tsmAddExp_Click(object sender, EventArgs e)
        {
            AddExpenditure AddExpFrm = new AddExpenditure();
            AddExpFrm.MdiParent = this;
            AddExpFrm.Show();
        }

        private void tsmAbout_Click(object sender, EventArgs e)
        {
            AboutForm aboutForm = new AboutForm();
            aboutForm.MdiParent = this;
            aboutForm.Show();
        }

        private void tsmStatistics_Click(object sender, EventArgs e)
        {

        }

        private void tsmUser_Click(object sender, EventArgs e)
        {
            Login loginForm = new Login();
            loginForm.MdiParent = this;
            loginForm.Show();
        }

Combined with the previous, the following operation results are achieved



So far, the syntax of C# has come to an end, the UI interface design has been completed, and the back-end is what follows. We have to supplement the knowledge of the database quickly.

Posted by renegade33 on Wed, 01 Dec 2021 15:35:39 -0800