Friday, May 18, 2012

Custom Dialog trong winform

- Trong quá trình phát triển phần mềm thì việc phải hiệu chỉnh các công cụ và thư viện mặc định của hệ thống là một việc thường xuyên. Đối với window form cũng vậy. Bạn hãy thử nghỉ tất cả các chức năng của phần mềm mà bạn đang viết đều được việt hóa chỉ riêng hộp thống báo (MessageBox) thì giao diện nút nhấn là "Yes" và "No" như vậy sẽ làm cho phần mềm của bạn mất giá trị. Một vấn đề lớn nữa là nếu như khi triển khai phần mềm cho khách hàng qua thời gian sử dụng họ thông báo với bạn là phần mềm của bạn đang gặp lỗi và không thể chạy được như vậy bạn sẽ phản ứng thế nào ? Đến khách hàng hiệu chỉnh ? hay gọi điện thoại nhờ họ giải thích ? remote máy khách hàng ? các cách này coi ra hơi phiền phức. Nếu như bạn thống báo cho khách hàng của bạn là phần mềm của bạn hiện đang có lỗi và thêm một chức năng nhỏ trên đó cho phép phần mềm của bạn tự gởi thông báo lỗi chi tiết (exception phát sinh) về cho bạn thì sẽ giúp cho phần bạn được thông minh và có gí trị hơn.







[sourcecode language="CSharp" wraplines="false"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

private void btnShow_Click(object sender, EventArgs e)
{
try
{
int c = Convert.ToInt32(textBox1.Text) / Convert.ToInt32(textBox2.Text);
}
catch (Exception exa)
{
frmMessage frm = new frmMessage("Hệ thống có lỗi phát sinh, liên hệ monkey@abc.com để được giúp đỡ !", "Xác nhận", exa);
DialogResult result = frm.ShowDialog();
}
}
}
}
[/sourcecode]

[sourcecode language="CSharp" wraplines="false"]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;

namespace CustDialog
{
public partial class frmMessage : Form
{
string caption = "";
DialogResult ok = DialogResult.No;

public frmMessage(string Message, string Caption, Exception ex)
{
InitializeComponent();

label1.Text = Message;
caption = Caption;

this.Height = this.Height - 100;
this.richTextBox1.Visible = false;
this.richTextBox1.Text = ex == null ? "" : ex.ToString();
btnSend.Visible = (ex != null);
}

private void btnExp_Click(object sender, EventArgs e)
{
if (!this.richTextBox1.Visible)
{
this.richTextBox1.Visible = true;
this.Height = this.Height + 100;
this.btnExp.Text = "Thu nhỏ";
}
else
{
this.Height = this.Height - 100;
this.richTextBox1.Visible = false;
this.btnExp.Text = "Chi tiết";
}
}

private void btnSend_Click(object sender, EventArgs e)
{
this.Enabled = false;
string Sendto = "ngotuongdan01@gmail.com"; //Email Address to reciever
// tai khoan này các bạn sử đừng sử dụng để gửi tùm lum dùm tui nhe
string UserName = "ngotuongdan04@gmail.com"; //Ur Gmail address
string PassWord = "ngotuongdan"; //Gmail password
// this mail is my demo mail please not change it's password, tks alot
NetworkCredential loginInfo = new NetworkCredential(UserName, PassWord);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(UserName);
msg.To.Add(new MailAddress(Sendto.ToString()));
msg.Subject = "Error"+ DateTime.Now.ToString();
msg.Body = richTextBox1.Text;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.Port = 587;
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
this.Enabled = true; ;
}

private void frmMessage_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = (ok == DialogResult.No);
}

private void button1_Click(object sender, EventArgs e)
{
ok = MessageBox.Show("Are you sure to change student information?", "Change information", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
}
}
}
[/sourcecode]

5 comments:

  1. Á e nhớ thầy rồi, thầy dạy ở trường Aptech cần thơ đúng không thầy???

    ReplyDelete
  2. Giờ thầy dạy bên gì rồi thầy??? e lên kiếm mấy cái MessageBox và mà mấy cái nút showdialog mà gặp thầy hehehe. thầy có viết bên c# nữa hả thầy. e nhớ thầy dạy java mà.

    ReplyDelete
  3. chuyên môn chính của tôi là .Net (chủ yếu là winform và ASP.NET) SqlServer, mạng máy tính. Hiện tại thì Java, Android và PHP.
    Nói chung lâu năm rồi nên cũng phải rèn nhiều thứ.

    ReplyDelete
  4. Dạ, chứ e thấy em luyện Winform không cũng đuối rồi.hiiii

    ReplyDelete

Translate