No overload for 'My Void' matches delegate 'My Delegate'? - c#

I have a user control with one custom event. This is in a Windows Phone app if it matters. The event in the user control's C# looks like this:
public delegate void TextChangedVoid(string newText)
public event TextChangedVoid TextChanged;
I then add this to my Window's XAML:
<local:ClientList TextChanged="clist_TextChanged"...
And in the Page's C#:
private void clist_TextChanged(string newText)
{
...
}
It looks fine, but every time I build my project I get this error:
No overload for 'clist_TextChanged' matches delegate 'TextChangedVoid'.
Why is this happening? Should I be using a class other than string in my event?

It turned out that I should have been using a class besides string. I created a class inheriting from EventArgs to use to hold the string variable. This fixed the problem. It looked like:
public partial class ClientChangedEventArgs : EventArgs
{
public ClientChangedEventArgs(string mewText)
{
this.NewText = newText;
}
public string NewText;
}
And in my User Control:
public delegate void TextChangedVoid(ClientChangedEventArgs e)
public event TextChangedVoid TextChanged;

Related

Define custom path for control events

I've been working a lot with WPF, and after awhile the MainWindow class becomes cluttered and unorganized. Is there a way to store all of the control events in a custom class like below? Inheriting doesn't work and i'm guessing its because it has no instance of the new class to go off of.
public partial class MainWindow : Window
{
public class ControlEvents : MainWindow //Custom class
{
private void Abutton_Click(object sender, RoutedEventArgs e)
{
...Stuff
}
}
}
Is there a way to store all of the control events in a custom class like below?
No, the event handlers themselves must be defined in the code-behind of the same view class where the element is defined and the handler is hooked up.
You could move the code inside the event handlers to another class though:
public partial class MainWindow : Window
{
private YourClass _handler = new YourObject();
public class ControlEvents : MainWindow //Custom class
{
private void Abutton_Click(object sender, RoutedEventArgs e)
{
_handler.HandleButtonClick(e);
}
}
}
But you should look into MVVM: https://msdn.microsoft.com/en-us/library/hh848246.aspx. There is a reason why this is the recommended design pattern for developing XAML based UI applications.
If you don't use mvvm:
You can create user control for area of controls and load this user control in your main window.
Also - you can take your code of "do stuff" to another class and call it from the event function.
for example:
functions.cs
dostuff1()
{
...
}
dostuff2()
{
...
}
your usercontrol/mainwindow.xaml.cs:
functions f = new functions();
private void Abutton_Click(object sender, RoutedEventArgs e)
{
f.dostuff1();
}
good luck
You can move all the events to Partial class in separated file.
call the file MainWindowEvents.cs or something. (to remember what is it)

How to move methods to external file in windows form C#

newbie question :(
I'm making a program using windows forms and i have a lot of small methods like this
private void label1_Click(object sender, EventArgs e)
{
textBox1.Select();
}
private void label13_Click(object sender, EventArgs e)
{
textBox13.Select();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
plotGraph(prostokat);
}
in the Form1.cs file and to make the code more transparent, I would like to move these small methods out somewhere to an external file (class?) but I don't really know how to do this. If they were normal methods I would just make a class and create an object of that class and just call the methods using that object but these are functions that "happen" when a user action is performed i.e. a textbox is clicked, so I'm not sure how to make this work.
It is possible to create an extra partial class (separated file) for your Form1 and place your cluttering methods there.
Or you could collapse them with #region
#region UI Handlers
#endregion
The perfect solution would be using some kind of MVVM for WinForms. In that case in your ViewModel you can implement your business logic separately from the code-behind.
Check out this:
https://www.codeproject.com/articles/364485/mvvm-model-view-viewmodel-patte
Hope it helps!
Have a look at your Form class subsection. It most cases it is still a partial class. Create a new .cs file in the same subsection in your project and add another partial form class to it.
You can find additional information here:
Partial Classes and Methods
Sure, you can add a new class to your project (right-click the project in Solution Explorer --> Add Class --> ) and put your methods there. Then you will need to hook the methods up to the controls in code:
I added a static class called "Form Methods" and put a method in there for label1 Click event:
static class FormMethods
{
public static void label1_Click(object sender, EventArgs e)
{
Label label = (Label) sender;
// Try to find the textbox through the label's parent form
TextBox textBox = (TextBox) label.Parent.Controls.Find("textBox1", true).First();
if (textBox != null)
{
textBox.Select();
}
}
}
Then in the Form Designer code, you can hook up the event:
this.label1.Click += new System.EventHandler(FormMethods.label1_Click);
Alternatively, you can make the class part of your original form class, and it will still be a separate file. If you want to do this, you can then make your event a private non-static method, and you would change the class definition to a public partial class:
public partial class Form1 // <-- This used to be: static class FormMethods
{
// This used to be: public static void label1_Click
private void label1_Click(object sender, EventArgs e)
{
. . .
And then hooking up the event looks like:
this.label1.Click += new System.EventHandler(label1_Click);
You can create any number of partial class files mimicking your original and group methods inside as your functionally needs - however, you won't be able to use the designer to directly navigate to your callbacks. That is, if you double click a graphic element or click an event of a graphic element you will have an unexpected behavior: in both cases you will have an event handler generated in your first partial and a hook created to that . . . so you can't directly navigate to those handlers anymore, and you need to go trough your partial files looking for their definitions.
Use partial to split C# code like this.
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}

Firing events within a plug-in architecture vs single application

I am getting stumped with my plug-in architecture that I am trying to develop with respect to events. I can do this just fine, in a single application: (Obviously this is a very simplified version of what I am trying to accomplish, and if this were my code there are easier ways to accomplish this, just try to follow the logic ;)
public Form1()
{
public event EventHandler OnValueOver5Chars;
Main()
{
OnValueOver5Chars+= new EventHandler(WarnUser);
....
}
private void textBox_Changed( object sender, EventArgs e )
{
if( sender.Text.count() > 5 )
OnValueOver5Chars(sender, e); // WORKS HERE
}
private void WarnUser(object sender, EventArgs e)
{
...
}
}
However, now I have a plug-in architecture, where the plugin implements an interface which houses my event:
// Interface.cs
public interface IPlugin
{
event EventHandler OnValueOver5Chars;
...
}
// Plugin1.cs
public class Plugin1 : IPlugin
{
public event EventHandler OnValueOver5Chars;
Plugin1()
{
OnValueOver5Chars += new EventHandler(Plugin1WarnUser);
}
private void Plugin1WarnUser(object sender, EventArgs e)
{
...
}
}
// Form.cs
public class Form1 : Form
{
public Form1()
{
Assembly SampleAssembly = Assembly.LoadFrom("Plugin1.dll");
Type myType = SampleAssembly.GetTypes()[0];
if (myType.GetInterfaces().Contains(typeof(IPlugin)))
{
IPlugin myInstance = Activator.CreateInstance(myType) as IPlugin;
myInstance.OnValueOver5Chars(this, new EventArgs());
// Compiler Error CS0079 - The event 'event' can only appear on the left hand side of += or -=
}
}
????
You're trying to hook up an event to another event, and that won't work. You need to hook the plug-ins event to a method/delegate. Once you do that, have the method/delegate call the other event.
myInstance.OnValueOver5Chars += OnValueOver5CharsFunc;
...
/*In Form1*/
void OnValueOver5CharsFunc( object sender, EventArgs args )
{
OnValueOver5Chars( sender, args );
}
Events in C# have the property that they are not "callable" directly as methods (or as such as delegates) outside of the class where they are defined.
In your first example you are calling the event from within the class in which you define it. In the second example, however, you are trying to call OnValueOver5Chars from outside the class - hence the error.
To solve this you could consider adding a method to your IPlugin interface (e.g. ValueOver5Chars) that performs OnValueOver5Chars. Note that it is more common to name the event ValueOver5Chars (say), and provide a method OnValueOver5Chars to raise it (i.e. the other way round). See for example the Windows Forms Button class and its Click event.

how can I implement a user control's method outside user control?

Say I have a user control(SubmitButton) having a submit button that when a user clicked on, I want the control, which contains a SubmitButton instance, decide the behavior of submit button.
I have tried the following in the user control .cs file:
protected void nextPage_Click(object sender, EventArgs e) {
submitData();
Response.Redirect("completed.aspx");
}
protected abstract void submitData();
But I don't know where the submitData method should be implemented.
It's like a place holder for method.
Your control should expose event. For example SubmitClicked. Than in control that contains it You subscribe to that event and do whatever You choose to do. If you have event exposed You can attach to it as many event handlers as you like.
That's what the asp:Button already does. It exposes Click event and in aspx page You just subscribe to that event and implement event handler in Your code behind file.
This will be an abstract class. You can never create an instance of these, you must create your own class that inherits from it and implement the abstract methods and properties.
Eg:
public class MySubmitButton : SubmitButton {
protected override void submitData() {
// do somthing
}
}
Try something like a function delegate:
using System;
namespace ConsoleApplication1
{
public delegate void FunctionToBeCalled();
public class A
{
public FunctionToBeCalled Function;
public void Test()
{
Function();
Console.WriteLine("test 2");
}
}
class Program
{
static void Main(string[] args)
{
A instanceA = new A();
instanceA.Function = TheFunction;
instanceA.Test();
}
static void TheFunction()
{
Console.WriteLine("test 1");
}
}
}

How can I update a TextBox from a class using events?

I would like to update a form's textbox in winforms with data that is being processed from a class file.
Example:
Class.RunProcess();
Inside of RunProcess I would like to fire an event that will update the textbox with the text that I pass in on the win form.
I know you can do this with events so I don't have to make the textbox public or pass it into the many methods I have, I am just not quite sure how to start.
Edit:
Maybe I am just not clear.
I have a windows form with a text box on it.
I have a button that runs a process.
The process is in a class file outside of the form class.
Inside this process method I would like to be able to update the text box on the main form without having to pass the TextBox as a parameter to all the methods that I will have (I currently have 6 and will be more).
I thought I could subscribe to an event that would allow me to display a message in the text box while the processes ran in my class.
It's probably time to consider:
Databinding your textbox to your class
Model/View/Presenter (MVP)
For #1 (Databinding), you can use the INotifyPropertyChanged interface on your class and raise the event for changed properties.
public class BusinessModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int _quantity;
public int Quantity
{
get { return _quantity; }
set
{
_quantity = value;
this.OnPropertyChanged("Quantity");
}
}
void OnPropertyChanged(string PropertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
And in your form, youcan bind the .Text property of the textBox to the object in several ways. Through the UI or in code.
Links:
Bind Better with INotifyPropertyChanged
How to: Implement the INotifyPropertyChanged Interface
Now, if you need to add to text that already exists such as in your example, you can either track the full text in the class or raise events from your class and respond in the form code. Tracking it in the class would be better - you really don't want any logic in the form at all, which brings us back to binding and/or some form of MVP/MVC.
If you're wanting Class.RunProcess to run when an event from an external class is fired then you can do it like this
public delegate void MyDelegate();
public class MyClass
{
public event MyDelegate Myevent;
public void DoSomething()
{
this.Myevent();
}
}
public static class Class
{
public static void RunProcess()
{
txtData.Text += "Message";
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MyClass myClass = new MyClass();
myClass.Myevent += Class.RunProcess;
}
}
You could add a parameter to your constructor that is a TextBox and save the reference in the class as a private variable and call that in each method.

Categories

Resources