1
Hello. I am going through the following scenario. I have a Usercontrol that has dynamic width based on the text typed in a Textbox within it. For this in the Textchanged event of this Textbox, the following algorithm is made:
private void MensurarTamanho()
{
var tamanho = TextRenderer.MeasureText(txtLyric.Text, txtLyric.Font).Width + txtLyric.Margin.Horizontal + 2;
var tamanhomin = cbChord.Width + btopc.Width + checkBox1.Width + 6;
Width = tamanho < tamanhomin ? tamanhomin : tamanho;
}
This control is lined up inside a Flowlayoutpanel (which represents a group of controls) which in turn is Efileirado in another Flowlayoutpanel (ie a Flowlayoutpanel with Flowlayoutpanels as child controls. In order for the width of the Flowlayoutpanel to be adjusted and dynamic as the controls are added I used the following code in the Controldded and Controlremoved events:
private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
{
var maior = flowLayoutPanel1.Controls.OfType<Control>().OrderBy(c => c.Height).LastOrDefault();
var largura = 6 + flowLayoutPanel1.Controls.OfType<Control>().Sum(c => c.Width + c.Margin.Horizontal);
int altura = 120;
if (maior != null)
{
altura = maior.Height + maior.Margin.Vertical;
}
Size = new Size(largura, altura);
}
This works perfectly when I add and remove controls, however, the internal controls adjust as the text is edited, as already described at the beginning. In this case I need to identify when an internal control is resized so I can apply the above resizing algorithm again. I thought of adding a delegate at the Resize event to each child control added and removing the delegate whenever a control is removed, but I look for a more elegant way to do this.
Exactly. I could use the "Controldded" and "Controlremoved" events to assign the delegates to the Resize Kids Controls events. However, I was looking for a more elegant way to do this.
– Odair José Gambert Nossa
I will explain with this image: https://i.imgur.com/Icqlv1q.png E as the explanation is long and the comment does not give me many characters, here is the explanation: https://pastebin.com/uniqPetB
– Odair José Gambert Nossa
https://i.imgur.com/7kP5ZNM.png This is the method triggered by "Controldded" and "Controlremoved". It works perfectly when I’m adding or removing a controller. But I need to invoke it every time a child control is resized. (sorry for the quality of the code, it’s a test code)
– Odair José Gambert Nossa
I’ll do the edits, sorry, I’m new to the community.
– Odair José Gambert Nossa
Yes. It doesn’t work for that scenario.
– Odair José Gambert Nossa
Right, try to put as much explained as possible, in the same question, and put the code you have already made, as code same and not print screen. Use print screen only for the screen. That lot of comment, I will delete later
– Rovann Linhalis
Edited question.
– Odair José Gambert Nossa