Click here to Skip to main content
15,891,889 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have made a program that saves the coordinates from a form in a Access table.
In the Event Mouse_up I send the coordinates to the table.
But I would like to get a messagebox or something else where it askes the user what the coordinates tag would be called. This result should be saved in the same record in access table.
---------------------------------------------
Oki. I will try to explain better.
When mouse down it will start getting coordinates.
when mouse_move it starts calculate width and hight.
When mouse_up it saves this 4 fields to a record in an access database.
This works fine and I get the 4 coordinates to access.

In Access i have 5 fields. IDTAG, LEFT, BOTTOM, WIDTH, HIGHT.

Here comes the problem.
I would like to get a question from the system what i would like to call this 4 coordinates, the answer should be saved in IDTAG.

Ex. If I have a map over the world.
I would like to make a rectangel over a part. When I do mouse Up i would like to save this with the name off the region. (Europe etc.)
Posted
Updated 18-Jan-15 8:57am
v3
Comments
Sergey Alexandrovich Kryukov 17-Jan-15 19:16pm    
What have you tried so far?
—SA
BillWoodruff 17-Jan-15 19:57pm    
Are you familiar with creating UserControls ?

Can we assume that the writing-to-the-database of the extra text field associated with the co-ordinates is no problem for you ?
ZurdoDev 17-Jan-15 21:16pm    
Where are you stuck?

Here's a very simple way to do this; it assumes:

1. You want to capture the co-ordinates of the MouseDown only when the user clicks on the Form with the Control-Alt keys held down.

2. When a valid click is received, then you want a TextBox to pop-up located where the user clicked on the Form, and this TextBox will be closed when the user hits the Escape Key.

At design-time: assume your main form is named 'form1

1. put a Textbox on the Form, size and style it in the way you want, set its 'Visible property to 'false. Name it 'tbCoordinates

2. add this code: link-up the Form's MouseDown EventHandler, and the TextBox's KeyDown and Leave EventHandlers to the code here:
C#
private Point mouseDownPoint;

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    // if visible, this is an error: hide the TextBox
    if (tbCoordinates.Visible)
    {
        tbCoordinates.Hide();
        this.Cursor = Cursors.Default;
        return;
    }

    // test for Control/Alt down
    // note: 'HasFlag requires .NET 4.0
    if (!(ModifierKeys.HasFlag(Keys.Control) && ModifierKeys.HasFlag(Keys.Alt))) return;

    mouseDownPoint = e.Location;

    // show the TextBox, make it capture input
    tbCoordinates.Location = mouseDownPoint;
    tbCoordinates.Show();

    tbCoordinates.Focus();
    tbCoordinates.Capture = true;
    this.Cursor = Cursors.IBeam;
}

// exit on either Escape key, or Return/Enter key
private void tbCoordinates_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.Return)
    {
        tbCoordinates.Hide();
        this.Cursor = Cursors.Default;

        // validate the text ?
        // update your database with the
        // Point and text in tbCoordinates here
        // updateDataBase(mouseDownPoint, tbCoordinates.Text);
    }
}

// hide the control if it loses focus
private void tbCoordinates_Leave(object sender, EventArgs e)
{
    if (tbCoordinates.Visible)
    {
        tbCoordinates.Hide();
        this.Cursor = Cursors.Default;
        return;
    }
}
Notes:

1. You could, of course, display the TextBox for entering the name of the co-ordinate Points in a fixed position, or in a ToolStripMenu, or anywhere else you wished.

2. You could, if you wanted something "fancier," create a UserControl that displayed the co-ordinates clicked and any other data you wanted to; you could show it, and hide it, using the same techniques here, but you would have to manage transmitting the edited name to the Form for use with your database.
 
Share this answer
 
v3
For the message box that allows user's input and returns the input to the calling form, you have to custom make one with a form, e.g.
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = CustomInputControl.ShowDialog("Enter feedback", "Feedback");
    }
}

public static class CustomInputControl
{
    public static string ShowDialog(string label, string title)
    {
        Form inputBox = new Form();
        inputBox.Width = 300;
        inputBox.Height = 200;
        inputBox.Text = title;

        Label lbl = new Label() { Left = 40, Top = 40, Text = label };

        TextBox txtInput = new TextBox() { Left = 40, Top = 70, Width = 200 }; ;

        Button btnConfirm = new Button() { Text = "Ok", Left = 40, Top = 100, Width = 100  };
        btnConfirm.Click += (sender, e) => { inputBox.Close(); };

        inputBox.Controls.Add(lbl);
        inputBox.Controls.Add(txtInput);
        inputBox.Controls.Add(btnConfirm);

        inputBox.ShowDialog();
        return txtInput.Text;
    }
}

With this, you will be able to capture the user's input from the custom dialog box. Next, insert this input into the Access database using parameterized query, refer: Inserting data into access using c#[^]
 
Share this answer
 
Comments
BillWoodruff 18-Jan-15 3:16am    
+4 While I think the code here is a little more elaborate than really needed, it works !
Peter Leow 18-Jan-15 6:30am    
Noted. Thank you, BillWoodruff.
I got a hint to check out the code below. And it was exactly what i needed.

using Microsoft.VisualBasic;
string input = Interaction.InputBox("Prompt","Title","Default",0,0);

Then I inserted the input together with the coordinates.

Thank you all for the help.
 
Share this answer
 

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