listbox to rictbox

本文介绍了一个使用C#实现的应用程序,该程序从XML文件中读取配置,并利用拖放功能将列表项插入到富文本框中作为表达式的一部分。此外,文章还展示了如何解析这些表达式并进行数学计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;
using System.Text.RegularExpressions;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        string path;
        string id;
        string yt;
        string c;
        Hashtable ht = new Hashtable();
        Hashtable ht1 = new Hashtable();
        XmlDocument doc = new XmlDocument();  
        string txtFormula;

        private void Form1_Load(object sender, EventArgs e)
        {

            path = "e://config.xml";

            doc.Load(path);
            attributesOpertion oper = new attributesOpertion();
            oper.Doc = doc;
            id = "ST3";
            ht = oper.GetSummary(id);
            foreach (string st in ht.Values)
            {
                listBox1.Items.Add(st);
            }
            ht1 = oper.GetDetail(id);
            foreach (string st in ht1.Values)
            {
                listBox1.Items.Add(st);
            }


            this.richTextBox1.DragEnter +=new DragEventHandler(richTextBox1_DragEnter);
            this.richTextBox1.DragDrop +=new DragEventHandler(richTextBox1_DragDrop);
            richTextBox1.AllowDrop = true;

        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            ListBox lb = ((ListBox)sender);
            Point pt = new Point(e.X, e.Y);
            int index = lb.IndexFromPoint(pt);
            if (index >= 0)
            {
                lb.DoDragDrop(lb.Items[index].ToString(), DragDropEffects.All);
            }
           
        }


        private void richTextBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effect = DragDropEffects.All;

            c = this.richTextBox1.Text;
        }

        private void richTextBox1_DragDrop(object sender, DragEventArgs e)
        {
           
            yt = (String)e.Data.GetData(DataFormats.Text).ToString();

            foreach (DictionaryEntry de in ht)
            {
                if (de.Value.ToString() == yt)
                {
                    yt = de.Key.ToString();
                    break;
                }
            }

            foreach (DictionaryEntry de in ht1)
            {
                if (de.Value.ToString() == yt)
                {
                    yt = de.Key.ToString();
                    break;
                }
            }

             string strInsertText = "@" + yt;
            int start = this.richTextBox1.SelectionStart;
            this.richTextBox1.Text = this.richTextBox1.Text.Insert(start, strInsertText);
            this.richTextBox1.Focus();
            this.richTextBox1.SelectionStart = start;
            this.richTextBox1.SelectionLength = strInsertText.Length;
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            addOneNode(path);
            MathParser.MathParser Fun = new MathParser.MathParser();
            bool OK;
            double y;
         txtFormula = richTextBox1.Text;
            Hashtable ft = new Hashtable();
            ft.Add("@STM1", 1);
            ft.Add("@SVAL1", 2);
            ft.Add("@STM2", 3);
            ft.Add("@SVAL2", 4);
            ft.Add("@DVAL2", 5);
            ft.Add("@DVAL1", 6);
            ft.Add("@DTM1", 7);
            TxtValue(ft);
            OK = Fun.StoreExpression(txtFormula);
            if (OK == false)
            {
                MessageBox.Show("表?式不正?");
            }
            y = Fun.Eval1(0);
            textBox1.Text = Convert.ToString(y);
            //if (textBox1.Text=="1")
            //{
            //    textBox1.Text = Convert.ToString(true);
            //}
        }

        private void addOneNode(string filePath)
        {
            doc.Load(filePath);
            string xPath = "//HILS/zzz";
            if (exits(xPath) == true)
            {
                XmlElement xel = (XmlElement)doc.SelectSingleNode(xPath);
                xel.RemoveAll();
                xel.SetAttribute("str", richTextBox1.Text);
                doc.Save(path);
            }
            else
            {
                XmlNode root = doc.SelectSingleNode("//HILS");
                XmlElement xel = doc.CreateElement("zzz");
                root.AppendChild(xel);
                xel.SetAttribute("str", richTextBox1.Text);
                doc.Save(path);
            }
        }

        private bool exits(string xPath)
        {
            XmlNodeList nodeList;
            nodeList = doc.SelectNodes(xPath);
            if (nodeList.Count == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public string TxtValue(Hashtable tt)
        {
            foreach (DictionaryEntry de in tt)
            {
               txtFormula = txtFormula.Replace(de.Key.ToString(),de.Value.ToString() );
            }
            return txtFormula;
       
        }

        private void ?制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Copy();??
        }

        private void 撤?ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //richTextBox1.Undo();??
            richTextBox1.Text = c;
        }

        private void 剪?ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Cut();??
        }

        private void 粘?ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Paste();?
        }

        private void 全?ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();??
        }

        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值