我有一個userControl問題。 當我嘗試將其懸停以更改所有組件的顏色時,「ChangeColor」功能無法正確激活。更改UserControl元素的顏色
如果我將鼠標懸停在標籤或用戶控件的PictureBox的,它是誘發事件MouseLeave
這是我的用戶設計的
public partial class infoUser : UserControl
{
public infoUser()
{
InitializeComponent();
}
public void SetNome(string nome)
{
labelUserLogged.Text = nome;
}
public void ChangeColor(System.Drawing.Color color)
{
labelUserLogged.BackColor = color;
pictureBoxUser.BackColor = color;
}
private void infoUser_MouseHover(object sender, EventArgs e)
{
ChangeColor(Color.CadetBlue);
}
private void infoUser_MouseLeave(object sender, EventArgs e)
{
ChangeColor(Color.WhiteSmoke);
}
}
代碼
private void InitializeComponent()
{
this.labelUserLogged = new System.Windows.Forms.Label();
this.pictureBoxUser = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBoxUser)).BeginInit();
this.SuspendLayout();
//
// labelUserLogged
//
this.labelUserLogged.BackColor = System.Drawing.SystemColors.Control;
this.labelUserLogged.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.labelUserLogged.Cursor = System.Windows.Forms.Cursors.Hand;
this.labelUserLogged.Location = new System.Drawing.Point(0, 0);
this.labelUserLogged.Name = "labelUserLogged";
this.labelUserLogged.Size = new System.Drawing.Size(167, 27);
this.labelUserLogged.TabIndex = 3;
this.labelUserLogged.Text = "Non loggato";
this.labelUserLogged.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// pictureBoxUser
//
this.pictureBoxUser.BackColor = System.Drawing.Color.Transparent;
this.pictureBoxUser.Cursor = System.Windows.Forms.Cursors.Hand;
this.pictureBoxUser.Image = global::Castor.Gestionale.Properties.Resources.user_icon;
this.pictureBoxUser.Location = new System.Drawing.Point(5, 6);
this.pictureBoxUser.Name = "pictureBoxUser";
this.pictureBoxUser.Size = new System.Drawing.Size(18, 15);
this.pictureBoxUser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBoxUser.TabIndex = 4;
this.pictureBoxUser.TabStop = false;
//
// infoUser
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.pictureBoxUser);
this.Controls.Add(this.labelUserLogged);
this.Name = "infoUser";
this.Size = new System.Drawing.Size(171, 30);
this.MouseLeave += new System.EventHandler(this.infoUser_MouseLeave);
this.MouseHover += new System.EventHandler(this.infoUser_MouseHover);
((System.ComponentModel.ISupportInitialize)(this.pictureBoxUser)).EndInit();
this.ResumeLayout(false);
}
我們應該看到xaml或設計器代碼的控件設計。 – jackjop
它是WinForms還是WPF? – Verbon
@Verbon它在WinForms –