none
Setting Text & Label Size At RunTime RRS feed

  • Question

  • When you set text box and label sizes at run time, how do you ensure that none of the labels are hidden behind the text box, or the text box does not over run and hide the text of the label?
    Friday, October 24, 2014 2:04 PM

Answers

  • You can move the label like this:

    If TextBox1.Location.X + TextBox1.Size.Width > Label1.Location.X Then
    	Label1.Location = New Point(TextBox1.Location.X + TextBox1.Size.Width, Label1.Location.Y)
    End If
    

    Keep in mind that this can get complicated. If you have several controls in a line, you may have to do a lot of moving, and you will have to decide what to do if there is no room left at the right side of the form. You may want to think of a better way of laying out your form that doesn't require you to do this.

    • Marked as answer by IndigoMontoya Friday, October 24, 2014 2:57 PM
    Friday, October 24, 2014 2:52 PM

All replies

  • textBox1.Size = new Size(200, 30);

    you can't find the hidden controls but you can bring the controls to front

    by using

    textBox1.BringToFront();


    Happy Coding, RDRaja


    Friday, October 24, 2014 2:25 PM
  • textBox1.Size = new Size(200, 30);


    Happy Coding, RDRaja

    How would that ensure that this doesn't make the text box to large and it covers part of the label that is sitting directly behind it?
    Friday, October 24, 2014 2:27 PM
  • you can't find the hidden controls but you can bring the controls to front

    by using

    textBox1.BringToFront();


    Happy Coding, RDRaja

    Friday, October 24, 2014 2:29 PM
  • You should know which label is to the right of the TextBox. Check to see if the new size of the Textbox takes it past the start of the Label.

    If TextBox1.Location.X + TextBox1.Size.Width > Label1.Location.X Then
    	'Do what you want to do if the TextBox overlaps the label
    End If
    

    Or do the check before you change the size of the TextBox.

    If TextBox1.Location.X + 200 <= Label1.Location.X Then
    	TextBox1.Size = new Size(200, 30)
    End If
    

    Friday, October 24, 2014 2:38 PM
  • You should know which label is to the right of the TextBox. Check to see if the new size of the Textbox takes it past the start of the Label.

    If TextBox1.Location.X + TextBox1.Size.Width > Label1.Location.X Then
    	'Do what you want to do if the TextBox overlaps the label
    End If
    
    If the location overlaps like your syntax above, could I then shift the next text box & label after so their is no further overlapping?  If yes, how would I do that?
    Friday, October 24, 2014 2:43 PM
  • You can move the label like this:

    If TextBox1.Location.X + TextBox1.Size.Width > Label1.Location.X Then
    	Label1.Location = New Point(TextBox1.Location.X + TextBox1.Size.Width, Label1.Location.Y)
    End If
    

    Keep in mind that this can get complicated. If you have several controls in a line, you may have to do a lot of moving, and you will have to decide what to do if there is no room left at the right side of the form. You may want to think of a better way of laying out your form that doesn't require you to do this.

    • Marked as answer by IndigoMontoya Friday, October 24, 2014 2:57 PM
    Friday, October 24, 2014 2:52 PM