WindowsForm 打印

 

打印:


打印对话框:printdialog
页面设置:pagesetupdialog
这两个对话框都需要通过设置printdocument来指定打印对象
printdocument:打印对象,必须要有,一块画板,用于打印机与打印内容之间中转,打印机打印的是printdoment
printDocument1_PrintPage:事件,每打印一页之前触发,用于给printdocument指定打印内容
通过画板把内容画到打印对象的页上:
System.Drawing.Font f = new System.Drawing.Font("宋体",12);
e.Graphics.DrawString(textBox1.Text,f,System.Drawing.Brushes.Aqua,5,5);
最后打印: 打印对话框那,如果打印对话框返回确定打印,就执行printdocument.print();

 

看一下打印界面

 

 

下面是打印界面的代码 (点击小加号打开)

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text;
            string upass = textBox2.Text;
            //连接数据库
            SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=123");
            conn.Open();
            //执行语句
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from users where uname='" + username + "'and upass='" + upass + "'";
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                //MessageBox.Show(dr["uname"].ToString());
                MessageBox.Show("登陆成功");

            }
            else
            {
                MessageBox.Show("登录失败");
            }
            conn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //添加数据到数据库

            string username = textBox1.Text;
            string upass = textBox2.Text;

            //连接数据库
            SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=123");
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "insert into users values ('" + username + "'," + upass + ") ";
            int count = cmd.ExecuteNonQuery();//增删改
            if (count > 0)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }
            conn.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string code = textBox3.Text;
            string uname = textBox1.Text;
            string upass = textBox2.Text;
            //连接数据库
            SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=123");
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "update users set uname='" + uname + "',upass='" + upass + "'where code=" + code;
            int count = cmd.ExecuteNonQuery();

            if (count > 0)
            {
                MessageBox.Show("修改成功");
            }
            else
            {
                MessageBox.Show("修改失败");
            }
            conn.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string code = textBox3.Text;
            //连接数据库
            SqlConnection conn = new SqlConnection("server=.;database=data1220;user=sa;pwd=123");
            conn.Open();
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "delete from user where code=" + code;
            cmd.ExecuteNonQuery();
            conn.Close();
        }

    }
}
打印的 代码

 

posted @ 2015-03-12 20:04  奔跑的小黄鸡  阅读(216)  评论(0编辑  收藏  举报