TOC

This article has been localized into Turkish by the community.

Diyaloglar:

MessageBox Diyaloğu

WPF uygulamalarınızın kullanışlı olması için çok farklı diyaloglar sunar, ama en basiti açıkçası MessageBox diyaloğudur. Yegane amacı kullanıcıya bir mesaj göstermek ve sonra kullanıcıya mesaja çeşitli cevap verme seçenekleri sunar.

MessageBox nasıl görüneceğini ve davranacağını belirleyeceğiniz bir çok farklı şekillerde parametreler alabilen static Show() metodunu çağırarak kullanılır. Bu yazıda MessageBox.Show() satırlarıyla ve sonucun ekran görüntüleriyle tüm varyasyonları göreceğiz. Yazının sonunda komple bir örnek size tüm varyasyonları test imkanı bulacaksınız.

MessageBox'ın burada sadece gösterilecek mesajı parametre olarak alan en basit şekli var :

MessageBox.Show("Hello, world!");

Başlığı olan MessageBox

Yukarıdaki örnek çok fazla minimum - pencere üzerinde bir başlık yazısı belki daha yardımcı olacaktır. Şükür ki ikinci ve opsiyonel parametre bize başlık ekleme imkanı verir :

MessageBox.Show("Hello, world!", "My App");

Fazladan butonları olan MessageBox

Default olarak MessageBox sadece bir OK (ya da TAMAM) butonuna sahiptir, fakat eğer kullanıcıya basit bir bilgi verip soru sormak isterseniz bu değiştirilebilir. Ayrıca alt satır karakteri (\n) kullanarak nasıl çok satırlı mesaj yazdığıma dikkat ediniz :

MessageBox.Show("This MessageBox has extra options.\n\nHello, world?", "My App", MessageBoxButton.YesNoCancel);

MessageBoxButton enumerasyonunu kullanarak hangi butonların gösterileceğini kontrol edersiniz - bu örnekte Yes, No ve Cancel butonları (Evet, Hayır ve İptal) dahil edilmiş. Aşağıdaki değerler ne amaçla kullanıldığını kendi açıklıyor zaten :

  • OK
  • OKCancel
  • YesNoCancel
  • YesNo

Bir çok seçeneğiniz olduğuna göre kullanıcının hangisini tercih ettiğini de bilmek isteyeceksiniz, ve şükür ki MessageBox.Show() metodu her zaman MessageBoxResult enumerasyonundan sizin kullanabileceğiniz bir değer döner. İşte bir örnek :

MessageBoxResult result = MessageBox.Show("Would you like to greet the world with a \"Hello, world\"?", "My App", MessageBoxButton.YesNoCancel);
switch(result)
{
	case MessageBoxResult.Yes:
		MessageBox.Show("Hello to you too!", "My App");
		break;
	case MessageBoxResult.No:
		MessageBox.Show("Oh well, too bad!", "My App");
		break;
	case MessageBoxResult.Cancel:
		MessageBox.Show("Nevermind then...", "My App");
		break;
}

MessageBox.Show() metodunun dönen değerini kontrol ederek kullanıcının seçimine yukarıda görülen kod örneğinde ve resimlerdeki gibi reaksiyon gösterebilirsiniz.

Bir ikonu olan MessageBox

MessageBox dördüncü bir parametre kullanarak mesaj yazısının solunda önceden tanımlı ikonlardan birini gösterme kabiliyetine sahip :

MessageBox.Show("Hello, world!", "My App", MessageBoxButton.OK, MessageBoxImage.Information);

MessageBoxImage enumerasyonu kullanarak değişik durumlara göre ikonlar seçilebilir. İşte komple liste :

  • Asterisk
  • Error
  • Exclamation
  • Hand
  • Information
  • None
  • Question
  • Stop
  • Warning

İsimleri nasıl görünecekleri hakkında bilgi veriyor, fakat deneyerek görebilirsiniz ya da buradaki hepsinin açıklandığı hatta canlandırıldığı MSDN makalesine bakabilirsiniz : http://msdn.microsoft.com/en-us/library/system.windows.messageboximage.aspx

Default seçeneği olan MessageBox

MessageBox bir butonu default seçer ve sonra bu buton mesaj gösterildiğinde kullanıcı sadece Enter tuşuna basarsa çalışacaktır. Örneğin "Yes" ve "No" butonları olan bir MessageBox'ta "Yes" butonu default cevap olacaktır. Bu davranışı MessageBox.Show() metodu beşinci parametresi ile değiştirebilirsiniz :

MessageBox.Show("Hello, world?", "My App", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);

Ekran görüntüsüne bakarsanız "No" butonu seçili olarak görünüyor, kullanıcı Enter veya Space bastığında bu buton tıklanmış gibi olacak.

Komple örnek

Söz verdiğimiz gibi işte bu yazıdaki örneklerin tamamından oluşan bir uygulama :

<Window x:Class="WpfTutorialSamples.Dialogs.MessageBoxSample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MessageBoxSample" Height="250" Width="300">
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel.Resources>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="0,0,0,10" />
            </Style>
        </StackPanel.Resources>
        <Button Name="btnSimpleMessageBox" Click="btnSimpleMessageBox_Click">Simple MessageBox</Button>
        <Button Name="btnMessageBoxWithTitle" Click="btnMessageBoxWithTitle_Click">MessageBox with title</Button>
        <Button Name="btnMessageBoxWithButtons" Click="btnMessageBoxWithButtons_Click">MessageBox with buttons</Button>
        <Button Name="btnMessageBoxWithResponse" Click="btnMessageBoxWithResponse_Click">MessageBox with response</Button>
        <Button Name="btnMessageBoxWithIcon" Click="btnMessageBoxWithIcon_Click">MessageBox with icon</Button>
        <Button Name="btnMessageBoxWithDefaultChoice" Click="btnMessageBoxWithDefaultChoice_Click">MessageBox with default choice</Button>
    </StackPanel>
</Window>
using System;
using System.Windows;

namespace WpfTutorialSamples.Dialogs
{
	public partial class MessageBoxSample : Window
	{
		public MessageBoxSample()
		{
			InitializeComponent();
		}

		private void btnSimpleMessageBox_Click(object sender, RoutedEventArgs e)
		{
			MessageBox.Show("Hello, world!");
		}

		private void btnMessageBoxWithTitle_Click(object sender, RoutedEventArgs e)
		{
			MessageBox.Show("Hello, world!", "My App");
		}

		private void btnMessageBoxWithButtons_Click(object sender, RoutedEventArgs e)
		{
			MessageBox.Show("This MessageBox has extra options.\n\nHello, world?", "My App", MessageBoxButton.YesNoCancel);
		}

		private void btnMessageBoxWithResponse_Click(object sender, RoutedEventArgs e)
		{
			MessageBoxResult result = MessageBox.Show("Would you like to greet the world with a \"Hello, world\"?", "My App", MessageBoxButton.YesNoCancel);
			switch(result)
			{
				case MessageBoxResult.Yes:
					MessageBox.Show("Hello to you too!", "My App");
					break;
				case MessageBoxResult.No:
					MessageBox.Show("Oh well, too bad!", "My App");
					break;
				case MessageBoxResult.Cancel:
					MessageBox.Show("Nevermind then...", "My App");
					break;
			}
		}

		private void btnMessageBoxWithIcon_Click(object sender, RoutedEventArgs e)
		{
			MessageBox.Show("Hello, world!", "My App", MessageBoxButton.OK, MessageBoxImage.Information);
		}

		private void btnMessageBoxWithDefaultChoice_Click(object sender, RoutedEventArgs e)
		{
			MessageBox.Show("Hello, world?", "My App", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
		}
	}
}

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!