I'm just creating a ContextMenu..
At this line, I don't know what I shall put in the third param (or better: how I have to form it -syntaxly-):
(contextMenuStrip.Items[0] as System.Windows.Forms.ToolStripMenuItem).DropDownItems.Add(contextUnterMenuStrip.Items.Add(exe),null, HERE);
on 'HERE' I have to set an EventHandler onClick
By Example I got this Method:
public void DoSomething()
{
//...
}
How could I call this Method? (Over the Eventhandler?) or do I have to make a Method like:
private void button_Click(object sender, RoutedEventArgs e)
{
//...
}
Don't "call" the method but take its address. Which means omitting the ()
private void menuItem1_Click(object sender, EventArgs e)
{
//...
}
// your code, I think it misses a few ')'
... (contextMenuStrip.Items[0] as System.Windows.Forms.ToolStripMenuItem)
.DropDownItems.Add(contextUnterMenuStrip.Items
.Add(exe),null, menuItem1_Click);
As you can see here, the callback has to have the following prototype:
public delegate void EventHandler( Object sender, EventArgs e )
So your method DoSomething has to look like:
private void DoSomething(object sender, EventArgs e)
{
//...
}
You can create an anonymous event handler using the Linq libraries and call your method that way. This can be a nice and quick way of doing something (especially if it's just a test project). But if you start using it extensively, it might become difficult to read it.
An example of this would be:
var menuItem1 = new MenuItem();
menuItem1.Click += (sender, e) => DoSomething();
Refer here for further information on using Linq: http://msdn.microsoft.com/library/bb308959.aspx
Related
Lets assume I have this code
private void Exitbtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
And I want to call this function from another part of my code and not only from the click of the user
code...
{
...do something...
Exitbtn_Click()
}
I have tried Exitbtn.Click +=new EventHandler(Exitbtn_Click); but it doesn't work i believe it is because of the word new but I am not sure, I am new to C#
Whilst you could call:
Exitbtn_Click(null, EventArgs.Empty);
If you actually need sender / EventArgs to be populated it could become messy.
Personally it seems cleaner to wrap the common logic in a private method and call from both places.
private void ApplicationExit()
{
Application.Exit();
}
private void Exitbtn_Click(object sender, EventArgs e)
{
ApplicationExit();
}
code...
{
...do something...
ApplicationExit();
}
As john said in the comments:
Exitbtn_Click(null, EventArgs.Empty);
I am trying to call a OnCellEditEnding event from another event,
private void BillsTableRecords_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
// do stuff here
}
My issue is I don't know how to pass the DataGridCellEditEndingEventArgs into the method, i.e. the e in the below method obviously gives an error as it is referencing RoutedEventArgs not DataGridCellEditEndingEventArgs.
private void BillsRecordsCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
BillsTableRecords_OnCellEditEnding(sender, e);
}
So how do obtain the value from DataGridCellEditEndingEventArgs so that I can pass the value in the method? Please note that the DataGrid cell with be selected at this point so it will contain a value.
I wouldn't recommend this approach. Event handlers are to be called by events; their signature does not really fit for a standalone call. In case you execute business code in your event handler, it is also not good design, because your event handlers are UI code, which should be separated from business code.
The best way to go here is to create a dedicated method that does what you want and call it from both event handlers:
private void DoStuff(/* add the parameters you need*/) {
//do stuff
}
private void BillsTableRecords_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DoStuff();
}
private void BillsRecordsCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
DoStuff();
}
try that
private void BillsRecordsCheckBox_OnChecked(object sender, RoutedEventArgs e)
{
BillsTableRecords_OnCellEditEnding(sender, new DataGridCellEditEndingEventArg());
}
If you want to keep the arguments from the RoutedEventArgs, add them to the constructor of DataGridCellEditEndingEventArg
I need to add an element to a Winform ListView control from other thread, so I am using a delegate, this way:
private delegate void AddMessageLogCallback(string message);
public void AddMessageLog(string message)
{
if (InvokeRequired)
Invoke(new AddMessageLogCallback(AddMessageLog), message);
else
{
lstLogs.Items.Add(message).EnsureVisible();
}
}
The problem is that the Invoke does nothing, not even throws an exception.
I have used this kind of delegates before and never had problems. What different is at this time?
Your code works as desired with the test code below, so the problem should be something else.
private void button1_Click(object sender, EventArgs e)
{
AddMessageLog("local message");
}
private async void button2_Click(object sender, EventArgs e)
{
await Task.Run(() => AddMessageLog("async message"));
}
Btw, I would mention that there is no need to define a new AddMessageLogCallback delegate and to call the AddMessageLog recursively. So a more simple (and maybe cleaner) solution:
public void AddMessageLog(string message)
{
Action addLog = () => lstLogs.Items.Add(message).EnsureVisible();
if (InvokeRequired)
Invoke(addLog);
else
addLog();
}
I want to run a method inside serialport_DataReceived event.
public void Draw(byte[] data);
private void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DrawingAudioData(data)));
}
This is not work. It gives an error that say "Method name expected". What can i do?
Try
public delegate void Draw(byte[] data);
private void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
this.Invoke(new Draw(DrawingAudioData), data);
}
Seems to me that the DrawingAudioData passed to Invoke does not have EventHandler signature. Also you should pass the method Name to the delegate constructor.
The DrawingAudioData method should have the signature that matches the Draw delegate:
public void DrawingAudioData(byte[] data) {
More information about Event Handler here.
More information about the Delegate and Invoke method here.
I am trying to figure out how to make it that when my timer ticks, it performs a bidder00_TextChanged, or something like that.
Is this even possible to do? and if it isn't, is there any other way to do it?
I tried to search Google for it but i didn't get any results, if you find anything that i missed please post it here.
I don't really have any code but here it is:
private void bidder00_TextChanged(object sender, EventArgs e)
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
That is my TextChanged Event
My timer doesn't have any code because it is going to perform the bidder00_TextChanged Event.
You could create a method Perform() and call it from within your event handlers :
private void timer1_Tick(object sender, EventArgs e)
{
Perform();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
Perform();
}
private void Perform()
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
I assume you have coupled your actual logic with your click event which is not a good idea. Separate the code out into a separate function and have both parts of the application call the same code e.g.
private void SubmitBid()
{
// code you want to execute
}
private void OnSubmitBid()
{
// confirm whether we can actually submit the bid
if (bidder00.Text == addbidder1.Text)
{
SubmitBid();
}
}
private void Timer1_OnTick(object sender, EventArgs e)
{
// trigger code from timer
OnSubmitBid();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
// trigger code from text change
OnSubmitBid();
}
private void btnBid_Click(object sender, EventArgs e)
{
// trigger code from button press
OnSubmitBid();
}
Notice all the UI controls trigger the same code. There is an extra call in there for the text control validation (i.e. OnSubmitBid()) - if this wasn't required then you would just call SubmitBid directly.