2016-11-07 14 views
1

私はユーザーコントロールを追加するフォームを持っています。このユーザーコントロールには、そこにあるすべてのコントロール(ラベル、ラジオボタン、動的に追加される)をスクロールできる垂直スクロールバーがあります。一番下には保存ボタンがあり、フォームをロードするとフォーカスが得られます(ボタンは動的に追加されません)。これにより、ユーザーコントロールが下方向にスクロールします。私のユーザーコントロールの上にスクロール/ Winformsの下部コントロールからフォーカスを削除

ユーザーコントロールを上にスクロールする必要があります。

  • セットVerticalScroll:

    私が試してみました何(以下の私のコードを参照してください)。これは明らかにユーザーコントロールの読み取り専用のプロパティですので、私はそれを設定することができませんでした。

  • UserControlのActiveControlを一番上のコントロールに設定します。コントロールを動的に追加するので、これを行うことはできません。
  • トップコントロールのTabIndexを設定します。以前と同じストーリーです。

ユーザーコントロール

public partial class InterviewScreenUserControl : UserControl 
{ 
    // Private members. 
    private readonly IInterviewScreenView _view; 
    private List<List<Control>> answers; 
    private List<string> results; 
    private List<List<string>> questions; 
    private List<List<string>> maturityAnswers; 
    private List<List<string>> complianceAnswers; 

    // Initialize user control with IInterviewScreenView. Subscribe to necessary events. 
    public InterviewScreenUserControl(IInterviewScreenView view) 
    { 
     InitializeComponent(); 
     _view = view; 
     _view.InitializingUserControl += InitializeInterview; 
    } 

    // Initialize the interview by adding questions and answer input. 
    public void InitializeInterview(object sender, EventArgs e) 
    { 
     answers = new List<List<Control>>(); 
     questions = new List<List<string>>(_view.Questions); 
     maturityAnswers = new List<List<string>>(_view.MaturityAnswers); 
     complianceAnswers = new List<List<string>>(_view.ComplianceAnswers); 

     int startingPoint = this.Size.Width/15; 
     int offset = 10; 
     int labelWidth = this.Size.Width/3 - 20; 
     int lastControl = 0; 
     int lastWidth = 0; 
     int radioSeparation = 20; 
     int questionNr = 0; 

     for (int i = 0; i < questions.Count; i++) 
     { 
      for (int j = 0; j < questions[i].Count; j++) 
      { 
       List<Control> answerValues = new List<Control>(); 

       Panel left_panel = new Panel(); 
       left_panel.Dock = DockStyle.Left; 
       left_panel.BackColor = Color.Gray; 
       left_panel.Size = new Size(2, 0); 
       Controls.Add(left_panel); 

       Panel right_panel = new Panel(); 
       right_panel.Dock = DockStyle.Right; 
       right_panel.BackColor = Color.Gray; 
       right_panel.Size = new Size(2, 0); 
       Controls.Add(right_panel); 

       Label q_label = new Label(); 
       q_label.Location = new Point(startingPoint, lastControl + offset); 
       q_label.Text = questions[i][j]; 
       q_label.Font = new Font("Arial", 12F, FontStyle.Regular); 
       q_label.AutoSize = true; 
       q_label.MaximumSize = new Size(this.Width - 50, 75); 
       Controls.Add(q_label); 

       Label m_label = new Label(); 
       m_label.Location = new Point(startingPoint, q_label.Bounds.Bottom + offset); 
       m_label.Text = "Maturity level"; 
       m_label.Font = new Font("Arial", 12F, FontStyle.Bold); 
       m_label.AutoSize = true; 
       Controls.Add(m_label); 

       Label c_label = new Label(); 
       c_label.Location = new Point(startingPoint + labelWidth, q_label.Bounds.Bottom + offset); 
       c_label.Text = "Compliance level"; 
       c_label.Font = new Font("Arial", 12F, FontStyle.Bold); 
       c_label.AutoSize = true; 
       Controls.Add(c_label); 

       Label n_label = new Label(); 
       n_label.Location = new Point(startingPoint + labelWidth * 2, q_label.Bounds.Bottom + offset); 
       n_label.Text = "Notes"; 
       n_label.Font = new Font("Arial", 12F, FontStyle.Bold); 
       n_label.AutoSize = true; 
       Controls.Add(n_label); 

       TextBox notes = new TextBox(); 
       notes.Location = new Point(startingPoint + labelWidth * 2, n_label.Bounds.Bottom); 
       notes.Multiline = true; 
       notes.Font = new Font("Arial", 10F, FontStyle.Regular); 
       notes.Size = new Size(labelWidth, 135); 
       notes.BackColor = Color.AliceBlue; 
       if(_view.Results != null) notes.Text = _view.Results[questionNr * 4 + 3]; 
       Controls.Add(notes); 
       answerValues.Add(notes); 
       lastControl = notes.Bounds.Bottom; 
       lastWidth = notes.Bounds.Right; 

       Panel m_panel = new Panel(); 
       m_panel.Location = new Point(startingPoint + 5, m_label.Bounds.Bottom); 
       m_panel.AutoSize = true; 
       m_panel.BackColor = Color.Transparent; 
       Controls.Add(m_panel); 

       Panel c_panel = new Panel(); 
       c_panel.Location = new Point(startingPoint + labelWidth + 5, c_label.Bounds.Bottom); 
       c_panel.AutoSize = true; 
       c_panel.BackColor = Color.Transparent; 
       Controls.Add(c_panel); 


       for (int x = 0; x < maturityAnswers[i].Count; x++) 
       { 
        RadioButton m_answer = new RadioButton(); 
        m_answer.Text = maturityAnswers[i][x]; 

        if(_view.Results != null && m_answer.Text == _view.Results[questionNr * 4 + 1]) 
        { 
         m_answer.Checked = true; 
        } 

        m_answer.AutoSize = true; 
        m_answer.Location = new Point(5, radioSeparation * x); 
        m_panel.Controls.Add(m_answer); 
        answerValues.Add(m_answer); 
       } 

       for (int x = 0; x < complianceAnswers[i].Count; x++) 
       { 
        RadioButton c_answer = new RadioButton(); 
        c_answer.Text = complianceAnswers[i][x]; 

        if (_view.Results != null && c_answer.Text == _view.Results[questionNr * 4 + 2]) 
        { 
         c_answer.Checked = true; 
        } 

        c_answer.AutoSize = true; 
        c_answer.Location = new Point(5, radioSeparation * x); 
        c_panel.Controls.Add(c_answer); 
        answerValues.Add(c_answer); 
       } 

       if (m_panel.Bounds.Bottom > lastControl) 
       { 
        lastControl = m_panel.Bounds.Bottom; 
       } 
       if (c_panel.Bounds.Bottom > lastControl) 
       { 
        lastControl = c_panel.Bounds.Bottom; 
       } 

       this.answers.Add(answerValues); 
       questionNr++; 
      } 
     } 
     saveResultsButton.Size = new Size(130, 25); 
     saveResultsButton.Location = new Point(lastWidth - saveResultsButton.Width, lastControl + offset); 
     saveResultsButton.Text = "Save answers"; 
     saveResultsButton.BackColor = SystemColors.ButtonHighlight; 
     this.Controls.Add(saveResultsButton); 

     _view.AddUserControl(); 
    } 
} 

答えて

0

わかりましたので、私はいくつかのテストの後に、それを解決しました。

私のダイナミックコントロールのTabIndexプロパティを設定できなかったので、ボタンのTabIndexを削除して、ユーザーコントロールがフォーカスしてすべてスクロールすることができなくてはなりませんでした。 falseにタブストップを設定することにより

this.saveResultsButton.TabStop = false; 

、これが達成されたとユーザーコントロールは、ボタンにフォーカスせずにロードされたので、それが一番上にロードしました。

関連する問題