滚动到我的用户控件的顶部/从 Winforms 的底部控件中删除焦点

Scrolling to the top of my User Control / removing focus from bottom control in Winforms

我有一个添加了用户控件的表单。这个用户控件有一个垂直滚动条,可以滚动浏览那里的所有控件(标签、单选按钮,它们是动态添加的)。在底部我有一个保存按钮,它在我加载表单时获得焦点(该按钮不是动态添加的)。这会导致用户控件一直向下滚动。

我希望用户控件滚动到顶部。

我尝试了什么(请看下面我的代码):

用户控制

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();
    }
}

好的,经过一些测试,我已经解决了。

由于我无法为我的动态控件设置 TabIndex 属性,我不得不删除按钮的 TabIndex,这样用户控件就无法聚焦它并一直向下滚动.

this.saveResultsButton.TabStop = false;

通过将 TabStop 设置为 false,实现了这一点,加载用户控件时没有将焦点放在按钮上,因此它加载在顶部。