Click here to Skip to main content
15,896,269 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:-

C#
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:-

C#
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();
         }
    }
}
Posted
Updated 25-May-15 19:40pm
v2
Comments
Sinisa Hajnal 26-May-15 2:10am    
Close and dispose your objects in finally block. In case of an exception they will be left hanging.

Why is your button id string if you're identifying them with numbers? Change it to integer.

For multiple string concatenations, use StringBuilder. It is more readable, easier to maintain and check and faster.

In your button handlers in MessageBox class, you should check for null before calling dispose.

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.

C#
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.

C#
if(MyMessageBox.ShowBox(...) == DialogResult.Ok)
{
 // Open Map
}
else
{
 // Close window
}
 
Share this answer
 
C#
private Enum MessageBoxButtons  {
Undefined = 0
Map = 1
Close = 2
}


in the line you call MyMessageBox.ShowBox (define clickedButtonId outside of the loop):
C#
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);


}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900