Results 1 to 5 of 5

Thread: How to add a form control on top of a main form control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    How to add a form control on top of a main form control

    I have two forms. Form1 with richtextbox and form2 with listbox.

    I'd like to put listbox on top of richtextbox, docking it. I want to do exactly like this:

    Code:
    form2.Controls.Remove(ListBox1)
    form2.hide
    form1.richtextbox1.controls.add(form2.listbox1)
    form1.richtextbox1.hide 'I can't hide, also listbox will hide.. So let's do form1.controls.add(form2.listbox) instead.
    form2.listbox1.location= richtextbox1.location or
    form2.listbox1.bounds=richtextbox1.bounds
    Everything works until setting form2.listbox1.location as If richtextbox1 is hidden also listbox will be. Also, setting location and/or bounds of listbox the same as richtextbox is not completely covering the richtextbox. I also tried using the same size for both.

    I have tried also with:
    Code:
    Dim LB As ListBox = Form2.ListBox1
    Form1.RichTextBox1.Controls.Add(LB)
    LB.Location = New Point(0, 0)
    LB.Size = Form1.RichTextBox1.Size
    but if the text in the richtextbox is longer than the LB, then you'll still be able to scroll the RTB down to see that text.
    Here a demonstration gif
    So it is not really doing its job properly.. How can I make it ?
    Thanks a lot!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: How to add a form control on top of a main form control

    Create your own control that does what you want.

    I applaud your approach, but you can only take it so far. I don't see a clear way to make two controls do what you want, and you've largely identified why not. However, to make that happen, you may just need to build your own control. The control might include both the RTB and the listbox, or you might even build the listbox as a panel with a scrollbar. A control of your own creation can show/hide as it needs to, rather than with parents hiding children, which is what you're going to get if you make the listbox a child of the RTB.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to add a form control on top of a main form control

    What are you actually trying to achieve here? You've described how you're trying to achieve it but not what you're actually trying to achieve. If you want a RichTextBox and a ListBox to occupy the same area then why don't you just add them to the same form with the same Size and Location? You can show and hide or change the z-order as required so the user can only see one at a time. You could also create a user control containing both and then treat them as a single unit.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Re: How to add a form control on top of a main form control

    Hi, thanks. So, I am trying to do so, if you check the last code i m trying to steal listbox to be in form1. Then, i am setting location which is 0,0 if i want to cover the rtb and i m setting the same size. As you can see in the gif i posted, listbox is not properly on top of rtb. Guys i know this is not so professional but i want to keep this way without using usercontrols which i will need to handle events from another form and it is starting to be a mess to me.

    I want to achieve the replacement of rtb with listbox whenever the user wants so. And whenever He decides, rtb will be visible again in form1 and listbox will be aain in form2. Hope i was clear. Thanks

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2013
    Posts
    108

    Re: How to add a form control on top of a main form control

    @Shaggy, I would like not to use usercontrols as I dont want to handle events from a different form, so I m trying to have ideas. The only idea stable so far is:
    Dont add listbox to rtb as if i hide rtb then listbox will be hidden, so in this case, add rhe control to form1. Hide rtb e move listbox in the same position of rtb.. the idea seems very easy but I tried already and it doesnt work.. thanks

    EDIT: I Solve it.
    The richtextbox is inside of a groupbox, so the right code is:
    Code:
     Dim LB As ListBox = Form2.ListBox1
        GroupBox3.Controls.Add(LB)
        LB.Bounds = RichTextBox1.Bounds
        RichTextBox1.Hide()
    Last edited by matty95srk; Oct 30th, 2020 at 10:11 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width