如何将两个按钮保存到c#Windows窗体应用程序中创建的消息框 [英] How to Keep two Buttons to a created Message Box in c# Windows Forms Application

查看:79
本文介绍了如何将两个按钮保存到c#Windows窗体应用程序中创建的消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我创建了一个消息框,其中显示了一个位置弹出窗口。表单名称是mymessagebox。它以主要形式被调用。首先显示弹出窗口时,点击消息框上的OK按钮后,MessageBox将关闭并在Web浏览器中打开Goog​​le Map。



现在我添加了另一个名为关闭的按钮。并将那个ok buton重命名为Map。现在当我点击Map时,应该在浏览器中打开Map。当我点击关闭它应该关闭该循环并进入下一个循环。



我的源代码是: -



MyMessageBox: -



Hi all,

I have created a message box, Which shows a Popup of a Location. The form name is "mymessagebox". and it was called in the main form. At first when a popup is shown, after clicking on OK button on the messagebox the MessageBox gets closed and Opens a Google Map in the web browser.

Now i have added another button named "close". and rename that ok buton to "Map". Now when i click on Map the Map should be opened in the browser. And when i click on close it should close that loop and go to the next loop.

My source code is:-

MyMessageBox:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace LocationFinder
{
    public partial class MyMessageBox : Form
    {

        static MyMessageBox newMessageBox;
        static string Button_id;
        public MyMessageBox()
        {
            InitializeComponent();
        }

        public static string ShowBox(string txtMessage)
        {
            newMessageBox = new MyMessageBox();
            newMessageBox.lblMessage.Text = txtMessage;
            newMessageBox.ShowDialog();
            return Button_id;
        }

        public static string ShowBox(string txtMessage, string txtTitle)
        {
            newMessageBox = new MyMessageBox();
            newMessageBox.lblTitle.Text = txtTitle;
            newMessageBox.lblMessage.Text = txtMessage;
            newMessageBox.ShowDialog();
            return Button_id;
        } 

        private void MyMessageBox_Load(object sender, EventArgs e)
        {         
        }

        private void MyMessageBox_Paint(object sender, PaintEventArgs e)
        {
            Graphics mGraphics = e.Graphics;
            Pen pen1 = new Pen(Color.FromArgb(96, 155, 173), 1);
            
            Rectangle Area1 = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            LinearGradientBrush LGB = new LinearGradientBrush(Area1, Color.FromArgb(192, 0, 0), Color.FromArgb(245, 251, 251), LinearGradientMode.Vertical);
            mGraphics.FillRectangle(LGB, Area1);
            mGraphics.DrawRectangle(pen1, Area1);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button_id = "1";
            newMessageBox.Dispose();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Button_id = "2";
            newMessageBox.Dispose();
        }
        }
        }





主表格: -





Main Form:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LocationFinder
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
        public void MainForm_Load(object sender, EventArgs e)
        {
                string old_value = "";
                while (true)
                {
                    String connectionString = null;
                    SqlConnection conn;
                    SqlCommand cmd;
                    String sql = null;
                    SqlDataReader reader;

                    connectionString = "server=(local);database=modelDb;user id=sa;pwd=Esi123456";
                    sql = "DECLARE @var varchar(1000) = (SELECT TOP 1 Text FROM Alarms WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC) DECLARE @start_position int, @end_position int SELECT @start_position = PATINDEX('% at%', @var) SELECT @end_position = PATINDEX('%kilometers%', @var) DECLARE @VALUE VARCHAR(10) = (Select SUBSTRING(@var, @end_position-9,8)) Select Top 1 @VALUE,RouteTable.Latitude,Routetable.Longitude,Alarms.ApplicationTime,RouteTable.StationName,RouteTable.SectionName FROM Alarms INNER JOIN Routetable ON Routetable.Location BETWEEN FLOOR(@VALUE) AND CEILING(@VALUE) WHERE AlarmDefinitionId=139 ORDER BY EventTime DESC";

                    conn = new SqlConnection(connectionString);
                    try
                    {
                        conn.Open();
                        cmd = new SqlCommand(sql, conn);
                        reader = cmd.ExecuteReader();
                        while (reader.Read())
                        {

                            if (old_value.ToString() != reader.GetValue(0).ToString())
                            {
                                MyMessageBox.ShowBox("Leak Location:-" + "            " + reader.GetValue(0) + Environment.NewLine + "Latitude:-" + "                        " + reader.GetValue(1) + Environment.NewLine + "Longitude:-" + "                      " + reader.GetValue(2) + Environment.NewLine + "Leak Occured Time:-" + "       " + reader.GetValue(3) + Environment.NewLine + "Station Name:-" + "                " + reader.GetValue(4) + Environment.NewLine + "Section Name:-" + "                " + reader.GetValue(5));
                                string lati = reader.GetValue(1).ToString();
                                string longi = reader.GetValue(2).ToString();
                                string url1 = "https://www.google.co.in/maps/place/";
                                string url = url1 + lati + "," + longi;
                                System.Diagnostics.Process.Start(url);
                            }
                            else
                            {

                            }
                            old_value = reader.GetValue(0).ToString();
                       }
                        reader.Close();
                        cmd.Dispose();
                        conn.Close();
                    }
                    catch (Exception ex)
                    {
                      MyMessageBox.ShowBox(ex.ToString());
                    }
                    System.Threading.Thread.Sleep(5 * 1000);
                }
            }
 
     private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About_LocationFinder formhelp = new About_LeakLocationFinder();
            formhelp.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
         }
    }
}

推荐答案

使用Form的DialogResult属性来确定是否用户点击地图或关闭。在按钮单击事件处理程序中,将DialogResult的值设置为DialogResult.Ok或DialogResult.Cancel。



Use the DialogResult property of the Form to determine if the user clicked on "Map" or "Close". In the button click event handlers, set the value of DialogResult to either DialogResult.Ok or DialogResult.Cancel.

private void button1_Click(object sender, EventArgs e)
{
  this.DialogResult = DialogResult.Ok;
}

private void button2_Click(object sender, EventArgs e)
{
  this.DialogResult = DialogResult.Cancel;
}

public static DialogResult ShowBox(string txtMessage)
{
  using(newMessageBox = new MyMessageBox())
  {
    newMessageBox.lblMessage.Text = txtMessage;
    return newMessageBox.ShowDialog();
  }
}
 
public static DialogResult ShowBox(string txtMessage, string txtTitle)
{
  using(newMessageBox = new MyMessageBox())
  {
    newMessageBox.lblTitle.Text = txtTitle;
    newMessageBox.lblMessage.Text = txtMessage;
    return newMessageBox.ShowDialog();
  }
}





在主表单中加载方法写下这样的内容。





In your main form load method write something like this.

if(MyMessageBox.ShowBox(...) == DialogResult.Ok)
{
 // Open Map
}
else
{
 // Close window
}


private Enum MessageBoxButtons  {
Undefined = 0
Map = 1
Close = 2
}




您调用MyMessageBox的行中的
.ShowBox(在循环外定义clickedButtonId):



in the line you call MyMessageBox.ShowBox (define clickedButtonId outside of the loop):

clickedButtonId = MyMessageBox.ShowBox("your message");
if (clickedButtonId == MessageBoxButtons.Close)
{ // do something: skip to next iteration
continue;
}
else if (clickedbuttonId == MessageBoxButtons.Map) {
// do something else: show map
string lati = reader.GetValue(1).ToString();
                                string longi = reader.GetValue(2).ToString();
                                string url1 = "https://www.google.co.in/maps/place/";
                                string url = url1 + lati + "," + longi;
                                System.Diagnostics.Process.Start(url);





}



}


这篇关于如何将两个按钮保存到c#Windows窗体应用程序中创建的消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆