PrintDocument dan PrintPreview dialog di C#

Pertama – tama silahkan anda buat terlebih dahulu projek baru anda dengan nama laporanprintdialog, kemudian atur designya seperti gambar dibawah ini.



  • Untuk komponen – komponen yang dibutuhkan lihat table propertie dibawah. Sementara untuk komponen textbox biartkan default saja.
Toolbox Properties Keterangan
label1 text Nomor Induk
label2 text Nama
label3 text Jenis Kelamin
label4 text Alamat
label5 text Kelas
button1 text print
button2 text Preview
dataGridView1 name dataGridView1
bindingNavigator1 name bindingNavigator1
bindingSource1 name bindingSource1
printDocument1 name printDocument1
printPreviewDialog1 name printPreviewDialog1

LISTING PROGRAM

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 MySql.Data.MySqlClient;
namespace laporanprintdialog
{
public partial class Form1 : Form
{
public string databases = “server=localhost; database=sekolah; uid=root; pwd=””;
public MySqlConnection koneksi;
public MySqlCommand command;
public MySqlDataAdapter adp;
public MySqlDataReader rdr;
public Form1()
{
InitializeComponent();
this.MaximizeBox = false;
this.MinimizeBox = false;
this.FormBorderStyle = FormBorderStyle.Fixed3D;
}
public DataTable tampil()
{
string sql = “select * from siswa”;
DataTable dt = new DataTable();
try
{
koneksi = new MySqlConnection(databases);
koneksi.Open();
command = new MySqlCommand(sql, koneksi);
adp = new MySqlDataAdapter(command);
adp.Fill(dt);
command.ExecuteNonQuery();
bindingSource1.DataSource = dt;
dataGridView1.DataSource = bindingSource1;
bindingNavigator1.BindingSource = bindingSource1;
}
catch (Exception ali)
{
MessageBox.Show(ali.Message);
}
koneksi.Close();
return dt;
}
public void pindahketextbox()
{
textBox1.Text = dataGridView1.CurrentRow.Cells[“nomor_induk”].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[“nama”].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[“jenkel”].Value.ToString();
textBox4.Text = dataGridView1.CurrentRow.Cells[“alamat”].Value.ToString();
textBox5.Text = dataGridView1.CurrentRow.Cells[“kelas”].Value.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
tampil();
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
pindahketextbox();
}
private void button2_Click(object sender, EventArgs e)
{
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
printDocument1.PrinterSettings.DefaultPageSettings.Landscape = false;
printDocument1.Print();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
var font = new Font(“Khmer UI”, 12);
e.Graphics.DrawString(“\t\t\t Data Siswa – Siswi \n\n\n”, font, Brushes.Black, 25, 10);
e.Graphics.DrawString(“NIM           \t:” + textBox1.Text + “\n\n”, font, Brushes.Black, 20, 30);
e.Graphics.DrawString(“NAMA          \t:” + textBox2.Text + “\n\n”, font, Brushes.Black, 20, 50);
e.Graphics.DrawString(“Jenis Kelamin \t:” + textBox3.Text + “\n\n”, font, Brushes.Black, 20, 80);
e.Graphics.DrawString(“Alamat        \t:” + textBox4.Text + “\n\n”, font, Brushes.Black, 20, 110);
e.Graphics.DrawString(“Kelas         \t:” + textBox5.Text + “\n\n”, font, Brushes.Black, 20, 130);
}
}
}

Pada tahap ini aplikasi sudah selesai dibuat silakan coba jalankan projek anda.

Tidak ada komentar:

Posting Komentar

Profile Page Screen - Flutter UI

Profile Page Berikut ini adalah contoh  source code untuk Design Profile Page menggunakan flutter,  sebelumnya jangan lupa untuk membua...