c# referencing a label from another method - c#

I want if mouse leave the visible status change to false,but I get this error message:
Error CS7036 There is no argument given that corresponds to the required formal parameter 'e' of 'Form1.Repair_MouseLeave(object, EventArgs, Label)'
How shoudl I fix it?
private void Repair_MouseHover(object sender, EventArgs e)
{
Label RepairText = new Label();
RepairText = new Label();
RepairText.Location = new Point(161, 12);
RepairText.Text = "This what the program will do";
this.Controls.Add(RepairText);
RepairText.AutoSize = true;
Repair_MouseLeave(RepairText);
}
private void Repair_MouseLeave(object sender, EventArgs e,Label repairtext)
{
repairtext.Visible = false;
}

First of all, we need to set our event handlers for the MouseHover and MouseLeave methods for the Repair control. I am assuming you know how to do this. Still,
binding to the events of Repair control can be achieved using the Properties window of your Form in design mode. Set event handlers to both MouseHover and MouseLeave.
As far as I can see, you are trying to display a label with some text when the mouse is hovering over this Repair control and want to hide it when the mouse leaves it. But you are handling it incorrectly. First of all, calling MouseLeave from inside MouseHover would immediately hide your new label and it would not be displayed at all.
And your method signature for Repair_MouseLeave is also incorrect. A standard event handler takes two parameters: (object sender, EventArgs e)
Implement your event handlers like the following, having the new label repairText as an instance member of your class:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Label repairText;
private void Repair_MouseHover(object sender, EventArgs e)
{
if(repairText == null)
{
repairText = new Label();
repairText.Location = new Point(161, 12);
repairText.Text = "This what the program will do";
repairText.AutoSize = true;
this.Controls.Add(repairText);
}
repairText.Visible = true;
}
private void Repair_MouseLeave(object sender, EventArgs e)
{
if(repairText != null)
{
repairText.Visible = false;
}
}
}

Related

custom tooltip in c# .net win forms

I am looking to simulate a custom tooltip the like of you see in websites using c# .NET 4.5 windows forms.This tooltip will basically show status of some Tasks like how many tasks are pending,tasks in process, completed etc.To do this i am using a borderless win form.This winform will have some texts, images etc.I want it to reveal itself on button's mouseHover event and disappear on MouseLeave event.My problem is that on Mousehover event numerous instances of that tooltip form is getting generated and on MouseLeave they are not getting closed.My code is
private void B_MouseHover(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Close();
}
My code is not working, hence please tell me how to do this the correct way.Thanks
You're generating a new instance of the form class every time you get a hover event, and every time you get a leave event. If you want to continue to use this approach I would recommend you use a variable on your main form object to store the reference to your tooltip form. Secondly, you need to not generate a new instance whenever the event handler is called, but only when necessary. I would create your instance the first time your Hover event is called for a particular control, and then dispose of it when your Leave handler is called -- this is under the assumption that the tooltip dialog's constructor loads up different information for each control being hovered over. Like so:
frmSecQStatToolTipDlg f_tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
f_tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(f_tooltip != null)
{
f_tooltip.Close();
f_tooltip = null;
}
}
You should keep a global field for this form, and should not dispose or close it. Just hide it on some events and show again.
Sample Code:
frmSecQStatToolTipDlg tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg != null)
{
tooltip.Hide();
}
}
With this logic you'll not have to create tooltip instance again and again and it will not take time to popup if you frequently do this activity.
Declare your tooltip once as readonly and use it without asking anytime if it is null or not.
If you need to Dispose it, implement the IDisposable pattern:
https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
private readonly frmSecQStatToolTipDlg _tooltip = new frmSecQStatToolTipDlg() ;
private void B_MouseHover(object sender, EventArgs e)
{
_tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
_tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
_tooltip.Hide();
}

Populate textbox in a window using another window

I have an application with two windows. One window contains textboxes while the other window is the keyboard. My problem now is that when I click on my Keyboard window I encounter error. I cannot type on the textbox I clicked on the other window. Here is my code.
When I click one textbox from window I, I trigger this Mouse event.
private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.Focus();
}
I used sender as TextBox because my textboxes are programmatically added to my window. I used it to get the name of the textbox.
And here is my code when I click a button from my Keyboard window.
Let used button 1:
private void button_numeric_1_Click(object sender, RoutedEventArgs e)
{
Screen1 screen1 = new Screen1();
screen1.textBox.Text += "1";
}
Screen1 is the window containing my textboxes.
How I possibly able to type text in my textbox using my created keyboard. I'm using C#. Anyone please help me out.
instead of using new Screen1(); you may need to use the actual instance of the screen, you may pass the same to the keyboard via constructor.
example
class Screen
{
Keyboard keyboard;
public Screen()
{
//pass the screen instance to the keyboard
keyboard = new Keyboard(this);
}
//...
private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.Focus();
//open keyboard etc
keyboard.Show();
}
}
class Keyboard
{
private Screen screenInstance;
public Keyboard(Screen instance)
{
//store the instance in a member variable
screenInstance = instance;
}
//...
private void button_numeric_1_Click(object sender, RoutedEventArgs e)
{
//use the stored screen instance
screenInstance.textBox.Text += "1";
}
public void Show()
{
//display logic etc
}
}
above is just an example based on some assumptions, you may adjust/merge with your code as needed.
you may adjust the same to pass TextBox instance if you have multiple TextBoxes to be used
example
class Screen
{
Keyboard keyboard = new Keyboard();
private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
textBox = sender as TextBox;
textBox.Focus();
//open keyboard etc
keyboard.Show(textBox);
}
}
class Keyboard
{
private TextBox textBoxInstance;
private void button_numeric_1_Click(object sender, RoutedEventArgs e)
{
//use the stored TextBox instance
textBoxInstance.Text += "1";
}
public void Show(TextBox instance)
{
textBoxInstance = instance;
//display logic etc
}
}

How to create RowHeadersVisible changed event for DataGridView?

I have a Windows Form, DataGridView and two buttons.
When I will press the button1 it changes a value of RowHeadersVisible to true.
When I will press the button2 it changes a value of RowHeadersVisible to false.
public Form1()
{
InitializeComponent();
dataGridView1.RowHeadersVisible = false;
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = true;
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = false;
}
I cannot find any kind of events about "RowHeadersVisible" value changing in "DataGridView" class. As I mentioned "CellFormatting" event works for this action but it appears often, almost for all kind of action made in datagridview1.
I think we might create a custom event handler in order to make different decisions.
When "RowHeadersVisible" changes the value to false I need to call another function inside "CustomEvent".
private void CustomEvent(object sender, EventArgs e)
{
SomeFunction();
}
On the other hand "DataGridTableStyle" class has the event "RowHeadersVisibleChanged".
So How to solve this problem?
.NET 4.5, you should get help.
Dissolve it in the following way.
Represents the table drawn by the System.Windows.Forms.DataGrid control at run time.
// Instantiate the EventHandler.
public void AttachRowHeaderVisibleChanged()
{
myDataGridTableStyle.RowHeadersVisibleChanged += new EventHandler (MyDelegateRowHeadersVisibleChanged);
}
// raise the event when RowHeadersVisible property is changed.
private void MyDelegateRowHeadersVisibleChanged(object sender, EventArgs e)
{
string myString = "'RowHeadersVisibleChanged' event raised, Row Headers are";
if (myDataGridTableStyle.RowHeadersVisible)
myString += " visible";
else
myString += " not visible";
MessageBox.Show(myString, "RowHeader information");
}
// raise the event when a button is clicked.
private void myButton_Click(object sender, System.EventArgs e)
{
if (myDataGridTableStyle.RowHeadersVisible)
myDataGridTableStyle.RowHeadersVisible = false;
else
myDataGridTableStyle.RowHeadersVisible = true;
}

How to connect text change event in multiple text boxes to a single method (C#)?

When I double click on my text boxes in the designed, it creates a method auto-magically for me. Since I wish the same things to occur in any of the cases, I simply call an auxiliary method from each, like in the code below.
private void TextBox_1_TextChanged(object sender, EventArgs e)
{
TextChanged();
}
private void TextBox_2_TextChanged(object sender, EventArgs e)
{
TextChanged();
}
private void TextChanged(object sender, EventArgs e) { ... }
Now I'd like to know if there's a way (other than going into my design file (which, according to the information in it, shouldn't be attempted to) to connect the actions listeners to the same method and skip the detour via the automatically generated ones.
On the designer page go to the events tab, find the event you are looking for (TextChanged) and manually enter the name of the event handler you wish them all to use.
I usually proceed like this in my projects, if controls are not going to change at runtime (i.e. if all controls in the form are added at design time):
// this is the container's ctor
public MyForm()
{
TextBox1.TextChanged += new EventHandler(UniqueHandler);
TextBox2.TextChanged += new EventHandler(UniqueHandler);
...
TextBoxN.TextChange += new EventHandler(UniqueHandler);
}
void UniqueHandler(object sender, EventArgs e)
{
TextBox source = (sender as TextBox);
// handle the event!
}
If controls will change, it's actually quite similar, it just doesn't happen in the ctor but on-site:
// anywhere in the code
TextBox addedAtRuntime = new TextBox();
addedAtRuntime.TextChanged += new EventHandler(UniqueHandler);
MyForm.Controls.Add(addedAtRuntime);
// code goes on, the new textbox will share the handler
In the properties fold-out (most often to the right of your screen) you should have a thunder icon. That's where all the events are referable.
If you don't see the properties, select the regarded component (the text box in your case), right-mouse it and pick "properties" in the context menu.
You can do it by this way:
void btn1_onchange(object sender, EventArgs e)
{
MessageBox.Show("Number One");
}
void btn1_onchange2(object sender, EventArgs e){
MessageBox.Show("Number Two");
}
public MyForm() {
Button btn1 = new Button();
btn1.Text = "Click Me";
this.Controls.Add(btn1);
btn1.TextChange += new EventHandler(btn1_onchange);
btn1.TextChange += new EventHandler(btn1_onchange2);
}
You could do it in designer view. Instead of double-clicking on an element - go to your buttons' properties, select events tab and then put a proper handler name for adequate event. Voila!
Follow these steps:
Go to the InitializeComponent().
There are three events attached to each text box.
There you shoud be able to see the following.
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
Replace this with
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox2.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
And then remove the method below
private void TextBox_2_TextChanged(object sender, EventArgs e)
{
TextChanged();
}

how to generate a SelectionRangeChanged Event Programatically ChartControl WinForms

want to create a selectionRangeChanged event programatically not really getting how to do it
private void btn_10D_Click(object sender, EventArgs e)
{
double varRange = 10;
double var_Sel1 = DatesX[0].ToOADate();
Chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
Chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionColor = Color.LightGray;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionStart = var_Sel1;
Chart1.ChartAreas["ChartArea1"].CursorX.SelectionEnd = varRange + var_Sel1;
Chart1.ChartAreas["ChartArea1"].CursorX.Position = varRange + var_Sel1;
Chart1.SelectionRangeChanged += new EventHandler<CursorEventArgs>(Chart1_SelectionRangeChanged);
}
void Chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
throw new NotImplementedException();
}
thank you
For all events in C# is true that if class creator did not make extra effort to allow event firing form outside of class it is impossible to fire them.
According to MSDN
Chart.SelectionRangeChanged event Occurs when the selection start position or end position is changed.
But from my tests I can see that it is fired only if it is changed by user not program.
If I understand your intention correctly you want to handle those small buttons under your chart and btn_10D_Click method is a click handler for one of them. Try to move this line
Chart1.SelectionRangeChanged += new EventHandler<CursorEventArgs>(Chart1_SelectionRangeChanged);
to your constructor and ensure it is called once (remove it form other handlers). This will ensure your code is executed when user changes selection. If you want to execute same code for your button you should simply extract handler contents to method and call it form button click handler.
void Chart1_SelectionRangeChanged(object sender, CursorEventArgs e)
{
DoSomething(/*some arguments if you need them*/);
}
private void btn_10D_Click(object sender, EventArgs e)
{
\\your code
DoSomething();
}

Categories

Resources