WinForms开发桌面应用程序

        项目需要写个配置的桌面程序,作为一名前端工程师,接到这活我是懵逼的,然而boss说了,公司人人都是全栈工程师。额,好吧,我信了你的鬼,开干开干!

        【百度百科】WinForms 脚本都是基于c#,winforms是做客户端软件,WinForm是.Net开发平台中对Windows Form的一种称谓。

对WinForms的第一印象是类似于dreamweaver,可以用视图做些简单的功能。先做个简单外壳,设置页面需要用到label、textBox、checkBox以及Button,如下图所示

WinForms开发桌面应用程序_第1张图片

       外壳做好了,接下来实现业务逻辑,逻辑并不难,主要是对xml格式的文件进行读取、修改、保存等操作,以及桌面程序confirm弹框等基本功能的实现。

xml文件内容:



  root
  111111
  test
  localhost
  1
  1
  1

Form1.cs中添加程序的一些功能:

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 System.Xml;
using System.Xml.Linq;

namespace WindowsFormsApp1
{
    public partial class MNConfig : Form
    {
        public MNConfig()
        {
            InitializeComponent();
        }

        static void read(string[] args)
        {
           
        }

        private void MNConfig_Load(object sender, EventArgs e)
        {
            getXml();
        }

        private void save_btn_Click(object sender, EventArgs e)
        {
            updatexml();
        }

        private void exit_btn_Click(object sender, EventArgs e)
        {
            var a = MessageBox.Show("确认不保存退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
            if (Convert.ToString(a) == "OK")
            {
                this.Close();
            }
        }

        private void getXml()
        {
            var responseData = new List();
            var data = new Dictionary();
            XDocument document = XDocument.Load(@"D:\feb\test.xml");
            XElement root = document.Root;
            
            XElement UserName = root.Element("UserName");
            XElement Password = root.Element("Password");
            XElement DatabaseName = root.Element("DatabaseName");
            XElement ServerIP = root.Element("ServerIP");

            XElement CT_LUNG = root.Element("CT_LUNG");
            XElement CT_RIB = root.Element("CT_RIB");
            XElement DR_CHEST = root.Element("DR_CHEST");
                   

            this.tbUserName.Text = UserName.Value;
            this.tbPassword.Text = Password.Value;
            this.tbDatabaseName.Text = DatabaseName.Value;
            this.tbServerIP.Text = ServerIP.Value;
           
            this.CT_LUNG.Checked = Convert.ToBoolean(Convert.ToInt16(CT_LUNG.Value));
            this.CT_RIB.Checked = Convert.ToBoolean(Convert.ToInt16(CT_RIB.Value));
            this.DR_CHEST.Checked = Convert.ToBoolean(Convert.ToInt16(DR_CHEST.Value));
        }

        private void updatexml()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"D:\feb\test.xml");
            XmlNode xns = xmlDoc.SelectSingleNode("config");
            XmlNodeList xnl = xns.ChildNodes;
            foreach(XmlNode xn in xnl)
            {
                switch (xn.Name)
                {
                    case "UserName":
                        xn.InnerText = this.tbUserName.Text;
                        break;
                    case "Password":
                        xn.InnerText = this.tbPassword.Text;
                        break;
                    case "DatabaseName":
                        xn.InnerText = this.tbDatabaseName.Text;
                        break;
                    case "ServerIP":
                        xn.InnerText = this.tbServerIP.Text;
                        break;
                    
                    //checkbox
                    case "CT_LUNG":
                        xn.InnerText = Convert.ToString(Convert.ToInt32(this.CT_LUNG.Checked));
                        break;
                    case "CT_RIB":
                        xn.InnerText = Convert.ToString(Convert.ToInt32(this.CT_RIB.Checked));
                        break;
                    case "DR_CHEST":
                        xn.InnerText = Convert.ToString(Convert.ToInt32(this.DR_CHEST.Checked));
                        break;
                    default:
                        break;
                }
            }
            xmlDoc.Save(@"D:\feb\test.xml");
            MessageBox.Show("保存成功!");
            this.Close();
        }
    }

}

 

你可能感兴趣的:(C#)