[DefaultEvent("MyCustomEvent")]
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public delegate void onButtonClick(object sender, Myeventargs e);
public event onButtonClick btnHandler;
protected void ASPxButton1_Click(object sender, EventArgs e)
{
if (btnHandler != null)
{
btnHandler(this,e);
}
}
}
this code can work with
public partial class jscript : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XFButton1.MYClick += new EventHandler(XFButton1_MYClick);
}
void XFButton1_MYClick(object sender, EventArgs e)
{
ASPxLabel1.Text = "Hello";
}
but the problem is i want it automatically generate a new event when i double click on the button. can i done this by using a web user control ?
too bad to tell you that web user control may not able to do like that... pls read this http://dotnet-framework-aspnet.itgroups.info/32/9/thread-796040.html.
Related
In a project created in Windows 7 and Visual Studio 2012, I have a form with the following code:
public partial class Form1 : Form
{
private Form2 m_form2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
m_form2 = new Form2();
m_form2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Refreshing form1");
Refresh();
m_form2.Refresh();
}
}
Form2 has the following method:
public override void Refresh()
{
MessageBox.Show("Refreshing Form2");
base.Refresh();
}
Form2 contains an instance of the RefreshTestControl, which contains this method:
public override void Refresh()
{
MessageBox.Show("Control is being refreshed.");
base.Refresh();
}
I expected that my control's overridden Refresh() method would get called automatically when Form2.Refresh() is called, but it's not. Why not? Am I doing something wrong, or do I just not understand what happens when a form is refreshed?
I'm trying to build a GUI for my application. I'm stuck on one problem. I try to use timer "OnTImedEvent" to call my function to update the chart. Unfortunately, MVS gives me a "Cross-thread operation not valid". I've came across some tips, regarding something called delegate but I can't manage to get it working. Since I'm nooby in windowsFormsApp (started today) I'm asking for your help. Here's my code:
using System;
using System.IO.Ports;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
bool state = false;
private static System.Timers.Timer timer;
public Form1()
{
InitializeComponent();
timer = new System.Timers.Timer(10);
timer.Elapsed += OnTimedEvent;
timer.AutoReset = true;
timer.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("a\n");
}
else
MessageBox.Show("Brak połączenia z urządzeniem");
}
private void button4_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
Form2 form2 = new Form2(serialPort1);
form2.Show();
}
else
MessageBox.Show("Brak połączenia z urządzeniem");
}
private void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
comBoxPort.Items.AddRange(ports);
}
private void btnCon_Click(object sender, EventArgs e)
{
if (state)
{
state = false;
btnCon.ForeColor = Color.Red;
try
{
serialPort1.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
else
{
state = true;
btnCon.ForeColor = Color.Green;
try
{
serialPort1.PortName = comBoxPort.Text;
serialPort1.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
}
}
private void btnRoll_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("s\n");
}
else
MessageBox.Show("Brak połączenia z urządzeniem");
}
private void btnYAW_Click(object sender, EventArgs e)
{
drawChart();
if (serialPort1.IsOpen)
{
serialPort1.WriteLine("d\n");
}
//else
//MessageBox.Show("Brak połączenia z urządzeniem");
}
private void drawChart()
{
chart1.Series["PITCH"].Points.AddY(21);
chart1.Series["ROLL"].Points.AddY(122);
chart1.Series["YAW"].Points.AddY(13);
}
public delegate void drawChartCallback();
private void chart1_Click(object sender, EventArgs e)
{
}
private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
drawChart();
}
}
}
Thanks in advance for your help.
While its not the best way to do it, for a beginner the easist way to avoid the problem is to use the following variable form.CheckForIllegalCrossThreadCalls = false;
Before the user is allowed to save an Excel workbook that is "invalid", I would like to inform them that they have managed to damage it and give them the opportunity to abandon all modifications.
By pressing a few times the [TAB] key, I was able to generate most of the code below:
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WithBeforeSave
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
}
void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
MessageBox.Show("How do I put the proper dialog here?");
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
}
Therefore, all I need is the C# equivalent to this:
This example prompts the user for a yes or no response before saving the workbook.
TIA
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WithBeforeSave
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.WorkbookBeforeSave += new Excel.AppEvents_WorkbookBeforeSaveEventHandler(Application_WorkbookBeforeSave);
}
void Application_WorkbookBeforeSave(Excel.Workbook Wb, bool SaveAsUI, ref bool Cancel)
{
if (DialogResult.No == MessageBox.Show("Are you sure you want to save the DEFECTIVE, INVALID workbook?", "Example", MessageBoxButtons.YesNo))
{
Cancel = true;
MessageBox.Show("Save is canceled.");
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
}
}
That's it. I found the answer here:
This example prompts the user for a yes or no response before saving any workbook.
I have a public method in project and here is that method.
public bool SaveDocument(string customer)
{
//some codes
}
I have another project in same solution with this method
private void buttonSaveReport_Click(object sender, EventArgs e)
{
}
button click event I need to pass 3 parameters (name,title,isbn) to SaveDocument() method and override it.I am having a difficulty with implementing this.Trying with this and can anyone help me out to complete it.
public class Ext : DocumentManager.DocumentManager
{
public override void SaveDocument()
{
base.SaveDocumentCustomer(customer);
}
}
Thanks
public event EventHandler MyButtonClick = delegate { };
The construction above allows to not check if there is any subscriber:
public virtual void OnMyButtonClick(EventHandler e)
{
this.MyButtonClick(this, e);
}
in stead of
public virtual void OnMyButtonClick(EventHandler e)
{
if (MyButtonClick!=null)
this.MyButtonClick(this, e);
}
But is it really a good idea? Is this the only benefit: to not check if any subscriber exists?
UPDATE: Here is example
namespace ConsoleApplication2
{
public class TestClass
{
public event EventHandler MyButtonClick;
//= delegate { };
public void OnButtonClick(EventArgs e)
{
MyButtonClick(this, e);
}
}
class Program
{
static void Main(string[] args)
{
var testClass = new TestClass();
//it throws an exception
testClass.OnButtonClick(new EventArgs());
// if you add an handler it will call it
testClass.MyButtonClick += myCustomHandler;
testClass.OnButtonClick(new EventArgs()); // myCustomHandler has been invoiked
}
private static void myCustomHandler(object sender, EventArgs e)
{
Console.WriteLine("myCustomHandler has been invoiked");
}
}
}
Well, the code you've given here:
public virtual void OnMyButtonClick(EventHandler e)
{
if (MyButtonClick!=null)
this.MyButtonClick(this, e);
}
isn't thread-safe. If the final subscription is removed after the nullity check but before the invocation, you could end up with a NullReferenceException (depending on whether the "raising" thread sees the change).
So you can change it to this instead:
public virtual void OnMyButtonClick(EventArgs e)
{
var handler = MyButtonClick;
if (handler != null)
{
handler(this, e);
}
}
... but of course you might forget to do that, and even if you don't, it's cumbersome to do that all over the place, IMO. So yes, while the benefit is "only" to avoid the nullity check, I'd say that's not a bad trade-off in many cases. Anything that makes it harder to make mistakes is a good idea, IMO.
Another alternative is to have an extension method:
public static void SafeInvoke(this EventHandler handler, object sender,
EventArgs e)
{
if (handler != null)
{
handler(sender, e);
}
}
Then change your calling code to:
public virtual void OnMyButtonClick(EventArgs e)
{
MyButtonClick.SafeInvoke(this, e);
}
(and use the same code for other events). You'd probably want a generic form for EventHandler<T> as well.
you don't need to do that. If the client that uses you class won't add an handler (subscriber) for MyButtonClick event the code won't throw an exception.
That is how events works (and delegates as there are the same thing) otherwise you would be forced to add an handler to all the events of a class (assuming there are any)
so you can do the below:
public virtual void OnMyButtonClick(EventArgs e)
{
MyButtonClick(this, e);
}
have a look at the example below:
public class TestClass
{
public event EventHandler MyButtonClick = delegate { };
public void ButtonClick(EventArgs e)
{
MyButtonClick(this,e);
}
}
class Program
{
static void Main(string[] args)
{
var testClass=new TestClass();
testClass.ButtonClick(new EventArgs());
// if you add an handler it will call it
testClass.MyButtonClick += myCustomHandler;
testClass.ButtonClick(new EventArgs()); // myCustomHandler has been invoiked
}
private static void myCustomHandler(object sender, EventArgs e)
{
Console.WriteLine("myCustomHandler has been invoiked");
}
}