locked
TextBox vertical stretch? RRS feed

  • Question

  • I've been searching and haven't found the answer for this yet (too many common key words bringing up just about every piece of code there is? :) ) so sorry for the simple question in advance.

    Anyways what I'm wanting to do is get a textbox to accept multiple lines and stretch vertically to fill a space as the window is resized.  Horizontally isn't a problem and there's definitely a lot of room vertically in the container it's in but when I do something like set the vertical alignment to stretch it won't have any effect.  Is there some other property to set to make this happen or is there no easy way to have this occur?

    Thanks!
    Thursday, January 15, 2009 3:15 PM

Answers


  • Pay attention: do not set the Height or Width of the RichTextBox.
    If you set the Height or Width, the RichTextBox will not stretch even you resize the window thath contain it.
    The effect is:


    I use a DataBinding to show the ActualWidth and ActualHeight.
    Then you can see the change of size more clear.
    XAML:
    <Window x:Class="DataContextMulti.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        HorizontalContentAlignment="Stretch" 
            VerticalContentAlignment="Stretch" 
            Title="Window1" Height="300" Width="300"
        <Grid > 
            <RichTextBox  Margin="32,55,46,107" Name="richTextBox1" /> 
            <Label Foreground="Red"  Height="28" HorizontalAlignment="Left" Margin="79,12,0,0" Name="label1" VerticalAlignment="Top" Width="31"
                <Binding ElementName="richTextBox1" Path="ActualWidth"
     
                </Binding> 
     
            </Label> 
            <Label Foreground="Red" Content="{Binding ElementName=richTextBox1, Path=ActualHeight}" Height="28" Margin="0,12,35,0" Name="label2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="49" /> 
            <Label Height="28" Margin="116,12,90,0" Name="label3" VerticalAlignment="Top">ActualHeight</Label> 
            <Label Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label4" VerticalAlignment="Top" Width="70">ActualWidth</Label> 
        </Grid> 
    </Window> 
     
     
     
     
     

    Hope it helps.

    ----------------------------------------------------------

    Please mark this thread as answer if you think it is helpful.

    Thank you!



    • Proposed as answer by Tao Liang Wednesday, January 21, 2009 6:58 AM
    • Marked as answer by Tao Liang Wednesday, January 21, 2009 10:05 AM
    • Edited by Tao Liang Thursday, January 22, 2009 1:45 AM
    Wednesday, January 21, 2009 6:56 AM
  • liangtom, I don't know why you're saying that it doesn't work with a textbox, because it does. If you can tell me how to record the movement, I'll do so. Or I can just send you the compiled exe.

    <Window x:Class="WpfApplication1.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window1" Height="300" Width="300">  
        <Grid> 
            <TextBox Margin="32,55,46,107" VerticalAlignment="Stretch" Name="richTextBox1"  /> 
            <Label Foreground="Red"  Height="28" HorizontalAlignment="Left" Margin="79,12,0,0" Name="label1" VerticalAlignment="Top" Width="31">  
                <Binding ElementName="richTextBox1" Path="ActualWidth">  
     
                </Binding> 
     
            </Label> 
            <Label Foreground="Red" Content="{Binding ElementName=richTextBox1, Path=ActualHeight}" Height="28" Margin="0,12,35,0" Name="label2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="49" /> 
            <Label Height="28" Margin="116,12,90,0" Name="label3" VerticalAlignment="Top">ActualHeight</Label> 
            <Label Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label4" VerticalAlignment="Top" Width="70">ActualWidth</Label> 
        </Grid> 
    </Window> 

    It shouldn't matter, but I'm using Windows Vista 32-bits (English), and C# Express SP1.
    • Marked as answer by Tao Liang Thursday, January 22, 2009 1:45 AM
    Wednesday, January 21, 2009 2:17 PM

All replies

  • It works for me so I guess there must be something else in your code causing this.
    Thursday, January 15, 2009 3:38 PM

  • Pay attention: do not set the Height or Width of the RichTextBox.
    If you set the Height or Width, the RichTextBox will not stretch even you resize the window thath contain it.
    The effect is:


    I use a DataBinding to show the ActualWidth and ActualHeight.
    Then you can see the change of size more clear.
    XAML:
    <Window x:Class="DataContextMulti.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        HorizontalContentAlignment="Stretch" 
            VerticalContentAlignment="Stretch" 
            Title="Window1" Height="300" Width="300"
        <Grid > 
            <RichTextBox  Margin="32,55,46,107" Name="richTextBox1" /> 
            <Label Foreground="Red"  Height="28" HorizontalAlignment="Left" Margin="79,12,0,0" Name="label1" VerticalAlignment="Top" Width="31"
                <Binding ElementName="richTextBox1" Path="ActualWidth"
     
                </Binding> 
     
            </Label> 
            <Label Foreground="Red" Content="{Binding ElementName=richTextBox1, Path=ActualHeight}" Height="28" Margin="0,12,35,0" Name="label2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="49" /> 
            <Label Height="28" Margin="116,12,90,0" Name="label3" VerticalAlignment="Top">ActualHeight</Label> 
            <Label Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label4" VerticalAlignment="Top" Width="70">ActualWidth</Label> 
        </Grid> 
    </Window> 
     
     
     
     
     

    Hope it helps.

    ----------------------------------------------------------

    Please mark this thread as answer if you think it is helpful.

    Thank you!



    • Proposed as answer by Tao Liang Wednesday, January 21, 2009 6:58 AM
    • Marked as answer by Tao Liang Wednesday, January 21, 2009 10:05 AM
    • Edited by Tao Liang Thursday, January 22, 2009 1:45 AM
    Wednesday, January 21, 2009 6:56 AM
  • liangtom, I don't know why you're saying that it doesn't work with a textbox, because it does. If you can tell me how to record the movement, I'll do so. Or I can just send you the compiled exe.

    <Window x:Class="WpfApplication1.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        Title="Window1" Height="300" Width="300">  
        <Grid> 
            <TextBox Margin="32,55,46,107" VerticalAlignment="Stretch" Name="richTextBox1"  /> 
            <Label Foreground="Red"  Height="28" HorizontalAlignment="Left" Margin="79,12,0,0" Name="label1" VerticalAlignment="Top" Width="31">  
                <Binding ElementName="richTextBox1" Path="ActualWidth">  
     
                </Binding> 
     
            </Label> 
            <Label Foreground="Red" Content="{Binding ElementName=richTextBox1, Path=ActualHeight}" Height="28" Margin="0,12,35,0" Name="label2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="49" /> 
            <Label Height="28" Margin="116,12,90,0" Name="label3" VerticalAlignment="Top">ActualHeight</Label> 
            <Label Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label4" VerticalAlignment="Top" Width="70">ActualWidth</Label> 
        </Grid> 
    </Window> 

    It shouldn't matter, but I'm using Windows Vista 32-bits (English), and C# Express SP1.
    • Marked as answer by Tao Liang Thursday, January 22, 2009 1:45 AM
    Wednesday, January 21, 2009 2:17 PM
  • Oh yes, _patrick_ is absolutely right.

    It also work with a TextBox!

    The most import point is never setting the Width or Height the TextBox if you want the stretch effect.

    I made a mistake when I build a test project.
    The Height was set a value!
    Thursday, January 22, 2009 1:43 AM

  • It take 3 steps to upload a video:


    1 Convert the video file into GIF format.

    2 Upload the gif image to a server(I use picasa ).

    3 Embed the image into the content of your thread

    Thursday, January 22, 2009 2:00 AM
  • Exactly, the TextBox sets by default it's height (23), and the RichEdit doesn't.
    Thanks for the explanation about uploading movies, although actually I was refering to what program you're using to capture the movement.

    Regards,
    Patrick

    p.d. regards to Sai as well LOL
    Thursday, January 22, 2009 11:01 AM