简体   繁体   中英

How to dynamically position a listbox in relation to textbox in winforms

I'm currently programming a grid with 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. 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
Use the sender to make it work for all textboxes.

TextBox TextB = (TextBox)sender;"

Then use the textbox location X and Y to set the list box.
You need to add to the Y the height of the textbox and the space you want it to have under you're textbox.

"listBox1.Location = new Point(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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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