How can I call this event private void panel1_Paint(object sender, PaintEventArgs e){.....} from like button click function or something?
I've tried panel1.Paint += new PaintEventHandler(panel1_Paint); and couple more but they did not seem to work.
try this:
private void Button1_Click(object sender, EventArgs e){
panel1.Invalidate();
}
the Invalidate() Method force the control to repaint.
You need to use it as simple function:
...your_code...; panel1_Paint(null, null); ...your_code...
Related
I have a button on my C# Winform, and the following code:
button1.Click += button1_Click;
and also:
private static void button1_Click(object sender, EventArgs e)
{
// do something
}
I am trying to simplify and reduce the amount of code in my application. Is there any way to do this?
Here's what I am trying to achieve:
button1.Click += void button1_Click(object sender, EventArgs e)
{
// do something
};
This does not work. Is there any other way to achieve this?
You can do this with an anonymous method:
button1.Click += (sender, e) =>
{
// do something
};
But note that you will never be able to unregister this event handler as it is an anonymous method.
I have populated a datagrid but would like an event to fire when the user double clicks the row and highlights it. I have looked through all the properties but cannot seem to work out which one it is.
I've also looked on MSDN but also cannot see anything.
Many Thanks,
Sam
Try to use the code below:
this.dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellDoubleClick);
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
}
The problem with CellDoubleClick is it won't fire an event for keyboard users when navigating with arrows.
Instead, some other events you can handle are:
RowEnter
SelectionChanged
RowStateChanged
You can poke around when each fires by attaching handlers to each event and writing to the output window:
dataGridView1.RowEnter += dgv_RowEnter;
dataGridView1.SelectionChanged += dgv_SelectionChanged;
dataGridView1.RowStateChanged += dgv_RowStateChanged;
dataGridView1.CellContentClick += dgv_CellContentClick;
private void dgv_RowEnter(object sender, DataGridViewCellEventArgs e)
{
Debug.WriteLine("RowEnter");
}
private void dgv_SelectionChanged(object sender, EventArgs e)
{
Debug.WriteLine("SelectionChanged");
}
private void dgv_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
Debug.WriteLine("RowStateChanged");
}
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
Debug.WriteLine("CellContentClick");
}
private void DrawIt()
{
System.Drawing.Graphics graphics = this.CreateGraphics();
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
50, 50, 150, 150);
graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
}
private void Form1_Load(object sender, EventArgs e)
{
DrawIt();
}
private void button1_Click(object sender, EventArgs e)
{
DrawIt();
}
when putting the 'DrawIt' method in a button event it works, but in a form load event it doesn't, why?
Change Load event to Paint.
If you want redraw your form use this.Refresh();
When you are in Paint method use:
private void mForm_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(...);
}
The Load event is run before the Form is drawn. So anything you draw is overwritten by the Form.
Call your DrawIt method from an event which fires after the Form has loaded.
You need to an event to trigger DrawIt(). You can use a panel or sth else. Then write an OnClik event. Your drawing will start after a click.
if vb6 just add call to your sub or function in Form_Activate
Instead of using Form_Load event use Form_Shown event. That should work.
I have simple Windows form With just one button which Calls one url
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo sInfo = new ProcessStartInfo("http://myurl.com/");
Process.Start(sInfo);
}
When i click exe then it shows button and then i have to click button to perform action. Is it possible to call button click event automatically on clicking exe.
You can do the same with Form.Load event. Just refactor you code with extract method to have no duplicated code and call your function in both event handlers:
private void button1_Click(object sender, EventArgs e)
{
OpenUrl();
}
private void form1_Load(object sender, EventArgs e)
{
OpenUrl();
}
private void OpenUrl()
{
ProcessStartInfo sInfo = new ProcessStartInfo("http://myurl.com/");
Process.Start(sInfo);
}
Simply call:
button1_Click(this, null);
from anywhere in your code.
I have a label working as a button. I would like when I press a button the click event to this label to take action. for example
private void Label1_Click(object sender, EventArgs e)
{
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Now I want when I press this button, the label1 click event to be performed
private void button1_Click(object sender, EventArgs e)
{
// I want when I press this button something like this happens
Label1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e);
}
now if you want to show a message of which control was clicked all in one method do the following
private void label1_Click(object sender, EventArgs e)
{
Control control = (Control)sender;
var name = control.Name;
MessageBox.Show(string.Format("I pressed this {0} and showed me this messagebox",name));
}
Two ways to do this.
First:
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e); // Just call the Label's click handler
}
Second:
// Bind the Label1_Click handler to the button1 Click event, as they both use the same delegate
button1.Click += new EventHandler(Label1_Click);
With the second approach, note that in C# delegates are multi-cast, so both the button1_Click handler and the Label1_Click handler will be called when the button is clicked, in the order they were bound.
private void button1_Click(object sender, EventArgs e)
{
//What the label click do:
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Is that not easier?
Why do you want to do it ?
I think it would be easier for you to just include the lable click functionality with the button click. Maybe even separate each piece in their own method and call them from the button click. Here is how you'd call another click event.
private void button1_Click(object sender, EventArgs e)
{
label1_Click(sender, e);
}
public class MyLabel:Label
{
public void PerformClick()
{
OnClick(new EventArgs());//InvokeOnClick(this,new EventArgs());
}
}