Search code examples
c#visual-studiowinformsbunifu

Sending parameters to a control user from a form c#,visual Studio


I am trying to use a function of my control user from my main form, my code is the next:

chatbox bbl = new bunifchat.chatbox();
            bbl.sendmessage("a");

chatbox is my control user and i am trying to user the function sendmessage sending a string but nothing happens. This is the function in my control user:

public void sendmessage(String message)
        {
            bubble bbl = new bunifchat.bubble(message);//this is another control user 
            bbl.Location = bubble1.Location;
            bbl.Size = bubble1.Size;
            bbl.Anchor = bubble1.Anchor;
            bbl.Top = bbl_old.Bottom + 10;
            panel2.Controls.Add(bbl);


            panel2.VerticalScroll.Value = panel2.VerticalScroll.Maximum;

            bbl_old = bbl;  
        }

Solution

  • First of all you need to make sure you are using the right control.

    chatbox bbl = new bunifchat.chatbox();
    bbl.sendmessage("a");
    

    This will create a new chatbox object and call a method on it. But unless this actual object is added to the Controls of your main form it is not part of the UI and nothing will happen. You probably want to add the Chatbox control to your form in the designer, give it a name, and just call the method on that object:

    myChatbox.sendmessage("a");
    

    Second problem is that your send method tries to emulate a list with by docking individual controls. This will probably cause problems. A better solution would be to use a listView.

    Third problem is that sendmessage does not invalidate the control. This is sometimes needed to make sure windows redraws the control with the latest updates. It is possible this is done implicitly by Control.Add, but whenever there is something not showing updates I usually add a invalidate call and check if that solves the problem (and remove it if it does not solve the problem).