Search code examples
windows-mobilecompact-frameworkwindows-mobile-6.5windows-mobile-6

How to Change height of label dynamically in windows mobile 6.5


In my app i will get text from server whose length is unknown. can some one give idea for how to change the height of label so that the text will not get cutted off if is larger than the length of label.


Solution

  • Using ctacke's solution adopted to the question (constant width of label):

    protected override void OnPaint(PaintEventArgs e)
    {
        if (NewLabelText != null)
        {
            //get the width and height of the text
            var size = e.Graphics.MeasureString(NewLabelText, label1.Font);
            if(size.Width>label1.Width){
                //how many lines are needed to display the text
                int iLines = (int)(System.Math.Round((size.Width / label1.Width)+.5));
                //multiply with using the normal height of a one line text
                //label1.Height=iLines*label1.PreferredHeight; //preferredHeight not supported by CF
                label1.Height=(int)(iLines*size.Height*1.1); // add some gutter
            }
            label1.Text = NewLabelText;
            NewLabelText = null;
        }
    
        base.OnPaint(e);
    }
    

    I was unable to test that with CF. Possibly PreferredHeight is not available in CF, if so, use label1.height instead.