2014-01-23 50 views
0

我正在使用.net 4.5的Winform應用程序。如何將滾動條重置爲窗口頂部

在我的項目中,我有一個只包含一個按鈕的窗口。

當窗口顯示時,我動態地添加了大量的控件以滿足滾動條的需要。

添加控件後,我將單個按鈕移動到窗口中控件的底部。

將按鈕移動到控件底部的這一步使窗口滾動到底部。我已經通過不將按鈕移動到窗體的底部來測試這一點,並且滾動保持在頂部。

我試過「this.VerticalScroll.Value = 0;」在設置按鈕的位置之前和之後。

這裏是代碼,這樣你可以得到什麼,我試圖做一個清晰的概念:

public SignoffSurvey(int task_id) 
    { 
     InitializeComponent(); 

     this.task_id = task_id; 

     int form_top = 10; 
     int question_num = 0; 

     XmlDocument doc = new XmlDocument(); 
     doc.Load(DBHandler.getSetting("files_directory") + "\\" + "questionaire.xml"); 

     foreach (XmlNode node in doc.SelectNodes("/Questions/Question")) 
     { 
      question_num++; 

      string type = node.Attributes["type"].Value; 
      int top = 0; 

      Panel pnl = new Panel(); 
      pnl.AutoSize = true; 
      pnl.Top = form_top; 
      pnl.Left = 10; 

      Label text_lbl = new Label(); 
      text_lbl.Top = top; 
      text_lbl.AutoSize = true; 
      text_lbl.Text = node["text"].InnerText; 
      pnl.Controls.Add(text_lbl); 

      top += text_lbl.Height + 5; 

      if (type == "mc" || type == "mct") 
      { 
       XmlNode choices = node["choices"]; 
       Boolean fc = false; 
       foreach (XmlNode choice in choices.ChildNodes) 
       { 
        RadioButton rb = new RadioButton(); 
        rb.AutoSize = true; 
        rb.Text = choice.InnerText; 
        rb.Top = top; 
        rb.Left = 10; 
        top += rb.Height + 5; 
        pnl.Controls.Add(rb); 

        if (!fc) // check first item. 
        { 
         fc = true; 
         rb.Checked = true; 
        } 
       } 
      } 

      if (type == "mct" || type == "txt") 
      { 

       TextBox tb = new TextBox(); 
       tb.Multiline = true; 
       tb.Width = 500; 
       tb.Height = 250; 
       tb.Top = top; 
       tb.Left = 10; 
       pnl.Controls.Add(tb); 
       top += tb.Height + 5; 
      } 

      pnl.Height = top; 
      this.Controls.Add(pnl); 
      form_top += pnl.Height + 10; 
     } 

     this.VerticalScroll.Value = 0; 
     this.save_btn.Top = form_top; 


    } 

我怎麼能強迫窗口垂直滾動頂端無論在哪裏這1個按鈕移動到?

回答

0

Okie。對於任何有類似問題的人,這裏是問題的原因以及如何克服它。

問題似乎是窗口上默認選中了單個按鈕。所以當窗口啓動時,它會滾動到按鈕的位置。

我加了這一點:

this.Controls[1].Select(); 

加入我的所有控件後。

您必須使用[1],因爲窗體上已有的按鈕是[0]。

相關問題