C# 滚动条未响应最大值

C# 滚动条未响应最大值,c#,.net,winforms,C#,.net,Winforms,我正在尝试在windows窗体的面板控件中添加自定义滚动条。除了最大值外,所有的东西都运转良好 面板包含提供路径中的文件名。为了显示滚动条,我使用了变量。将新文件添加到面板时,变量将递增。标签控件用于显示文件名。将所有文件添加到面板后。我检查面板视图外是否有任何文件。如果有,则滚动条可见,最大值设置为所需高度变量。所需高度变量是控件的总高度。所有组件都是通过工具添加的 下面是MainForm类的代码 public partial class MainForm : Form {

我正在尝试在windows窗体的面板控件中添加自定义滚动条。除了最大值外,所有的东西都运转良好

面板包含提供路径中的文件名。为了显示滚动条,我使用了变量。将新文件添加到面板时,变量将递增。标签控件用于显示文件名。将所有文件添加到面板后。我检查面板视图外是否有任何文件。如果有,则滚动条可见,最大值设置为所需高度变量。所需高度变量是控件的总高度。所有组件都是通过工具添加的

下面是MainForm类的代码

public partial class MainForm : Form
    {
        file obj=new file();
        Label[] file_names;

        public MainForm()
        {

            InitializeComponent();
// viewpanel is the name of panel to display the file name
            viewpanel.AutoScroll=false;
// viewscrollbar is the name of scroll bar associated with viewpanel
            viewscrollbar.ValueChanged+= new EventHandler(viewscroll);
            viewscrollbar.Enabled=true;
            viewscrollbar.Visible=false;
        }
视图更新面板的代码。此函数用于遍历标签数组并将每个标签添加到viewpanel

void updateviewpanel()
        {
         //reheight is a variavle used to calculate total height     //required.
            int reheight=0;
            int yaixs=0;
            for(int i=0;i<file_names.Length;i++)
            {

                file_names[i].Location=new Point(0,yaixs);
                file_names[i].Height=20;
                file_names[i].Width=viewpanel.Width-5;
                file_names[i].BorderStyle=BorderStyle.Fixed3D;

                yaixs=yaixs+20;
                //the reheight variable is increment each time a label is added to panel.
                reheight=reheight+20;
                viewpanel.Controls.Add(file_names[i]);

            }// end  of for loop
// check to see if required height is greater then actual height  of panel
                if(reheight>viewpanel.Height)
                    {
                            viewscrollbar.Visible=true;
                        viewscrollbar.Maximum=reheight+viewscrollbar.Height;
                        viewpanel.VerticalScroll.Enabled=false;
                        viewpanel.VerticalScroll.Visible=false;
// to output the Maximum value of scroll. 
                        label1.Text=Convert.ToString(viewscrollbar.Maximum);
                    }// end of if
            }
void updateviewpanel()
{
//reheight是一个用于计算总高度的变量//必需。
int-reheight=0;
int-yaixs=0;
对于(int i=0;iviewpanel.Height)
{
viewscrollbar.Visible=true;
viewscrollbar.Maximum=reheight+viewscrollbar.Height;
viewpanel.VerticalScroll.Enabled=false;
viewpanel.VerticalScroll.Visible=false;
//以输出滚动的最大值。
label1.Text=Convert.ToString(viewscrollbar.max);
}//if结束
}
滚动的最大值为1390。但是scroll的值不会超过100。但是,当垂直滚动条buildin scroll bar处于活动状态时,自定义滚动条verticalscrollbar的值将超过100。这对我来说毫无意义

感谢您抽出时间通读