1

I have a big problem with a small matter. I'm coding a messaging app.

This is the layout I use: Main Form --> container (UserControl) --> bubble(UserControl)

I am sending these parameters to the container

this.container1.addOutMessage(message, get_min_sec(time), tickStatus);

and container creates bubbles dynamically in itself.

public void addOutMessage(string message, string time, int tick_status) {...}

According to the suggestion I got in this forum, I draw the contents of the message bubbles in the Paint event. Since the width and height information was not yet clear when creating, I could not create the vertical distance between the messages correctly.

enter image description here

That's why I created this function.

public void ContaninerConfigure()
    {
        int counter = 0;
        int keepBottom = 0;
        foreach (var item in container1.panel.Controls.OfType<Control>().OrderBy(ee => ee.TabIndex))
        {
            //SuspendDrawing(item);
            if (item != null)
            {
                if (counter != 0)
                {
                    item.Top = keepBottom + 10;
                    keepBottom = item.Bottom;
                }
                else keepBottom = item.Top + item.Height;
            }
            counter++;
        }
        counter = 0;
}

then

enter image description here

My problem starts here. When I add it to the container's resize event it works fine but when I call it in the main form after my messages are loaded it doesn't work at all. Paint event continues to run on every scroll movement. In the container's resize event, only the message bubbles that appear on the screen are aligned. It can crashes again when I scroll up. Then it is necessary to call the previous function again to align it.

I did not access Paint event in Bubble(UC) which is dynamically created in Container(UC).That's why I couldn't control with the paint event, its run or stop.

After some research I found the SuspendDrawing method. I implemented it this way but I don't want to use it. It takes 3 seconds for 25 bubbles to line up on the panel and main form stays "up to date" during compile.

There has to be a more practical way of doing this. For example, I don't know how to check the bool in the bubble(UC) from the main form.

If you have any idea about this issue, please help.

quahvaen
  • 31
  • 1
  • 3
  • 1
    Imagine a two or maybe three hours chitchat and your app keep creating and adding controls this way. Your app will crash eventually, I promise you that. I think you need to reconsider the design and find alternatives. Like HTML viewer, a single control to **draw** everything in the view, or maybe move to WPF which provides better tools for this kind of apps. Apologies for the negativity. – dr.null Aug 22 '21 at 15:12
  • This is sad news, but when I examine the structures you mentioned, I understand what you mean, you're right. I'm asking in order not to waste time because I need to finish it soon. Which side do you use your preference? Which path should I follow with the fewest possible changes to the existing logic? And on the other hand, which one should I use for the best results? – quahvaen Aug 22 '21 at 20:21
  • 1
    See [this](https://www.codeproject.com/Articles/1181555/SignalChat-WPF-SignalR-Chat-Application) as WPF example. Some more examples in YouTube. – dr.null Aug 23 '21 at 06:40
  • 2
    You can do something like this, if you need to *finish soon*: [Fake-scrolling containers with very many controls](https://stackoverflow.com/a/39810717/7444103). Use a *virtual UserControl* that exists only in the View, so you don't allocate graphics resources and handles (it's a race between the two for which will kill the app first). – Jimi Aug 23 '21 at 06:58
  • Thanks for your suggestions. I'll try the fake scroll event first. It looks like it'll save me if I can. – quahvaen Aug 24 '21 at 19:32

0 Answers0