账单1.3-winform版

Winform下program 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
namespace WindowsFormsApp1
{
    class Txthander// 操作TXT文件类
    {
        private string filefullname;
        public string Filefullname
        {set; get;  }
        public string GetAlltext(string fileFullName)//方法:读取文件中的所有内容
        {  
            return File.ReadAllText(fileFullName); 
        }
        public string GetFirstLine(string filefullname)//读取文件中的第一行
        {
            string[] strs1 = File.ReadAllLines(filefullname);
            int a = strs1.Length;
            return strs1[0];
         }
        public string GetFinalLine(string filefullname)//读取最后一行字符
        {
            string[] strs1 = File.ReadAllLines(filefullname);
            int a = strs1.Length;
            return strs1[a - 1];
        }
        public void WriteAfterFinalLine(string finalLine, string filefullname)//最后一行写入内容,不覆盖原来的内容
        {
            StreamWriter sw = new StreamWriter(filefullname, true);
            sw.WriteLine(finalLine);
            sw.Close();
        }
        public string[] TransferFinalLineToNumber(string filefullname)//将file fullname中的内容最后一行转成一个字符串数组
        {
            string[] strs1 = File.ReadAllLines(filefullname);
            int a = strs1.Length;
            char[] seperator = { ' ' };
            return strs1[a - 1].Split(seperator);
        }
        public string[] TransferOneLineToNumberArr(string filefullname,int n)//将file fullname中的内容某一行转成一个字符串数组
        {
            string[] strs1 = File.ReadAllLines(filefullname);
           
            char[] seperator = { ' ' };
            return strs1[n - 1].Split(seperator);
        }
        public string JoinToString(string[] valuegroup)//将字符串数组连接成一个字符串
        {
            return string.Join(" ", valuegroup);
        }

    }
    class ZhangDan : Txthander
    {
        public double TotalValue(string[] valuegroup)//将最后一行,重新计算total值
        {
            double totalValue = 0;
            for (int i = 0; i < valuegroup.Length - 1; i++)
            {
                totalValue += double.Parse(valuegroup[i]);
            }
            return totalValue;
        }
        public void CheckMoney(string filefullname,int sourceIndex,int targetIndex,double numberTotransfer)//进行不同账户中的金额转移,最后将新的记录加到到txt文件中的最后一行
        {
            string[] valuegroup = TransferFinalLineToNumber(filefullname);//获取最后一行的数字,转化成数组
           double sourcePosition = double.Parse(valuegroup[sourceIndex]); //取出源序列号位置的初始值
           double targetPosition = double.Parse(valuegroup[targetIndex]);//目标位置的值初始值
            sourcePosition -= numberTotransfer;
            targetPosition += numberTotransfer;
            valuegroup[sourceIndex] = Convert.ToString(sourcePosition);//  源位置更新后的值
            valuegroup[targetIndex] = Convert.ToString(targetPosition);//目标位置更新后的值
            WriteAfterFinalLine(JoinToString(valuegroup), filefullname);//将valuegroup整理格式后,做为末行写入filefullName中。
        }
    }

    static class Program
    {  
       
        ///
        /// 应用程序的主入口点。
        ///

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            //以下为在winform中调用控制台
       
            

        }
    }
}
----------------------------------
Form1 类代码
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.IO;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ZhangDan ta = new ZhangDan();
           
           //string peiZhiPath = @"J:\b.txt";
           //ta.Filefullname = ta.GetFirstLine(@"J:\a.txt");
           // MessageBox.Show(ta.Filefullname);
           
            string[] valuegroup = ta.TransferFinalLineToNumber(@"J:\a.txt");
 
            textBox1.Text = Convert.ToString(valuegroup[0]);
            textBox2.Text = Convert.ToString(valuegroup[1]);
            textBox3.Text = Convert.ToString(valuegroup[2]);
            textBox4.Text = Convert.ToString(valuegroup[3]);
            textBox5.Text = Convert.ToString(valuegroup[4]);
            textBox6.Text = Convert.ToString(valuegroup[5]);
            textBox7.Text = Convert.ToString(valuegroup[6]);
            textBox8.Text = Convert.ToString(valuegroup[7]);
            textBox9.Text = Convert.ToString(valuegroup[8]);
            textBox10.Text = Convert.ToString(valuegroup[9]);
            textBox11.Text = Convert.ToString(valuegroup[10]);
            string[] arrForListBox = ta.TransferOneLineToNumberArr(@"J:\b.txt", 3);
            int i = 0;
            foreach (var item in arrForListBox)
            {
                listBox1.Items.Add(arrForListBox[i]);
                listBox2.Items.Add(arrForListBox[i]);
                i++;
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            ZhangDan ta = new ZhangDan();
            string peiZhiPath = @"J:\a.txt";
            //ta.Filefullname = ta.GetFirstLine(peiZhiPath);
            int aa = listBox1.SelectedIndex;
            int bb = listBox2.SelectedIndex;
            ta.CheckMoney(peiZhiPath, aa, bb, double.Parse(textBox12.Text));

            string[] valuegroup = ta.TransferFinalLineToNumber(peiZhiPath);
            textBox1.Text = Convert.ToString(valuegroup[0]);
            textBox2.Text = Convert.ToString(valuegroup[1]);
            textBox3.Text = Convert.ToString(valuegroup[2]);
            textBox4.Text = Convert.ToString(valuegroup[3]);
            textBox5.Text = Convert.ToString(valuegroup[4]);
            textBox6.Text = Convert.ToString(valuegroup[5]);
            textBox7.Text = Convert.ToString(valuegroup[6]);
            textBox8.Text = Convert.ToString(valuegroup[7]);
            textBox9.Text = Convert.ToString(valuegroup[8]);
            textBox10.Text = Convert.ToString(valuegroup[9]);
            textBox11.Text = Convert.ToString(valuegroup[10]);
 
 
        }
    }
}

你可能感兴趣的:(账单1.3-winform版)