I'm creating a user control in C# but I can't figure out how to do the event stuff. I want to change the backcolor property of the panel on mouse hover but it's not working.
Code:
public partial class QuestionList : UserControl
{
public QuestionList()
{
InitializeComponent();
}
public struct QuestionListItem
{
public string Question { get; set; }
public string Answer { get; set; }
public QuestionListItem(string question, string answer)
{
Question = question;
Answer = answer;
}
}
public void Add(QuestionListItem questionlistItem)
{
Panel panel = new Panel();
panel.Dock = DockStyle.Top;
Label label = new Label();
label.MouseHover += Label_MouseHover;
label.Dock = DockStyle.Fill;
label.Text = questionlistItem.Question;
panel.Controls.Add(label);
Controls.Add(panel);
}
//Here (no idea what I just did..)
private void Label_MouseHover(Object sender, EventArgs e)
{
Label label = (Label)sender;
Panel panel = (Panel)label.Container;
panel.BackColor = Color.Red;
}
}
I think you have added your event handler right. The problem is with the line you put in your event handler:
Panel panel = (Panel)label.Container;
Should be
Panel panel = (Panel)label.Parent;
Change the Container into Parent.
Also, I think it is best to use VS designer to test what is the strongly-typed signature of the event handler. In your signature, you use EventArgs. I believe it should be MouseEventArgs instead.
Related
I extended the button control to have also LabelName. When I press the button I need to write the name of the button in the label.
My first idea was using events - easy and simple.
The question is: Is there more elegant way to do it? (I've been asked to bind the button and the label)...
I think that the best way to do it would be to use an action listener and the best way to use the action listener would be to build it into your class that extends the button control so that the user doesn't have to do this on their own. It would look like this.
class Button2 : Button
{
public string LabelName = "";
public Button2()
{
this.Click += this.SetLabelName;
}
private void SetLabelName(object sender, EventArgs e)
{
this.LabelName = "Something?";
}
//You could also do this instead.
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
}
}
If you're talking about changing the Text property of an external Label control, then simply create a property in your Button to hold a reference to a Label. You can set this via the IDE like any other property:
Here's the Button class:
public class MyButton : Button
{
private Label _Label = null;
public Label Label
{
get { return _Label; }
set { _Label = value; }
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
if (this.Label != null)
{
this.Label.Text = this.Name;
}
}
}
Here's the Label after I clicked the Button:
I have a UserControl that has someother controls:
I need to enable click on any item I click of the user control so I can set the UserControl borderstyle.
This works if I don't have any control added, but If I have for example a panel and I try to click on the panel my UserControl's click event doesn't get fired.
This is my code:
public partial class TestControl : UserControl
{
public TestControl()
{
InitializeComponent();
this.Click += Item_Click;
IsSelected = false;
}
public bool IsSelected { get; set; }
void Item_Click(object sender, EventArgs e)
{
if (!IsSelected)
{
IsSelected = true;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
}
else
{
IsSelected = false;
this.BorderStyle = System.Windows.Forms.BorderStyle.None;
}
}
}
Any clue on how to fire my UserControl click's event even if I click over other elements?
Actually it's really simple to achieve this, you can iterate through all the controls contained in your UserControl and register the Item_Click to their EventHandler which will invoke it when the Click event is fired:
public partial class TestControl : UserControl {
public TestControl( ) {
//...
for ( int i = 0; i < Controls.Count; i++ ) {
Controls[ i ].Click += Item_Click;
}
}
//...
}
I'm making a Windows Form Application called FMP.
I've got a class called Form1, a class called Panels.
Then I use inheritance to make different Panels with different properties.
The reason for doing this is because the teacher doesn't want us to initialize all the panels in the Form-class.
But I'm not sure how to do this. Found some things here #Stackoverflow, but they couldn't help me either.
The Size, The Location and the color are for all the Panels the same. (By clicking on a button, an other panel will appear ;) )
But the Name, Controls on the panel, and BackgroundImages are different. The controls are the most important aspect here.
The question is:
The Width and the Height should be equal to the Widht and the Height from the Form.
What is best in programming C#? To set the Width and the Height from Panels in the Form1 (but i made them protected) or declarate the form in the Panels class and use
Form1.Width?
The code I'm having right know:
The Form1
public Form1()
{
InitializeComponent();
buttonsProperties();
panelsProperties();
}
private void button1_Click(object sender, EventArgs e)
{
panelsChanged(1);
}
private void button2_Click(object sender, EventArgs e)
{
panelsChanged(2);
}
private void panelsChanged(int panelNr)
{
if (panelNr == 1)
{
panel1.Visible = true;
panel1.Enabled = true;
panel2.Visible = false;
panel2.Enabled = false;
}
else if (panelNr == 2)
{
panel1.Visible = false;
panel1.Enabled = false;
panel2.Visible = true;
panel2.Enabled = true;
}
}
The Panels
class Panels
{
Form1 f = new Form1();
//Color Property
protected Color color { get; set; }
//Size
protected Int32 Width { get; set; }
protected Int32 Height{ get; set; }
//Location
protected Point Location { get; set; }
public Panels()
{
initMembers();
}
private void initMembers()
{
this.Width = f.Width;
this.Height = f.Height;
this.Location = new Point(0, 0);
}
}
public class Panel1 : Panels
{
//Nothing yet.
}
Using the name Panels as a base class for each panel is confusing:
The name should not be in a plural form, as each instance of the class clearly resembles one "panel" (a panel has a Width, multiple panels do not have (one) width)
Since you're crating a WinForms application, the name looks too much like the System.Windows.Forms.Panel class
If I were you I'd let your base class derive from System.Windows.Forms.Panel:
abstract class MyPanelBase : Panel
{
public MyPanelBase()
{
Dock = DockStyle.Fill;
}
}
class MyPanel1 : MyPanelBase
{
}
This way you automatically get the behavior (and properties) of the Panel and it allows you to add it to a parent control (in your case, the form).
If all functionality you want is already supported by Panel you could even skip the MyPanelBase bit and let MyPanel1 derive directly from Panel.
I have a custom TabPage class:
class CustomTabPage : TabPage
{
TextBox tbCode = new TextBox();
public CustomTabPage()
{
}
public CustomTabPage(string title)
{
tbCode.Multiline = true;
tbCode.Size = this.Size;
}
//Something like this...
private void OnThisControlSizeChanged()
{
tbCode.Size = this.Size;
}
}
What I need for this class is to raise the OnSizeChanged event of the TabPage control when I resize it from where I create the control. The reason for this is when I resize the TabPage control, I want to adapt the TextBox size accordingly so that they stay the same.
class CustomTabPage : TabPage
{
TextBox tbCode = new TextBox();
public CustomTabPage()
{
SizeChanged += CustomTabPage_SizeChanged;
}
void CustomTabPage_SizeChanged(object sender, EventArgs e)
{
OnThisControlSizeChanged();
}
public CustomTabPage(string title)
{
tbCode.Multiline = true;
tbCode.Size = this.Size;
}
private void OnThisControlSizeChanged()
{
tbCode.Size = this.Size;
}
}
this.OnSizeChanged(new EventArgs());
I have created a tooltip. This tooltip is shown when mouse is over an icon by calling the show method of the tooltip. I want to know if this tooltip is currently being displayed. How to do this? Maybe through reflection?
System.Reflection.FieldInfo fi = typeof(ToolTip).GetField("window", BindingFlags.NonPublic | BindingFlags.Instance);
fi.GetValue(someObject...) ...
and then request maybe if visible?
The ToolTip class raises its Popup event just before it begins to display the tooltip. You can consider this the start of the time span during which the TT is displayed. The end of that span is the first of two things; a MouseLeave event on the control for which the ToolTip was showing, indicating the user is no longer pointing the mouse at whatever you had been showing a ToolTip for, or the passage of the AutoPopDelay time period of the ToolTip after which the balloon will fade out.
So, you can handle this with code in your Form or other control containing the ToolTip, looking something like this:
private System.Windows.Forms.Timer ToolTipTimer = new Timer();
public MyControl()
{
myToolTip.Popup += ToolTipPopup;
ToolTipTimer.Tick += ToolTipTimerTick;
ToolTipTimer.Enabled = false;
}
private bool IsToolTipShowing { get; set; }
private Control ToolTipControl { get; set; }
private void ToolTipPopup(object sender, PopupEventArgs e)
{
var control = e.AssociatedControl;
//optionally check to see if we're interested in watching this control's ToolTip
ToolTipControl = control;
ToolTipControl.MouseLeave += ToolTipMouseLeave;
ToolTipAutoPopTimer.Interval = myToolTip.AutoPopDelay;
ToolTipTimer.Start();
IsToolTipShowing = true;
}
//now one of these two should happen to stop the ToolTip showing on the currently-watched control
public void ToolTipTimerTick(object sender, EventArgs e)
{
StopToolTip();
}
public void ToolTipMouseLeave(object sender, EventArgs e)
{
StopTimer();
}
private void StopTimer()
{
IsToolTipShowing = false;
ToolTipTimer.Stop();
ToolTipControl.MouseLeave -= ToolTipMouseLeave;
}