custom dialog with a text field in winmobile

2019-01-20 12:31发布

问题:

I'm looking to have a simple custom dialog box, like a message box, that has a label and a TextBox. If there's a simple way to do this, sorry! I'm really not well versed in the dialog stuff.

Thanks for any help, guys!

回答1:

Here is how to make a small custom dialog box in Windows Mobile that looks like this:

alt text http://www.freeimagehosting.net/uploads/b8fb5421d6.jpg

Add a form to your project, and set its FormBorderStyle property to None. This allows the form to be resized and positioned, but also means it has no border or titlebar, and there's no way for the user to move it.

So you have to fake all three. The easiest way to fake the border and the title bar is to make the BackColor of your form SystemColors.WindowFrame, then put a label (where it says "Dialog" in the picture) with BackColor = SystemColors.Highlight and ForeColor = SystemColor.HighlightText (and bold the font), then put a panel with BackColor = SystemColors.Window where you see white in the picture. You have to tweak the positions and sizes of the label and the panel so you have a 1-pixel border (which is just the BackColor of your form showing through).

To enable the form to be dragged around by its fake titlebar, add this code to the form (and of course you have to wire up the events, too):

private bool _Moving = false;
private Point _Offset;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
    _Moving = true;
    _Offset = new Point(e.X, e.Y);
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (_Moving)
    {
        Point newlocation = this.Location;
        newlocation.X += e.X - _Offset.X;
        newlocation.Y += e.Y - _Offset.Y;
        this.Location = newlocation;
    }
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
    if (_Moving)
    {
        _Moving = false;
    }
}

One other problem is that because there isn't a real titlebar, there's no way for the user to close the form. You have to add an OK (or Close) button, and put this in the button's Click event:

this.DialogResult = DialogResult.OK;

Normally you would use the mouse event on the title bar to facilitate dragging, but the label control doesn't have any mouse events. With this code you could actually grab anywhere on the form and drag it, except the panel blocks this and makes the title bar the only place to grab and drag.

My other answer has more details on getting information back from custom dialogs.

Update: actually, there's only no obvious way to close a borderless form without adding your own OK button. As long as you don't set your form's ControlBox property to False, the OK or X button in the upper right corner of the Today screen will close your dialog, even if it doesn't look like it will since it's not actually on the form.



回答2:

If you want a super-simple but[t] ugly solution, you can include a reference in your project to Microsoft.VisualBasic, which lets you use the VB function InputBox like this:

string s = Microsoft.VisualBasic.Interaction.InputBox("prompt text",
    "title text", "default value", 0, 0);

The dialog takes up the entire screen, but is simple to use. But is incredibly ugly, as I mentioned.



回答3:

I'm assuming you basically want a custom dialog box that returns a string entered by the user. One way is to add a reference to Microsoft.VisualBasic to your project, which gives you access to the InputBox method, which is basically a message box with a text box on it. But that's no fun and I'm not sure it would work on a smartphone anyway.

To roll your own, you just add a form (named CustomDialog) to your project and drag a textbox (textBox1), a label (label1), and a button (labeled "OK") onto it.

To set the label text, add a parameter to the form's constructor like this:

public CustomDialog(string textCaption)
{
    label1.Text = textCaption;
}

To expose the entered text to the caller, add this code to the form:

public override string Text
{
    get
    {
        return textBox1.Text;
    }
}

In the OK button's click event, put this code:

this.DialogResult = DialogResult.OK; // this will close the form, too

To use this dialog from your main form, you create an instance of this form, show it, check to see that the OK button was clicked, and then read its Text property (which returns what the user entered) like so:

using (CustomDialog dialog = new CustomDialog("What is your name"))
{
    if (dialog.ShowDialog(this) == DialogResult.OK)
    {
        string enteredText = dialog.Text;
    }
}

You can get fancier, but those are the basics.