列表框中的多个结果未对齐以进行打印

时间:2011-12-30 19:01:45

标签: c# winforms listbox

我已经为一些类工作创建了一个程序并且一切正常,但是我在同一行的列表框中插入的多个信息对齐方面遇到了一些问题。

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

无论如何我可以把它吃一点吗?我已经尝试过没有快乐,并且列表视图让我感到困惑。这是我的代码:

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;
    }
}

}

1 个答案:

答案 0 :(得分:1)

您应该使用DataGridView控件而不是ListBox,因为您试图显示“列”信息。

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

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