learning monodevelop and am having trouble getting a messgae box to appear

断了今生、忘了曾经 提交于 2019-12-11 02:12:12

问题


I am working in monodevelop and learning c#.... I am trying to get a message box to appear but I can't get it to function correctly ...

Here is my code:

using System;  
using Gtk;  
using GtkSharp;  

public partial class MainWindow : Gtk.Window  
{
    public MainWindow () : base(Gtk.WindowType.Toplevel)  
    {  
        Build ();  
    }  

    protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }
    protected virtual void OnButton11ResizeChecked (object sender, System.EventArgs e)
    {
        System.Windows.Forms.MessageBox.Show("Hello World Again!!");
    }

}

What am i missing?


回答1:


You cannot mix the GTK# and System.Windows.Forms UI toolkits. You need to use a GTK dialog, something like this:

void ShowMessage (Window parent, string title, string message)
{
    Dialog dialog = null;
    try {
        dialog = new Dialog (title, parent,
            DialogFlags.DestroyWithParent | DialogFlags.Modal,
            ResponseType.Ok);
        dialog.VBox.Add (new Label (message));
        dialog.ShowAll ();

        dialog.Run ();
    } finally {
        if (dialog != null)
            dialog.Destroy ();
    }
}

See also this question.




回答2:


You are referencing GTK which is the bundled graphical toolkit in mono but are trying to use Windows.Forms which, although included in mono too, is a different toolkit:

System.Windows.Forms: This is the toolkit used in windows, the implementation on mono "emulates" how this controls are drawn and behave under platforms that mono runs on.

Gtk: This is a toolkit used in many OpenSource applications (Firefox, Pidgin, etc) and GTKSharp is simply the implementation of this same library but exposed to the .Net languages available on mono although you could use it directly with Visual Studio or a Microsoft compiler too.

So summarizing, as Mike said, you cannot use them both, you have to choose either one. If you are just learning .Net I would greatly advice to learn GTK instead of Windows Forms. Windows forms is kind of poor and basic toolkit, and soon you'll find that you'll need to learn a new API from a third party to do stuff that windows forms cant do (DevExpress, Infragistics) and Gtk can be easily extended and adjusted to your needs.



来源:https://stackoverflow.com/questions/4403756/learning-monodevelop-and-am-having-trouble-getting-a-messgae-box-to-appear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!