簡體   English   中英

ScrollableControl和事件中的Windows窗體控件

[英]Windows Forms Controls in a ScrollableControl and Events

我注意到,單擊ScrollableControl(面板等)中包含的控件時,並非始終觸發Click事件或其他控件行為。

如果單擊的控件沒有焦點,並且僅部分可見,則將其滾動到視圖中。 這是我所期望的,但是不會觸發Click事件,也不會發生其他控件行為。

如果控件已經具有焦點並且僅部分可見,則事件會被觸發。

復選框-滾動到視圖中,選中狀態不會更改。 CheckedListBox-滾動到視圖中,未選擇單擊的項目。 TreeView-滾動到視圖中,未選擇單擊的節點。 按鈕-滾動到視圖中,不會引發click事件。

要重現此內容,您可以執行以下操作:

  1. 將以上任何控件添加到面板中
  2. 為Click,SelectedItemChanged等添加事件處理程序
  3. 調整表單大小,以便在面板上顯示滾動條
  4. 滾動面板,使其中一個控件部分可見
  5. 單擊部分可見的控件

有什么辦法可以確保事件被觸發?

大衛,

它為我工作。

Form1.Designer.cs中的代碼

namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.AutoScroll = true;
            this.panel1.Controls.Add(this.textBox2);
            this.panel1.Controls.Add(this.textBox1);
            this.panel1.Location = new System.Drawing.Point(86, 75);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(176, 70);
            this.panel1.TabIndex = 0;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(109, 17);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(100, 20);
            this.textBox2.TabIndex = 1;
            this.textBox2.Click += new System.EventHandler(this.textBox2_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(3, 17);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(100, 20);
            this.textBox1.TabIndex = 0;
            this.textBox1.Click += new System.EventHandler(this.textBox1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(493, 271);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.TextBox textBox1;
    }
}

Form1.cs中的代碼

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

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

        private void textBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Click1");
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Click2");
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM