如何在窗体加载时自动向下滚动Windows窗体面板上的滚动条?

时间:2018-12-20 19:09:16

标签: c# visual-studio-2017

我正在使用C#在Windows窗体上制作pdf文件打开器。我想使Windows窗体从特定的文件夹中获取pdf文件名到combobox,并使combobox每隔x分钟选择下一个内容。但是由于axAcroPDF1使用的是Adobe Reader的设置,因此如果不保持比例,就无法使单页显示全屏显示,而在不保持原始宽高比的情况下,左右两边都有巨大的空白区域。由于我无法使用Adobe Reader摆脱它,因此必须使用合适的宽度,但这会使页面对于阅读器而言太大,并且如果不向下滚动就无法查看全部内容。

我已经尝试了所有可以想到的方法,但是我无法使其正常工作。

private void Form1_Load(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Maximized;
            DirectoryInfo test = new DirectoryInfo(@"\c:\temp\"); 
            FileInfo[] Files = test.GetFiles("*.pdf"); //Getting Text files

            var fileNames = Files.Select(f => Path.GetFileNameWithoutExtension(f.Name)).ToList();
            comboBox1.DataSource = fileNames;
            timerset();
        }


        private void panel1_ControlAdded(object sender, ControlEventArgs e)
        {

        }

        public void axSetting()
        {
            axAcroPDF1.setShowToolbar(false);
            axAcroPDF1.setView("FitH");
            axAcroPDF1.setPageMode("none");
            axAcroPDF1.setShowScrollbars(false);
            axAcroPDF1.setLayoutMode("SinglePage");            
            axAcroPDF1.Show();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            axAcroPDF1.LoadFile(@"c:\temp\ + comboBox1.Text + ".pdf");
            axAcroPDF1.src = @"c:\temp\" + comboBox1.Text + ".pdf";
            axSetting();
        }

        public void comboBoxSelect()
        {
            if (comboBox1.SelectedIndex < (comboBox1.Items.Count - 1))
            {
                comboBox1.SelectedIndex += 1;
            }
            else
            {
                comboBox1.SelectedIndex = 0;
                DirectoryInfo test = new DirectoryInfo(@"c:\temp\");
                FileInfo[] Files = test.GetFiles("*.pdf");

                var fileNames = Files.Select(f => Path.GetFileNameWithoutExtension(f.Name)).ToList();
                comboBox1.DataSource = fileNames;
            }
        }

        public void timerset()
        {
            timer1 = new System.Windows.Forms.Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 10000; // in miliseconds            
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            comboBoxSelect();
        }

如何在每次加载pdf文件时使面板上的自动滚动从顶部滚动到底部?

2 个答案:

答案 0 :(得分:0)

我通常这样设置AutoScrollPosition
您需要在表单的Shown()事件(而不是Load())中拥有此代码。

[ScrollableContainer].AutoScrollPosition = 
    new Point(0, [ChildControl].Height - [ScrollableContainer].Height);

[ChildControl]的大小当然大于其[ScrollableContainer]的大小。

也可以简单地是:

[ScrollableContainer].AutoScrollPosition = new Point(0, [ChildControl].Height);

答案 1 :(得分:0)

立即尝试: 我将其更改为真实的:AxAcroPDF1.setShowToolbar(True) axAcroPDF1.setShowScrollbars(True);,并添加了comboBox1.SelectedIndex = 1;axAcroPDF1.AutoScrollOffset = new Point(axAcroPDF1.AutoScrollOffset.X, 10); axAcroPDF1.AutoScrollOffset = new Point(axAcroPDF1.AutoScrollOffset.Y, 10);

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Maximized;
            DirectoryInfo test = new DirectoryInfo(@"c:\temp\");
            FileInfo[] Files = test.GetFiles("*.pdf"); //Getting Text files

            var fileNames = Files.Select(f => Path.GetFileNameWithoutExtension(f.Name)).ToList();
            comboBox1.DataSource = fileNames;
            comboBox1.SelectedIndex = 1;
       axAcroPDF1.AutoScrollOffset = new Point(axAcroPDF1.AutoScrollOffset.X, 10);
        axAcroPDF1.AutoScrollOffset = new Point(axAcroPDF1.AutoScrollOffset.Y, 10);
            timerset();
        }


        private void panel1_ControlAdded(object sender, ControlEventArgs e)
        {

        }

        public void axSetting()
        {
            axAcroPDF1.setShowToolbar(true);
            axAcroPDF1.setView("FitH");
            axAcroPDF1.setPageMode("none");
            axAcroPDF1.setShowScrollbars(true);
            axAcroPDF1.setLayoutMode("SinglePage");
            axAcroPDF1.Show();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            axAcroPDF1.LoadFile(@"c:\temp\" + comboBox1.Text + ".pdf");
            axAcroPDF1.src = @"c:\temp\" + comboBox1.Text + ".pdf";
            axSetting();
        }

        public void comboBoxSelect()
        {
            if (comboBox1.SelectedIndex < (comboBox1.Items.Count - 1))
            {
                comboBox1.SelectedIndex += 1;
            }
            else
            {
                comboBox1.SelectedIndex = 0;
                DirectoryInfo test = new DirectoryInfo(@"c:\temp\");
                FileInfo[] Files = test.GetFiles("*.pdf");

                var fileNames = Files.Select(f => Path.GetFileNameWithoutExtension(f.Name)).ToList();
                comboBox1.DataSource = fileNames;
            }
        }

        public void timerset()
        {
            timer1 = new System.Windows.Forms.Timer();
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Interval = 10000; // in miliseconds            
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            comboBoxSelect();
        }
    }
}