Menu

Sabtu, 21 Mei 2016

23 Dialog Interaction

Pada post sebelumnya kita telah membahas salah satu Dialog Interaction yaitu MessageBox. Dialog Interaction berfungsi untuk menampilkan interaktif kepada User(pengguna) baik pesan(MessageBox), Value/Nilai(InputBox), Folder(FolderBrowseDialog), Open(OpenDialog), Save(SaveDialog), Print (PrintDialog), Font(FontDialog) dan Color(ColorDialog).
Sebenarnya ada banyak dari jenis interaction seperti Shell, Environ, Beep, GetSetting, SaveSettingDeleteSetting, dan lain sebagainya, akan tetapi semua itu bukanlah berupa Dialog. Sekarang kita akan membahas Input, Open, Save, Print dan Color Dialog.

Contoh Soal
Sekarang buatlah 1 Project dengan nama cs_12dialog(untuk C#) atau vb_12dialog(untuk VB). Masukan 7 object Button. 1 object Label kedalam form dan ("object FolderBrowserDialog, OpenFileDialog, SaveFileDialog, PrintDialog, FontDialog, ColorDialog" hanya optional, kita bisa menggunakan object dialog tersebut atau menggunakan reference. Disini kita coba menggunakan Reference bukan object) seperti gambar 23.1 dan Gambar 23.2 berikut ini.

Gambar 23.1

 Gambar 23.2

Atur Properties sebagai berikut:

Object Properties Value
button1 (Name) btnInput
Teks Input
button2 (Name) btnOpen
Teks Input
button3 (Name) btnSave
Teks Save
button4 (Name) btnOpen
Teks
button5 (Name) btnPrint
Teks Print
button6 (Name) btnFont

Teks Font
button7 (Name) btnColor
Teks Color

InputBox
InputBox merupakan sebuah function, yang tentu didalam C# tidak tersedia function, oleh karena itu kita dapat menambahkan Reference Microsoft.VisualBasic(khusus C#, VB tidak diperlukan). Klik 2x Button Input kemudian ketikan code berikut.
//C#
private void btnInput_Click(object sender, EventArgs e)
{
    string a;
    a = Interaction.InputBox("Ketik Apa Saja", "Contoh InputBox");
    if (a != ""){
        MessageBox.Show("Yang anda ketik adalah " + a);
    }
    else{
        MessageBox.Show("Anda menekan Cancel");
    }
}
'VB
Private Sub btnInput_Click(sender As Object, e As EventArgs) Handles btnInput.Click
    Dim a As String
    a = InputBox("Ketiklah apa saja", "Contoh InputBox")
    If a <> "" Then
        MessageBox.Show("yang anda ketik adalah " & a)
    Else
        MessageBox.Show("anda menekan cancel")
    End If
End Sub
Jalankan program kemudian klik button Input, cobalah anda ketik apa saja kemudian klik button OK atau menekan button Cancel. Hasilnya akan seperti gambar 23.3-5 dibawah ini.

Gambar 23.3

Gambar 23.4

Gambar 23.5


FolderBrowserDialog
Folder Browser Dialog merupakan sebuah dialog untuk memilih lokasi drive atau folder(tidak termasuk file). Klik 2x Button Folder kemudian ketikan code berikut.
//C#
private void btnFolder_Click(object sender, EventArgs e){
    FolderBrowserDialog folder = new FolderBrowserDialog();
    folder.Description = "Pilihlah folder anda";
    folder.ShowDialog();

    if (folder.SelectedPath != ""){
        MessageBox.Show("Lokasi yang anda pilih adalah " + folder.SelectedPath);
    }
    else{
        MessageBox.Show("Anda menekan Cancel");
    }
}
'VB
Private Sub btnFolder_Click(sender As Object, e As EventArgs) Handles btnFolder.Click
    Dim folder As FolderBrowserDialog = New FolderBrowserDialog()
    folder.Description = "Pilihlah folder anda"
    folder.ShowDialog()

    If folder.SelectedPath <> "" Then
        MessageBox.Show("Lokasi yang anda pilih adalah " + folder.SelectedPath)
    Else
        MessageBox.Show("Anda menekan Cancel")
    End If
End Sub

Jalankan program kemudian klik button Folder, cobalah anda pilih lokasi atau folder apa saja kemudian klik button OK atau menekan button Cancel. Hasilnya akan seperti gambar 23.6-8 dibawah ini.

Gambar 23.6

Gambar 23.7

Gambar 23.8


SaveDialog
Sebelum memulai save dialog khusus C# ketiklah kode berikut: using System.IO; kita akan mengambil class File dari Reference System.IO tersebut, tidak perlu klik Add Reference(cukup ketikan saja using System.IO;). Letakan code tersebut diatas seperti gambar 23.9 dibawah ini.

Gambar 23.9

Sedangkan untuk VB ketikan code Imports System.IO dan letakan code paling atas seperti gambar 23.10 dibawah ini.


Gambar 23.10

Sekarang kita coba menyimpan file txt dari apa yang kita ketik. Klik 2x button Save kemudian ketikan code berikut.
//C#
private void btnSave_Click(object sender, EventArgs e)
{
    string a = Interaction.InputBox("Ketik Apa Saja", "Contoh InputBox");
    if (a != ""){
         SaveFileDialog save = new SaveFileDialog();
         save.Filter = "Text File (*.txt)|*.txt|Semua file (*.*)|*.*";
         //save.FilterIndex = 2;
         //save.InitialDirectory = "C:";
         save.RestoreDirectory = true;
         save.Title = "Simpan File Txt";
         //if (save.ShowDialog() == DialogResult.OK)
         save.ShowDialog();
         if (save.FileName != ""){
             File.WriteAllText(save.FileName, a);
             MessageBox.Show("File tersimpan di " + save.FileName);
         }
         else{
             MessageBox.Show("Anda tidak menyimpan File txt");
         }   
    }
    else{
        MessageBox.Show("Anda menekan Cancel");
    }
}
'VB
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim a As String
    a = InputBox("Ketik Apa Saja", "Contoh InputBox")
    If a <> "" Then
        Dim save As SaveFileDialog = New SaveFileDialog
        save.Filter = "Text File (*.txt)|*.txt|Semua file(*.*)|*.*"
        'save.FilterIndex = 2
        'save.InitialDirectory = "C:\"
        save.RestoreDirectory = True
        save.Title = "Simpan File Txt"
        'If save.ShowDialog = DialogResult.OK Then
        save.ShowDialog()
        If save.FileName<> "" Then
            File.WriteAllText(save.FileName, a)
            MessageBox.Show("File tersimpan di " + save.FileName)
        Else
            MessageBox.Show("Anda tidak menyimpan File txt")
        End If
    Else
        MessageBox.Show("Anda menekan Cancel")
    End If
End Sub
Jalankan program dan klik button Save, cobalah untuk menekan button OK maupun Cancel, maka hasilnya akan seperti gambar 23.11-14 dibawah ini.

Gambar 23.11

Gambar 23.12

Gambar 23.13

Gambar 23.14


OpenDialog
Jika pada Save Dialog kita menggunakan class File, sekarang pada Open Dialog kita menggunakan class StreamReader dari Reference System.IO untuk membaca file txt. Klik 2x button Open kemudian ketikan code berikut.
//C#
private void btnOpen_Click(object sender, EventArgs e){
    OpenFileDialog open = new OpenFileDialog();
    open.Filter = "Text File (*.txt)|*.txt|Semua file (*.*)|*.*";
    //open.FilterIndex = 2;
    //open.InitialDirectory = "C:\"
    open.RestoreDirectory = true;
    open.Title = "Pilih file txt";
    //if (open.ShowDialog() == DialogResult.OK)
    open.ShowDialog();
    if (open.FileName != ""){
        try{
            using (StreamReader sreader = new StreamReader(open.OpenFile())){
                  MessageBox.Show(sreader.ReadToEnd());
            }    
        }
        catch (Exception ex){
            MessageBox.Show("Terjadi kesalahan " + ex.Message);
        }
    }
    else{
         MessageBox.Show("Anda tidak memilih File txt");
    }
}
'VB
Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
    Dim open As OpenFileDialog = New OpenFileDialog
    open.Filter = "Text File (*.txt)|*.txt|Semua file (*.*)|*.*"
    'open.FilterIndex = 1
    'open.InitialDirectory = "C:\"
    open.RestoreDirectory = True
    open.Title = "Pilih file txt"
    'If save.ShowDialog = DialogResult.OK Then
    open.ShowDialog
    If open.FileName <> "" Then
        Try
            Using sreader As New StreamReader(open.OpenFile)
                MessageBox.Show(sreader.ReadToEnd)
            End Using                
        Catch ex As Exception
            MessageBox.Show("Terjadi kesalahan " & ex.Message)         
        End Try
    Else
        MessageBox.Show("Anda tidak memilih File txt")
    End If
End Sub
Jalankan program kemudian klik button Open, pilihlah file txt lalu klik button Open atau Cancel dan hasil contohnya seperti berikut.

Gambar 23.15

Gambar 23.16

Gambar 23.17

PrintDialog
Sebelum memulai Print Dialog, untuk C# tambahkan code berikut using System.Drawing.Printing; sedangkan untuk VB tambahkan code berikut Import System.Drawing.Printing letakan diatas, seperti contoh gambar 23.9. setelah itu klik 2x pada button Print kemudian ketikan code berikut.
//C#
private void btnPrint_Click(object sender, EventArgs e){
    OpenFileDialog open = new OpenFileDialog();
    open.Filter = "Text File (*.txt)|*.txt|Semua file (*.*)|*.*";
    //open.FilterIndex = 2;
    //open.InitialDirectory = @"C:\";
    open.RestoreDirectory = true;
    open.Title = "Pilih file txt yang akan dicetak";
    //if (open.ShowDialog() == DialogResult.OK)
    open.ShowDialog();
    if (open.FileName != ""){
        try{
            StreamReader sreader;
            sreader = new StreamReader(open.FileName);
            string yangdiprin = sreader.ReadToEnd();
            try{
                PrintDialog prin = new PrintDialog();
                prin.AllowCurrentPage = true;
                prin.AllowPrintToFile = true;
                prin.AllowSelection = true;
                prin.AllowSomePages = true;
                PrintDocument prindoc = new PrintDocument();
                prindoc.DocumentName = "CSVB2015";
                prindoc.PrintPage += delegate (object senderprindoc, PrintPageEventArgs eprindoc){
                    eprindoc.Graphics.DrawString(yangdiprin, new Font("Arial", 10), new SolidBrush(Color.Black), 30,40);
                    };
                prin.Document = prindoc;
                if (prin.ShowDialog() == DialogResult.OK){
                    prindoc.Print();
                }
                else{
                    MessageBox.Show("Anda membatalkan Print");
                }
            }
            finally{
                sreader.Close();
            }
                }
                catch (Exception ex){
                    MessageBox.Show("Terjadi kesalahan " + ex.Message);
        }
    }
    else{
        MessageBox.Show("Anda tidak memilih File txt");
    }
}
'VB
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    Dim open As OpenFileDialog = New OpenFileDialog
    open.Filter = "Text File (*.txt)|*.txt|Semua file (*.*)|*.*"
    'open.FilterIndex = 1
    'open.InitialDirectory = "C:\"
    open.RestoreDirectory = True
    open.Title = "Pilih file txt yang akan dicetak"
    'If open.ShowDialog = DialogResult.OK Then
    open.ShowDialog
    If open.FileName <> "" Then
        Try
            Dim sreader As StreamReader
            sreader = New StreamReader(open.FileName)
            Dim yangdiprin As String = sreader.ReadToEnd()
            Try
                Dim prin As New PrintDialog()
                prin.AllowCurrentPage = True
                prin.AllowPrintToFile = True
                prin.AllowSelection = True
                prin.AllowSomePages = True
                Dim prindoc As New PrintDocument()
                prindoc.DocumentName = "CSVB2015"
                AddHandler prindoc.PrintPage, Sub(senderprindoc As Object, eprindoc As PrintPageEventArgs)eprindoc.Graphics.DrawString(yangdiprin, New Font("Arial", 10), New SolidBrush(Color.Black), 30, 40)
                prin.Document = prindoc
                If prin.ShowDialog = DialogResult.OK Then
                    prindoc.Print()
                Else
                    MessageBox.Show("Anda membatalkan Print")
                End If
            Finally
                sreader.Close()
            End Try
         Catch ex As Exception
            MessageBox.Show("Terjadi kesalahan " + ex.Message)
         End Try
    Else
         MessageBox.Show("Anda tidak memilih File txt")
    End If
End Sub

Jalankan program kemudian klik button Print, pilihlah file txt untuk dicetak lalu klik button Open atau Cancel dan hasil contohnya seperti berikut.

Gambar 23.18

Gambar 23.19

Gambar 23.20

Gambar 23.21

Gambar 23.22

FontDialog
Font Dialog berfungsi untuk menampilkan Font, Font style, Size untuk mengubah font sebuah object atau string dan lain sebagainya. Sebagai contoh kita akan mengganti font label1, seperti gambar 23.23 dibawah ini.

Gambar 23.23

//C#
private void btnFont_Click(object sender, EventArgs e){
    FontDialog fon = new FontDialog();
    if (fon.ShowDialog() == DialogResult.OK){
        label1.Font = new Font(fon.Font.Name, fon.Font.Size, fon.Font.Style, fon.Font.Unit);
    }
    else{
        MessageBox.Show("Anda membatalkan mengganti Font");
    }
}
'VB
Private Sub btnFont_Click(sender As Object, e As EventArgs) Handles btnFont.Click
    Dim fon As New FontDialog()
    if fon.ShowDialog() = DialogResult.OK Then
        label1.Font = New Font(fon.Font.Name, fon.Font.Size, fon.Font.Style, fon.Font.Unit)
    Else
        MessageBox.Show("Anda membatalkan mengganti Font")
    End If
End Sub
Jalankan program kemudian klik button Font, cobalah ganti Font, Font style, Size, kemudian klik OK. Hasilnya akan seperti contoh gambar dibawah ini.

Gambar 23.24

Gambar 23.25

Gambar 23.24

ColorDialog
Color Dialog
//C#
private void btnColor_Click(object sender, EventArgs e){
    ColorDialog bukancelanacolor = new ColorDialog();
    if (bukancelanacolor.ShowDialog() == DialogResult.OK){
        label1.ForeColor = bukancelanacolor.Color;
    }
    else{
        MessageBox.Show("Anda membatalkan mengganti warna");
    }
}
'VB
Private Sub btnColor_Click(sender As Object, e As EventArgs) Handles btnColor.Click
    Dim bukancelanacolor As new ColorDialog()
    If bukancelanacolor.ShowDialog() = DialogResult.OK Then
        label1.ForeColor = bukancelanacolor.Color
    Else
        MessageBox.Show("Anda membatalkan mengganti warna")
    End If        
End Sub
Jalankan program kemudian klik button Color, kemudian pilihlah warna dan hasilnya akan seperti contoh berikut.

Gambar 23.25

Gambar 23.26

Gambar 23.27


Sampai disini sample dari Dialog Interaction, selanjutnya kita akan membahas Color.

Password: csvb2015.blogspot.co.id

Tidak ada komentar :

Posting Komentar