C# 滚动到我的用户控件顶部/在Winforms中从底部控件移除焦点
我有一个窗体,在上面添加了一个用户控件。此用户控件有一个垂直滚动条,可以滚动其中的所有控件(标签、radiobutton,动态添加)。在底部我有一个save按钮,当我加载表单时它会获得焦点(该按钮不是动态添加的)。这会导致用户控件一直向下滚动 我希望用户控件滚动到顶部 我尝试的内容(请参见下面的代码):C# 滚动到我的用户控件顶部/在Winforms中从底部控件移除焦点,c#,winforms,dynamic,scroll,user-controls,C#,Winforms,Dynamic,Scroll,User Controls,我有一个窗体,在上面添加了一个用户控件。此用户控件有一个垂直滚动条,可以滚动其中的所有控件(标签、radiobutton,动态添加)。在底部我有一个save按钮,当我加载表单时它会获得焦点(该按钮不是动态添加的)。这会导致用户控件一直向下滚动 我希望用户控件滚动到顶部 我尝试的内容(请参见下面的代码): set VerticalScroll:这显然是用户控件上的只读属性,所以我无法设置它 将用户控件上的ActiveControl设置为top控件:我无法执行此操作,因为我是动态添加控件的 设置顶
- set VerticalScroll:这显然是用户控件上的只读属性,所以我无法设置它
- 将用户控件上的ActiveControl设置为top控件:我无法执行此操作,因为我是动态添加控件的
- 设置顶部控件的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();
}
}
public部分类访谈屏幕UserControl:UserControl
{
//私人成员。
私有只读IInterviewScreenView\u视图;
私人名单答案;
私人名单结果;
私人名单问题;
私人列表到期答案;
私人名单合规性回答;
//使用IInterviewScreenView初始化用户控件。订阅必要的事件。
公共访谈屏幕用户控件(IIInterviewScreenView视图)
{
初始化组件();
_视图=视图;
_view.InitializingUserControl+=InitializeInterview;
}
//通过添加问题和答案输入初始化面试。
public void InitializeInterview(对象发送方,事件参数e)
{
答案=新列表();
问题=新列表(_view.questions);
到期答案=新列表(_view.durityAnswers);
complianceAnswers=新列表(查看complianceAnswers);
int startingPoint=this.Size.Width/15;
整数偏移=10;
int labelWidth=this.Size.Width/3-20;
int lastControl=0;
int lastWidth=0;
int放射分离=20;
int=0;
for(int i=0;ithis.saveResultsButton.TabStop = false;