PratikumASP#4

Top of Form
Kali ini saya akan memberikan tutorial untuk membuat Form Validation menggunakan ASP.NET, langsung saja:

1.   Jalankan VS 2012 dan New Project seperti pratikum yang sebelumnya.

2.   Maka akan tampil seperti ini


3.   Ubah nama Form1 -> ValidationForm



4.   Selanjutnya adalah proses membuat form. Inilah form milik saya.
 

Ini yang kita butuhkan :






Untuk mengganti nama maka :

5.   Selanjutnya klik dua kali pada button eksekusi dan tuliskan script dibawah ini


Script:

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

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

      

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bar1.Value = 0;


            if (a.Text == "")
            {
                x.SetError(a, "Nama");
            }
            else
            {
                x.SetError(a, "");
            }

            if (b.Text == "")
            {
                x1.SetError(b, "Nama");
            }
            else
            {
                x1.SetError(b, "");
            }

            if (c.Text == "")
            {
                x2.SetError(c, "Nama");
            }
            else
            {
                x2.SetError(c, "");
            }

            if (d.Text == "")
            {
                x3.SetError(d, "Nama");
            }
            else
            {
                x3.SetError(d, "");
            }

            //------------------------- KOSONG 1-----------------------------------------//
            if (a.Text == "" )
            {
                x.SetError(a, "Masukan nama");
                MessageBox.Show("Data kurang Lengkap, Lengkapi namamu!");
                for (int i = 0; i < bar1.Maximum; i++)
                {
                    bar1.Value = bar1.Value + 1;
                }


            }
            else
            {
                if (b.Text == "")
                {
                    x1.SetError(b, "Masukan NIS");
                    MessageBox.Show("Data kurang Lengkap, Lengkapi NIS!");
                    for (int i = 0; i < bar1.Maximum; i++)
                    {
                        bar1.Value = bar1.Value + 1;
                    }
                }
                else
                {
                    if (c.Text == "")
                    {
                        x2.SetError(c, "MAsukan Kelas");
                        MessageBox.Show("Data kurang Lengkap, Lengkapi Kelas!");
                        for (int i = 0; i < bar1.Maximum; i++)
                        {
                            bar1.Value = bar1.Value + 1;
                        }
                    }
                    else
                    {
                        if (d.Text == "")
                        {
                            x3.SetError(d, "mASUKAN SMP");
                            MessageBox.Show("Data kurang Lengkap, Lengkapi Asal SMP");
                            for (int i = 0; i < bar1.Maximum; i++)
                            {
                                bar1.Value = bar1.Value + 1;
                            }
                        }
                    }
                  
                }

            }
                 
          //----------------------------------------------------------//

            //--------------------------HASIL-------------------------------//
            if (d.Text != "" && a.Text !="" && c.Text !="" && b.Text !="")
            {
                for (int i = 0; i < bar1.Maximum; i++)
                {
                    bar1.Value = bar1.Value + 1;
                }
                MessageBox.Show("\nNama\t : " + a.Text + "\nNIS\t : " + b.Text + "\nKelas\t : " + c.Text + "\nAsal SMP\t : " + d.Text);
            }
        }

   

        private void b_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            a.ResetText();
            b.ResetText();
            c.ResetText();
            d.ResetText();
            bar1.Value = 0;
            x.SetError(a, "");
            x1.SetError(b, "");
            x2.SetError(c, "");
            x3.SetError(d, "");
        }
    }
}

Sekian tutorial dari saya. semoga bermanfaat :D

Komentar

Postingan Populer