列表框中的多个结果未对齐,无法打印

本文关键字:对齐 打印 结果 列表 | 更新日期: 2023-09-27 18:19:03

我已经为一些类工作创建了一个程序,它都工作得很好,但我有一些问题,插入到列表框中相同行的多个信息位的对齐。

当我打印它时,它看起来很乱,在列表框中看起来也很乱。

有什么方法可以让我整理一下吗?我尝试过右pad,但没有任何乐趣,列表视图让我很困惑。下面是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using System.Collections;
namespace Assignment2
{
    public partial class frmCalculator : Form
    {
        bool blnDot = false;
        double dbAllPoints = 0;
        double dbAllMoney = 0;
        public frmCalculator()
        {
            InitializeComponent();
        }
        private void frmCalculator_Load(object sender, EventArgs e)
        {
            ddbItems.Items.Add("Glass");
            ddbItems.Items.Add("Paper");
            ddbItems.Items.Add("Beverage Cans");
            ddbItems.Items.Add("Tins");
            ddbItems.Items.Add("Milk Cartons");
            ddbItems.Items.Add("Juice Boxes");
            ddbItems.Items.Add("Plastics");
            ddbItems.Items.Add("Clothes");
        }
        private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '.'))
            {
                MessageBox.Show("Please input a number!", "Error");
                e.Handled = true;
            }
            if (e.KeyChar == '.')
            {
                if (blnDot == true) { e.Handled = true; }
                else { blnDot = true; }
            }
        }
        private void txtInput_MouseClick(object sender, MouseEventArgs e)
        {
            txtInput.Text = "";
        }
        private void btnCalculate_Click(object sender, EventArgs e)
        {
            String strItem = "";
            double dbMoney = 0;
            double dbPoints = 0;
            int intPoint = 0;
            double dbWeight = 0;
            if (((txtInput.Text == "")||(txtInput.Text=="Input the weight")||           (ddbItems.SelectedIndex==0)))
                {
                    MessageBox.Show("Please input a weight into the textbox and make a   selection from the drop down box", "Error");
                }
        else
        {
            if (ddbItems.SelectedIndex == 1)
            {
                intPoint = 7;
                strItem = ddbItems.Items[1].ToString();
            }
            if (ddbItems.SelectedIndex == 2)
            {
                intPoint = 8;
                strItem = ddbItems.Items[2].ToString();
            }
            if (ddbItems.SelectedIndex == 3)
            {
                intPoint = 10;
                strItem = ddbItems.Items[3].ToString();
            }
            if (ddbItems.SelectedIndex == 4)
            {
                intPoint = 10;
                strItem = ddbItems.Items[4].ToString();
            }
            if (ddbItems.SelectedIndex == 5)
            {
                intPoint = 3;
                strItem = ddbItems.Items[5].ToString();
            }
            if (ddbItems.SelectedIndex == 6)
            {
                intPoint = 3;
                strItem = ddbItems.Items[6].ToString();
            }
            if (ddbItems.SelectedIndex == 7)
            {
                intPoint = 5;
                strItem = ddbItems.Items[7].ToString();
            }
            if (ddbItems.SelectedIndex == 8)
            {
                intPoint = 6;
                strItem = ddbItems.Items[8].ToString();
            }
            dbWeight = Convert.ToDouble(txtInput.Text);
            dbPoints = intPoint * dbWeight;
            dbMoney = dbPoints * 0.01;
            dbAllPoints = dbAllPoints + dbPoints;
            dbAllMoney = dbAllMoney + dbMoney;
            lblTotals.Visible = true;
            lblTotals.Text = "You have " + dbAllPoints.ToString() + " points, and you have earned £" + dbAllMoney.ToString("0.00");
            lstResults.Items.Add(strItem + "   " + dbWeight.ToString() + "kg   " + dbPoints.ToString() + " points   £" + dbMoney.ToString("0.00"));
            txtInput.Text = "Input the weight";
            ddbItems.SelectedIndex = 0;
            blnDot = false;
        }
      }
    private void btnEnd_Click(object sender, EventArgs e)
    {
        frmWelcome frmWelcome = (frmWelcome)Application.OpenForms["frmWelcome"];
        frmWelcome.Close();
        this.Dispose();
    }
    private void btnReset_Click(object sender, EventArgs e)
    {
        DialogResult result;
        result = MessageBox.Show("Are you sure you want to reset everything?", "Confirm", MessageBoxButtons.YesNo);
        if (result == DialogResult.No) return;
        txtInput.Text = "Input the weight";
        lstResults.Items.Clear();
        ddbItems.SelectedIndex = 0;
        lblTotals.Text = "";
        lblTotals.Visible = false;
        blnDot = false;
    }
    private void btnPrint_Click(object sender, EventArgs e)
    {
        int intMax;
        intMax = lstResults.Items.Count;
        String[] arrResults = new String[intMax];
        int intLoop;
        for (intLoop = 0; intLoop < intMax; intLoop++)
        {
            arrResults[intLoop] = lstResults.Items[intLoop].ToString();
        }
        Array.Sort(arrResults);
        lstResults.Items.Clear();
        for (intLoop = 0; intLoop < intMax; intLoop++)
        {
            lstResults.Items.Add(arrResults[intLoop]);
        }
        printDocument1.Print();
    }
    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        String strLine = "";
        int intLoop;
        Font pfont = new Font("Verdana", 18, GraphicsUnit.Point);
        int intLine = 75;
        strLine = "Item                    Weight  Points    Money";
        e.Graphics.DrawString(strLine, pfont, Brushes.Black, 75, intLine);
        strLine = "";
        intLine = intLine + 30;
        intLine = intLine + 30;
        for (intLoop = 0; intLoop < lstResults.Items.Count; intLoop++)
        {
            strLine = strLine +lstResults.Items[intLoop];
            e.Graphics.DrawString(strLine, pfont, Brushes.Black, 75, intLine);
            intLine = intLine + 30;
            strLine = "";
        }
        intLine = intLine + 30;
        strLine = lblTotals.Text;
        e.Graphics.DrawString(strLine, pfont, Brushes.Black, 75, intLine);
        strLine = "";
        intLine = intLine + 30;
    }
}

}

列表框中的多个结果未对齐,无法打印

您应该使用DataGridView控件而不是ListBox,因为您正在尝试显示"列"信息。

同样,在打印时,您也应该为每列执行DrawString,以便它们正确排列。

如果你想继续你正在做的事情,那么你应该使用像Courier这样的单间距字体,而不是Verdana,并计算单词长度之间的空格。