开发者

C# Messagebox With ComboBox

开发者 https://www.devze.com 2022-12-24 18:16 出处:网络
How can I produce a messagebox in a C# Win Forms application that displays a combob开发者_C百科ox with a series of values to select as well as the usual \"Ok\" button?

How can I produce a messagebox in a C# Win Forms application that displays a combob开发者_C百科ox with a series of values to select as well as the usual "Ok" button?

I would like to be able to trigger this on calling the MessageBox.Show() method. I am assuming some sort of override will be necessary, but I haven't seen any pre-existing examples for this.


Use a custom Form instead with .ShowDialog()


You can not. The Windows MessageBox has limited functionality. You can expose a similar looking window as Dialog, but if you use the MessageBox, you are limited to the functionality a MessageBox is having.


Recently I needed to do this for a very small question, and instead of making a class, I generated a simple form with my combo-box and an "OK" button. Here is my function that generates a form, populates it, and gets the results. This is messy, but it works well for me.

/// <summary>
/// Generate a tiny form that prompts the user for the language to use.
/// </summary>
private void prompt_for_language()
{            
    QuestionForm.Text = "Language";
    Label lbLanguageChoice = new Label();
    lbLanguageChoice.Text = "Choose a Language";
    lbLanguageChoice.Location = new Point(1, 1);
    lbLanguageChoice.Size = new Size(200, lbLanguageChoice.Size.Height);

    ComboBox LanguageChoices = new ComboBox();

    LanguageChoices.Location = new Point(1, lbLanguageChoice.Location.Y + lbLanguageChoice.Height + 5);
    List<string> language_list = LanguageList();
    language_list.Sort();
    for (int loop = 0; loop < language_list.Count; loop++)
        LanguageChoices.Items.Add(language_list[loop]);
    int def = language_list.IndexOf(CurrentLanguage);
    if (def < 0) def = language_list.IndexOf(DefaultLanguage);
    if (def < 0) def = 0;
    if (language_list.Count < 1) return; //we cannot prompt when there are no languages defined
    if (def >= 0) LanguageChoices.SelectedIndex = def;

    Button Done = new Button();
    Done.Click += btnClose_Click;
    Done.Text = "Done";
    Done.Location = new Point(1, LanguageChoices.Location.Y + LanguageChoices.Height + 5); ;
    QuestionForm.Controls.Add(LanguageChoices);
    QuestionForm.Controls.Add(Done);
    QuestionForm.Controls.Add(lbLanguageChoice);
    QuestionForm.FormBorderStyle = FormBorderStyle.FixedDialog;
    QuestionForm.AutoSize = true;
    QuestionForm.Height = Done.Location.Y + Done.Height + 5; //This is too small for the form, it autosizes to "big enough"
    QuestionForm.Width = LanguageChoices.Location.X + LanguageChoices.Width + 5;
    QuestionForm.ShowDialog();
    if (LanguageChoices.SelectedIndex >= 0)
    {
        SetLanguage(LanguageChoices.SelectedItem.ToString());
    }
}

/// <summary>
/// Used by prompt_for_language -> done button. 
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClose_Click(object sender, EventArgs e)
{
    if(QuestionForm != null)
        QuestionForm.Close();
}


If you want more than just OK(yes no cancel etc) try this link: http://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxbuttons.aspx

here is an example on how to use it: http://msdn.microsoft.com/en-us/library/0x49kd7z.aspx

However if you want your own you will have to write it from the start. Create a new form and add constructors that take the parameters you need.

When your done just use

YourDialog dialog = new YourDialog("Button 1", "Button 2");
dialog.ShowDialog();


You'll need to create you're own form, here is a tutorial on how to do it, it's in VB.NET but it'll be simple enough to change to C#.


If a message box is not enough, you may want to use a Task Dialog. If you must support Windows XP, you can't use the native API for that, but there are plenty of .NET implementations for both Windows Forms and WPF, and it's also quite easy to implement by yourself. The good thing is that users today are used to task dialogs, rather than custom message boxes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号