如何在单击文本框后在文本框下方显示表单?

时间:2013-03-03 05:35:34

标签: c# winforms

目前Form1textBox1Form1StartPosition = CenterScreentextBox1textBox1_MouseClick

textBox1_MouseClick的代码

private void textBox1_MouseClick(object sender, MouseEventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.ShowDialog();
    }

点击Form2时,StartPosition = CenterScreentextBox1 Form2将涵盖textBox1

我想要发生的事情是textBox1显示Form2时不会显示它,它应该显示在textBox1下,就像工具提示一样。我怎样才能做到这一点?

更新代码:

private void textBox1_MouseClick(object sender, MouseEventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.StartPosition = FormStartPosition.Manual;
        frm2.Location = new Point(this.Location.X + textBox1.Location.X, this.Location.Y + textBox1.Location.Y);
        frm2.ShowDialog();

    }

    private void textBox2_MouseClick(object sender, MouseEventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.StartPosition = FormStartPosition.Manual;
        frm2.Location = new Point(this.Location.X + textBox2.Location.X, this.Location.Y + textBox2.Location.Y);
        frm2.ShowDialog();
    }

没有文字框点击:

enter image description here

TEXTBOX1点击:

enter image description here

TEXTBOX2点击:

enter image description here

3 个答案:

答案 0 :(得分:0)

您最好使用UserControl而不是表单来完成。只需将UserControl的位置设置为(textbox1.Left,textbox1.Top + textbox1.Height)。

您应该在项目中添加自定义TextBox类,继承自标准TextBox并连接其Enter / Leave事件,在其中显示/隐藏您的键盘控件并连接其“keypress”事件以修改自定义文本框的Text。这将允许您根据需要创建任意数量的文本框实例。您甚至可以使键盘控件成为自定义TextBox的静态成员,以节省一些资源。

答案 1 :(得分:0)

您也可以使用以下内容:

private Point GetPosition()
{
    return new Point(this.Location.X + this.textBox1.Location.X, this.Location.Y + this.textBox1.Location.Y);
}

private void button1_Click(object sender, EventArgs e)
{
    Form2 fm = new Form2();
    fm.Location = this.GetPosition();
    fm.ShowDialog();
}

这还不准确。您必须向该位置添加Form Borderwith。对于Form2,设置StartPosition = Manual

答案 2 :(得分:0)

我之前发布的是长期的通用解决方案。要快速解决手头的问题,您应该这样做:

frmKeyboard.Location = this.PointToScreen(new Point(txtYourTextBox.Left, txtYourTextBox.Top + txtYourTextBox.Height));