Home  Contents

Dialogs

نوافذ(صناديق)الحوار هى جزء مهم فى التطبيقات الرسومية، الحوار هو محادثة بين شخصين او اكثر، هنا المحادثة (المستخدم والتطبيق) وسيلة لإيصال معلومة للتطبيق. يمكن استخدامها فى اضافة بيانات ، تعديلها، او التحكم فى اعدادات البرنامج.. الخ.

يوجد نوعان (نافذة حوار معرفة مسبقا ، او واحدة اعدّت خصيصا)



FolderBrowserDialog

يستخدم هذا الصندوق الحوارى لإختيار مجلد ما

folderbrowserdialog.cs

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {

    private ToolBar toolbar;
    private ToolBarButton open;
    private StatusBar statusbar;

    public MForm() {
        Text = "FolderBrowserDialog";
 
        toolbar = new ToolBar();
        open = new ToolBarButton();

        statusbar = new StatusBar();
        statusbar.Parent = this;

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        Controls.Add(toolbar);

        CenterToScreen();
    }


    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       FolderBrowserDialog dialog = new FolderBrowserDialog();

       if (dialog.ShowDialog(this) == DialogResult.OK) {
           statusbar.Text = dialog.SelectedPath;
       }
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}

لدينا شريط ادوات وزر وعندا الضغط عليه يتم عرض FolderBrowserDialog لإختيار ملف ويتم عرض اسم ذلك الملف فى شريط الحالة



 FolderBrowserDialog dialog = new FolderBrowserDialog();

انشاء كائن من الصف FolderBrowserDialog

 if (dialog.ShowDialog(this) == DialogResult.OK) {
     statusbar.Text = dialog.SelectedPath;
 }       

الطريقة ShowDialog تعرض الصندوق على الشاشة، واذا ضغطنا على الزر OK يتم كتابة اسم المجلد -نحصل عليه بإستخدام الخاصية SelectedPath - فى شريط الحالة




FolderBrowserDialog

Figure: FolderBrowserDialog

ColorDialog

يستخدم لإختيار الوان من قبل المستخدم

colordialog.cs

 using System;
 using System.Drawing;
 using System.Windows.Forms;

class MForm : Form {

    private ToolBar toolbar;
    private ToolBarButton open;
    private Color color;

    private int rectWidth = 100;
    private int rectHeight = 100;
    private Rectangle r;

    public MForm() {
        Text = "ColorDialog";

        color = Color.Blue;
 
        toolbar = new ToolBar();
        open = new ToolBarButton();

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        LocateRect();

        SetStyle (ControlStyles.ResizeRedraw, true);
        Controls.Add(toolbar);
        Paint += new PaintEventHandler(OnPaint);

        CenterToScreen();
    }


    void OnPaint(object sender, PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      LocateRect();

      SolidBrush brush = new SolidBrush(color);

      g.FillRectangle(brush, r);
    }


    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       ColorDialog dialog = new ColorDialog();
       if (dialog.ShowDialog(this) == DialogResult.OK) {
          color = dialog.Color;
          Invalidate();
       }
    }

    void LocateRect() {
        int x = (ClientSize.Width - rectWidth) / 2;
        int y = (ClientSize.Height - rectHeight) / 2;
        r = new Rectangle(x, y, rectWidth, rectHeight);
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}

فى المثال لدينا صندوق اختيار لون ColorDialog لإختيار لون لمستطيل فى منتصف النافذة



 color = Color.Blue;

فى البداية، لون المستطيل سيكون ازرق. سنستخدم المتغير color لتحديد اللون



 ColorDialog dialog = new ColorDialog();

انشاء كائن من الصف ColorDialog

 if (dialog.ShowDialog(this) == DialogResult.OK) {
    color = dialog.Color;
    Invalidate();
 }

عرض الصندوق واذا تم الضغط على OK نحصل على اللون بإستخدام الخاصية Color ، ثم نستدعى Invalidate ليتم اعادة رسم المستطيل

The code shows the color dialog. If we click on the OK button, we get the selected color and call the Invalidate() method. The method invalidates the entire surface of the control and causes the control to be redrawn. The result is that the rectangle is drawn with a new color value.


ColorDialog

Figure: ColorDialog

FontDialog

يستخدم لإختيار خط ما

fontdialog.cs

 using System;
 using System.Drawing;
 using System.Windows.Forms;

class MForm : Form {

    private Label text;
    private ToolBar toolbar;
    private ToolBarButton open;

    public MForm() {
        Text = "FontDialog";

        text = new Label();
        text.Parent = this;
        text.Text = "Winforms tutorial";

        LocateText();
 
        toolbar = new ToolBar();
        toolbar.Parent = this;
        open = new ToolBarButton();

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        text.AutoSize = true;
        Resize += new EventHandler(OnResize);

        CenterToScreen();
    }

    void OnResize(object sender, EventArgs e){
       LocateText();
    }

    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       FontDialog dialog = new FontDialog();
       if (dialog.ShowDialog(this) == DialogResult.OK) {
          text.Font = dialog.Font;
          LocateText();
       }
    }

    void LocateText() {
        text.Top = (this.ClientSize.Height - text.Height) / 2;
        text.Left = (this.ClientSize.Width - text.Width) / 2;
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}

هنا نرسم نص فى منتصف النافذة، ولتغيير الخط نستخدم fontdialog

We draw some text in the middle of the form control. We use the font dialog to change font for this text.

 FontDialog dialog = new FontDialog();

تم انشاء كائن dialog من الصف FontDialog

 if (dialog.ShowDialog(this) == DialogResult.OK) {
    text.Font = dialog.Font;
    LocateText();
 }

عند الضغط على الزر OK نقوم بتحديد نوع الخط -نحصل على الخط بإستخدام الخاصية Font- ونسندها لل Font الخاص بال Label ثم نستدعى الطريقة LocateText لتحديد النص فى منتصف النافذة تماما


FontDialog

Figure: FontDialog

OpenFileDialog

يستخدم للحصول على ملف ما

opendialog.cs

using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;

class MForm : Form {

    private ToolBar toolbar;
    private ToolBarButton open;
    private TextBox textbox;

    public MForm() {
        Text = "OpenFileDialog";

        toolbar = new ToolBar();
        open = new ToolBarButton();

        textbox = new TextBox();
        textbox.Multiline = true;
        textbox.ScrollBars = ScrollBars.Both;
        textbox.WordWrap = false;
        textbox.Parent = this;
        textbox.Dock = DockStyle.Fill;
       

        toolbar.Buttons.Add(open);
        toolbar.ButtonClick += new ToolBarButtonClickEventHandler(OnClicked);

        Controls.Add(toolbar);
        Controls.Add(textbox);

        CenterToScreen();
    }

    void OnClicked(object sender, ToolBarButtonClickEventArgs e) {
       OpenFileDialog dialog = new OpenFileDialog();
       dialog.Filter = "C# files (*.cs)|*.cs";

       if (dialog.ShowDialog(this) == DialogResult.OK) {
           StreamReader reader = new StreamReader(dialog.FileName);
           string data = reader.ReadToEnd();
           reader.Close();
           textbox.Text = data;
       }
    }
}


class MApplication {
    public static void Main() {
        Application.Run(new MForm());
    }
}

نستخدم الصف OpenFileDialog لفتح ملفات مكتوبة بالسى شارب، ولدينا حقل ادخال لنعرض فيه محتوى الملف

 OpenFileDialog dialog = new OpenFileDialog();

ننشئ الكائن dialog من الصف OpenFileDialog

 dialog.Filter = "C# files (*.cs)|*.cs";

نحدد الخاصية Filter لتحديد نوع الملفات اللتى ممكن تصفحها والإختيار منها

 if (dialog.ShowDialog(this) == DialogResult.OK) {
     StreamReader reader = new StreamReader(dialog.FileName);
     string data = reader.ReadToEnd();
     reader.Close();
     textbox.Text = data;
 }

عند الضغط على OK نقرا محتوى الملف المختار -نحصل عليه بإستخدام الخاصية FileName- ونضع محتواه فى حقل الإدخال




OpenDialog

Figure: OpenDialog

فى هذه الجزئية تعرضنا لأهم الصناديق الحوارية


HomeContentsTop of Page