Home > C# > ภาษา C# การใช้ MessageBox

ภาษา C# การใช้ MessageBox

February 20, 2010 No Comments    

ในการใช้ MessageBox นั้นเพื่อแสดงข้อความแจ้งไปยังผู้ใช้โปรแกรมซึ่งในภาษา C# นั้นก็มีการใช้ MessageBox เพื่อแสดงมีรายละเอียดดังนี้
MessageBox.Show(ข้อความที่จะแสดงในกล่องข้อความ, ข้อความในแถบบอกกล่องข้อความ, รูปแบบปุ่ม, iconที่จะแสดง, ปุ่ม default);
รายละเอียดของรูปแบบปุ่มในภาษา C# มีดังนี้
c# programming, ภาษา c#, ภาษา ซีชาร์ป, สอน c#, สอน เขียน c#, โปรแกรมภาษา C#, โปรแกรม เชิงวัตถุ

รูปแบบ Icon ในภาษา C#

c# programming, ภาษา c#, ภาษา ซีชาร์ป, สอน c#, สอน เขียน c#, โปรแกรมภาษา C#, โปรแกรม เชิงวัตถุ

ส่วนปุ่ม default จะเริ่มนับจากทางซ้าย
ต่อไปก็เป็นตัวอย่างภาษา C# ในการใช้ MessageBox
ขั้นแรกก็ทำการสร้างโปรเจ็คใหม่เลือก WindowFormsApplication แล้วทำการตั้งชื่อว่า MyMessageBox

c# programming, new project, ภาษา c#, ภาษา ซีชาร์ป, สอน c#, สอน เขียน c#, โปรแกรมภาษา C#, โปรแกรม เชิงวัตถุ

ต่อจากนั้น Doble click ที่ forms เพื่อเพิ่มโค้ดในส่วนที่ขาดลงไป


private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show("คุณต้องการดู MessageBox ต่อไปหรือไม่","My Application", MessageBoxButtons.YesNo,
MessageBoxIcon.Information);

MessageBox.Show("แบบที่ 2 OK กับ Cancel", "My Application",
MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);

MessageBox.Show("แบบมี 3 ปุ่ม", "My Application",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

MessageBox.Show("แบบปุ่ม default อยู่ปุ่มที่ 3", "My Application",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button3);

}

ผลการรันโปรแกรม

result messagebox,c# programming, ภาษา c#, ภาษา ซีชาร์ป, สอน c#, สอน เขียน c#, โปรแกรมภาษา C#, โปรแกรม เชิงวัตถุ

resutl messagebox 2, c# programming, ภาษา c#, ภาษา ซีชาร์ป, สอน c#, สอน เขียน c#, โปรแกรมภาษา C#, โปรแกรม เชิงวัตถุ

resutl messagebox, c# programming, ภาษา c#, ภาษา ซีชาร์ป, สอน c#, สอน เขียน c#, โปรแกรมภาษา C#, โปรแกรม เชิงวัตถุ

result messagebox, c# programming, ภาษา c#, ภาษา ซีชาร์ป, สอน c#, สอน เขียน c#, โปรแกรมภาษา C#, โปรแกรม เชิงวัตถุ

จากผลการรันสังเกตโค้ดว่าจะเป็นไปตามรูปแบบที่บอกไว้
MessageBox.Show(ข้อความที่จะแสดงในกล่องข้อความ, ข้อความในแถบบอกกล่องข้อความ, รูปแบบปุ่ม, iconที่จะแสดง, ปุ่ม default);

ส่วนปุ่ม default จะอยู่ปุ่มที่ 3 โดยการกำหนดปุ่ม default จะใช้ดังนี้
ปุ่มที่ 1
MessageBoxDefaultButton.Button1
ปุ่มที่ 2
MessageBoxDefaultButton.Button2
ปุ่มที่ 3
MessageBoxDefaultButton.Button3

Download Source code

Tags: , , , , , ,

Comments : ภาษา C# การใช้ MessageBox

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>