Elegiac
Elegiac

Reputation: 129

Resizing label width depending on the width of its content

I'm working on a code-editor and want to auto-adjust the width of a label as the number increases. For example, for 1-9 (1 digit) there's a specific width. Then when it gets to 10-99 (2 digits), width of label increases. Then again for then 100-999 (3 digits), etc.

The result should be something like this:

enter image description here

Here is my code:

private void timer_countline_Tick(object sender, EventArgs e)
{
    updateNumberLabel();
}

private void updateNumberLabel()
{
    // we get index of first visible char and number of first visible line
    Point pos = new Point(0, 0);
    int firstIndex = rtb.GetCharIndexFromPosition(pos);
    int firstLine = rtb.GetLineFromCharIndex(firstIndex);

    // now we get index of last visible char and number of last visible line
    pos.X = ClientRectangle.Width;
    pos.Y = ClientRectangle.Height;
    int lastIndex = rtb.GetCharIndexFromPosition(pos);
    int lastLine = rtb.GetLineFromCharIndex(lastIndex);

    // this is point position of last visible char, we'll use its Y value for calculating numberLabel size
    pos = rtb.GetPositionFromCharIndex(lastIndex);

    // finally, renumber label
    numberLabel.Text = "";
    for (int i = firstLine; i <= lastLine + 1; i++)
        numberLabel.Text += i + 1 + "\n";
}

Upvotes: 3

Views: 9841

Answers (1)

MRS1367
MRS1367

Reputation: 1053

You can use TextRenderer for doing what you want. Please check the following code lines (You should add the code lines to TextChanged event of your label):

Please remember that the AutoSize property of your controls must set to False.

This is for changing Width of your control to fit with width of its contents.

yourLabelName.Width = TextRenderer.MeasureText(yourLabelName.Text, yourLabelName.Font).Width;

This is for changing Height of your control to fit with height of its contents.

yourLabelName.Height = TextRenderer.MeasureText(yourLabelName.Text, yourLabelName.Font).Height;

Update 1:

For changing your panel Width for showing all contents in it horizontally, you can use the followng code line:

yourPanelName.Width = yourLabelName.Left + yourLabelName.Width;

For changing your panel Height for showing all contents in it vartically, you can use the followng code line:

yourPanelName.Height = yourLabelName.Top + yourLabelName.Height;

Update 2:

If you are used SplitContainer control, you must change the properties of your SplitContainer as follows:

FixedPanel = none
IsSplitterFixed = False

Then you can use the following lines of code for achieve to what you want:

For changing your SplitContainer panel Width for showing all contents in it horizontally, you can use the followng code line:

int yourLabelNameWidth = TextRenderer.MeasureText(yourLabelName.Text, yourLabelName.Font).Width;
yourSplitContainerName.SplitterDistance = yourLabelName.Left + yourLabelNameWidth;
yourLabelName.Width = yourLabelName.Left + yourLabelNameWidth;

Upvotes: 5

Related Questions