2013-02-05 153 views
0

我在使用WinForms遇到我的真實程序出現問題後正在進行簡約測試應用程序。我把一個小組(小孩)放在一個更大的小組內(父母)。較大的面板將AutoScroll設置爲true。子面板的默認錨點設置爲「上」和「左」。子面板沒有停靠。面板中的面板,自動滾屏

我想要的行爲是當較小面板的位置太偏移時(無論是頂部,底部,左側還是右側)出現滾動條。問題在於它只在太正確或者太低時才起作用。當頂部太多或太多時,沒有滾動條出現。

我使用兩個簡單的按鈕強制兒童面板的位置向左200像素,或向右200像素以快速方式輕鬆修改其位置。

這裏是我的Form1的()代碼:

 private void button1_Click(object sender, EventArgs e) 
    { 
     childPanel.Location = new Point(childPanel.Location.X - 200, childPanel.Location.Y); 
     hostPanel.Invalidate(); 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     childPanel.Location = new Point(childPanel.Location.X + 200, childPanel.Location.Y); 
     hostPanel.Invalidate(); 
    } 

這裏的設計師代碼:

 private void InitializeComponent() 
    { 
     this.hostPanel = new System.Windows.Forms.Panel(); 
     this.childPanel = new System.Windows.Forms.Panel(); 
     this.moveChildLeft = new System.Windows.Forms.Button(); 
     this.moveChildRight = new System.Windows.Forms.Button(); 
     this.hostPanel.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // hostPanel 
     // 
     this.hostPanel.AutoScroll = true; 
     this.hostPanel.BackColor = System.Drawing.SystemColors.AppWorkspace; 
     this.hostPanel.Controls.Add(this.childPanel); 
     this.hostPanel.Location = new System.Drawing.Point(239, 48); 
     this.hostPanel.Name = "hostPanel"; 
     this.hostPanel.Size = new System.Drawing.Size(400, 400); 
     this.hostPanel.TabIndex = 0; 
     // 
     // childPanel 
     // 
     this.childPanel.BackColor = System.Drawing.SystemColors.ButtonHighlight; 
     this.childPanel.Location = new System.Drawing.Point(29, 62); 
     this.childPanel.Name = "childPanel"; 
     this.childPanel.Size = new System.Drawing.Size(342, 259); 
     this.childPanel.TabIndex = 0; 
     // 
     // moveChildLeft 
     // 
     this.moveChildLeft.Location = new System.Drawing.Point(61, 81); 
     this.moveChildLeft.Name = "moveChildLeft"; 
     this.moveChildLeft.Size = new System.Drawing.Size(75, 23); 
     this.moveChildLeft.TabIndex = 1; 
     this.moveChildLeft.Text = "Left 200"; 
     this.moveChildLeft.UseVisualStyleBackColor = true; 
     this.moveChildLeft.Click += new System.EventHandler(this.button1_Click); 
     // 
     // moveChildRight 
     // 
     this.moveChildRight.Location = new System.Drawing.Point(61, 111); 
     this.moveChildRight.Name = "moveChildRight"; 
     this.moveChildRight.Size = new System.Drawing.Size(75, 23); 
     this.moveChildRight.TabIndex = 2; 
     this.moveChildRight.Text = "Right 200"; 
     this.moveChildRight.UseVisualStyleBackColor = true; 
     this.moveChildRight.Click += new System.EventHandler(this.button2_Click); 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(1018, 549); 
     this.Controls.Add(this.moveChildRight); 
     this.Controls.Add(this.moveChildLeft); 
     this.Controls.Add(this.hostPanel); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.hostPanel.ResumeLayout(false); 
     this.ResumeLayout(false); 
    } 
+0

如果它錨定在它們上面,它怎麼可能太高或太低? –

+0

如果我刪除子面板的錨定屬性(即,我將其設置爲無),那麼無論子面板的位置如何,絕對不會在主機面板中顯示滾動條。自動滾動條似乎取決於至少一個錨點的存在。 –

回答

0

Yet - Another的WinForms無能通過WPF迅速解決:

XAML:

<Window x:Class="WpfApplication4.Window3" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Window3" WindowState="Maximized"> 
    <DockPanel> 
     <StackPanel DockPanel.Dock="Top" Orientation="Horizontal"> 
      <Button Content="Left" Click="MoveLeft"/> 
      <Button Content="Right" Click="MoveRight"/> 
     </StackPanel> 
     <Border BorderBrush="Blue" BorderThickness="1" Width="300" Height="300"> 
      <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" x:Name="Scr"> 
       <Grid Background="Green" Width="100" Height="100" x:Name="Grid"/> 
      </ScrollViewer> 
     </Border> 
    </DockPanel> 
</Window> 
背後

代碼:

using System.Windows; 

namespace WpfApplication4 
{ 
    public partial class Window3 : Window 
    { 
     public Window3() 
     { 
      InitializeComponent(); 
     } 

     private void MoveRight(object sender, RoutedEventArgs e) 
     { 
      if (Grid.Margin.Right <= 0) 
      { 
       Grid.Margin = new Thickness(Grid.Margin.Left + 100,0,0,0); 
      } 
      else 
      { 
       Grid.Margin = new Thickness(0, 0, Grid.Margin.Right - 100, 0); 
       Scr.ScrollToHorizontalOffset(Scr.HorizontalOffset - 100); 
      } 
     } 

     private void MoveLeft(object sender, RoutedEventArgs e) 
     { 
      if (Grid.Margin.Left > 0) 
      { 
       Grid.Margin = new Thickness(Grid.Margin.Left - 100, 0, 0, 0); 
      } 
      else 
      { 
       Grid.Margin = new Thickness(0, 0, Grid.Margin.Right + 100, 0); 
       Scr.ScrollToHorizontalOffset(Scr.HorizontalOffset + 100); 
      } 
     } 
    } 
} 

複製並粘貼我的代碼在一個文件 - >新建 - > WPF應用程序,看看效果如何。

0

最終,您可能需要將您的應用程序轉換爲WPF。因爲,Winform被判處死刑。