How to overlapping textbox auto adjust width on run time?

des11.zip (7.6 KB)

@John.He , @Professionalize.Discourse , @simon.zhao, please given early response.

At runtime, check if an existing sheet text overlaps based on the (x + width, y + height) coordinates. If it does, then adjust the width accordingly.

@imparthgalani

Cause

To achieve an auto-adjusting width for overlapping text boxes at runtime, it is essential to ensure that the text boxes can dynamically resize based on their content. This typically involves handling events that trigger when the content changes.

Solution

You can implement a method that calculates the required width of the text box based on its content and then sets the width accordingly. This can be done in the event handler for content changes or during the layout process.

Code

Here’s a simple example in C# that demonstrates how to adjust the width of a text box based on its content:

private void AdjustTextBoxWidth(TextBox textBox)
{
    // Measure the size of the text
    SizeF textSize = textBox.CreateGraphics().MeasureString(textBox.Text, textBox.Font);
    
    // Set the width of the text box to the measured width
    textBox.Width = (int)textSize.Width + 10; // Adding some padding
}

// Example usage
private void textBox_TextChanged(object sender, EventArgs e)
{
    AdjustTextBoxWidth((TextBox)sender);
}

In this example, the AdjustTextBoxWidth method measures the text size and adjusts the width of the text box accordingly. You would call this method in the TextChanged event of the text box.

Next Steps

  1. Integrate the above code into your application where you handle text box content.
  2. Test the functionality to ensure that the text boxes resize correctly as expected.
  3. If you have multiple overlapping text boxes, ensure that their positions are also adjusted to prevent visual overlap.

Please implement these steps, and feel free to reach out if you need further assistance!

@imparthgalani,

See and try the following sample code with (updated) template Excel file (attached in the zipped archive) for your reference. The output Excel file is also included in the attached zipped archive. Please refer to the code segment and write/update your own code by yourselves accordingly for your custom needs.
e.g.,
Sample code:

Workbook wb = new Workbook("e:\\test2\\des11_new1.xlsx");
Worksheet ws = wb.Worksheets[0];
Aspose.Cells.Drawing.ShapeCollection shapes = ws.Shapes;

// Sort shapes left to right by their X (LeftToCorner) value
List<Aspose.Cells.Drawing.Shape> shapeList = new List<Aspose.Cells.Drawing.Shape>(shapes.Cast<Aspose.Cells.Drawing.Shape>());
shapeList.Sort((a, b) => a.LeftToCorner.CompareTo(b.LeftToCorner));

int padding = 5; // minimum space between shapes

for (int i = 0; i < shapeList.Count - 1; i++)
{
    Aspose.Cells.Drawing.Shape current = shapeList[i];
    Aspose.Cells.Drawing.Shape next = shapeList[i + 1];

    int curLeft = current.LeftToCorner;
    int curRight = curLeft + current.Width;

    int nextLeft = next.LeftToCorner;

    if (curRight + padding > nextLeft)
    {
        // Overlap detected — adjust width
        int newWidth = Math.Max(10, nextLeft - curLeft - padding); // prevent collapsing
        if (newWidth < current.Width)
        {
            Console.WriteLine($"Reducing width of Shape[{i}] from {current.Width} to {newWidth} to avoid overlap with Shape[{i + 1}]");
            current.Width = newWidth;
        }
    }
}

wb.Save("e:\\test2\\fixed_shapes.xlsx");

files1.zip (16.0 KB)

Hope, this helps a bit.

1 Like

@amjad.sahi , Thanks you provide code sample. I try this code after inform you it will work or not.

@imparthgalani
Please take your time to try the suggested solutions. Hopefully, your issue will be sorted out. Please let us know your feedback.