简体   繁体   English

如何在Winforms中相对于文本框动态定位列表框

[英]How to dynamically position a listbox in relation to textbox in winforms

I'm currently programming a grid with winforms. 我目前正在使用Winforms编写网格。 I have multiple textboxes which are making up the cells each. 我有多个组成每个单元格的文本框。 When I click on the cells I want to Display a listbox (it is a single predefined listbox that I added via the designer before, thus the same listbox for each of the cells). 当我单击单元格时,我想显示一个列表框(这是我之前通过设计器添加的单个预定义列表框,因此每个单元格都具有相同的列表框)。

Now my question is how can I Position the listboxes under the textboxes? 现在我的问题是如何将列表框放在文本框下面?

The Events I Need to use I know already (as I'm using a Framework there I needed to use the Events there and already know the appropriate one where I can make the listbox visible and invisible). 我需要使用的事件我已经知道了(当我在那儿使用框架时,我需要在那儿使用事件,并且已经知道合适的事件可以在其中使列表框可见和不可见)。 I have handlers for the current TextBox in the Event. 我在事件中具有当前TextBox的处理程序。 The Problem I have is that I'm not sure how I can use These informations to Position the Listbox itself. 我遇到的问题是我不确定如何使用这些信息来定位列表框本身。

Thus which commands do I Need to use to Position the listbox? 因此,我需要使用哪些命令来定位列表框?

Add all the textboxes to the enter and leave event 将所有文本框添加到enter和离开事件
Use the sender to make it work for all textboxes. 使用发件人使其适用于所有文本框。

TextBox TextB = (TextBox)sender;" TextBox TextB =(TextBox)发送方;“

Then use the textbox location X and Y to set the list box. 然后使用文本框位置X和Y设置列表框。
You need to add to the Y the height of the textbox and the space you want it to have under you're textbox. 您需要将文本框的高度和希望其在文本框下方的空间添加到Y。

"listBox1.Location = new Point(TextB.Location.X, TextB.Location.Y + TextB.Height + 5);" “ listBox1.Location =新点(TextB.Location.X,TextB.Location.Y + TextB.Height + 5);”

Use the code below and it works 使用下面的代码,它可以工作

    private void textBox1_Enter(object sender, EventArgs e)
    {
        TextBox TextB = (TextBox)sender;

        listBox1.Location = new Point(TextB.Location.X, TextB.Location.Y + TextB.Height + 5);
        listBox1.Visible = true;
    }

    private void textBox1_Leave(object sender, EventArgs e)
    {
        listBox1.Visible = false;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM