Execute a Method - c#

Goal:
When you press on the menu button named "Delete" when you right click in a listView, the method named Test() should be executed.
Problem:
I cannot make it to be happened because I get a error messsage.
Error 1 No overload for 'Test' matches delegate
'System.Windows.RoutedEventHandler'
private void lvw_bokade_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
ListView selectedData = (ListView)sender;
Passlista myPasslista = (Passlista)selectedData.SelectedItem;
System.Windows.Point pt = e.GetPosition(this);
if (myPasslista != null && !(System.Windows.Media.VisualTreeHelper.HitTest(this, pt).VisualHit is ScrollViewer))
{
ContextMenu myContextMenu = new ContextMenu();
MenuItem menuItem2 = new MenuItem();
menuItem2.Header = "Delete";
myContextMenu.Items.Add(menuItem2);
menuItem2.Click += new RoutedEventHandler(Test);
myContextMenu.IsOpen = true;
}
}
private void Test()
{
MessageBox.Show("ssss");
}

Change the signature of your method to match the delegate of RoutedEventHandler:
private void Test(object sender, RoutedEventArgs e)
{
MessageBox.Show("ssss");
}
The signature of the delegate looks like this, according to MSDN:
public delegate void RoutedEventHandler(
Object sender,
RoutedEventArgs e
)

Your method needs to match this delegate:
public delegate void RoutedEventHandler(
Object sender,
RoutedEventArgs e
)
You need to change it to:
private void Test(object sender, RoutedEventArgs e)
{
MessageBox.Show("ssss");
}

change the signature of the method test to:
private void Test(object sender, RoutedEventArgs e)

Related

C# Button Object as a parameter

I'm trying to make a function with a parameter so whenever I call it, it will change the button's back color but my code doesn't work, any ideas
Button button = new Button();
private void Change(object sender)
{
if (button.Visible == true)
{
button.BackColor = Color.Red;
}
}
private void hr1_left2_btn_MouseEnter(object sender, EventArgs e)
{
Change(hr1_left2_btn);
}
Perhaps like this? I've changed the Sender Arg type of the Change function to ref a button object.
private void Change(Button sender) {
if (sender.Visible)
sender.BackColor = Color.Red;
}
private void hr1_left2_btn_MouseEnter(object sender, EventArgs e) {
Change(hr1_left2_btn);
}

Show TextBox on every TabPage

I attached a TextBox to the first TabPage of a TabControl. I would like to display the same TextBox object on every TabPage. I tried to add the control to the tabControl Collection but unfortunately it's not working.
private void Form1_Load(object sender, EventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}
Button b;
public Form1()
{
InitializeComponent();
b = new Button() { Text = "Prueba" };
}
private void Form1_Load(object sender, EventArgs e)
{
AddButtonToTabControl();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
AddButtonToTabControl();
}
public void AddButtonToTabControl()
{
tabControl1.SelectedTab.Controls.Add(b);
}
I missed two methods. It's working now!
tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(textBox);
}

How to execute the same code for different events

I have to work with touch monitors and sometimes with mouse and normal monitors.
So for drag and drop the for the first would be
private void lvAllowedPPtab2_StylusButtonDown(object sender, StylusButtonEventArgs e)
and for the second
private void ListBox_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
after that I have to execute the same code using sender and e.
I didn't get to make a common code routine.
The two event are similar and both have the GetPosition event.
I might have taken the wrong road but I have tought to something like:
Type eventType;
if (_e is StylusButtonEventArgs)
eventType = typeof (StylusButtonEventArgs);
else
eventType = typeof(MouseEventArgs);
but then I don't know how to cast e to event type.
Thank you
you can call them both with that
private void listView_StylusButtonDown(object sender, StylusButtonEventArgs e) { CommonCode(sender, e); }
private void listView_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e) { CommonCode(sender, e); }
and then tell inside common code
private void CommonCode(object sender, object _e)
{
//Sender is common
ListView parent = (ListView)sender;
string strListViewButtonName = (sender as ListView).Name;
if (_e is StylusButtonEventArgs)
... (_e as StylusButtonEventArgs).GetPosition(parent));
else
... (_e as MouseEventArgs).GetPosition(parent));
}
Better implementation (thanks to Eli Arbel):
private void listView_StylusButtonDown(object sender, StylusButtonEventArgs e) { CommonCode(sender, e.GetPosition((ListView)sender)); }
private void listView_PreviewMouseLeftButtonDown(object sender, MouseEventArgs e) { CommonCode(sender, e.GetPosition((ListView)sender)); }
private void CommonCode(object sender, Point p)
{
//Sender is common
ListView parent = (ListView)sender;
string strListViewButtonName = (sender as ListView).Name;
//you don't need getPosition since P is known
}

Dynamic method in c#

I have many labels on the form, and every label invokes same method with different argument(which belongs to label text/name). Here is the code:
//"res" is an array
private void label1_Click(object sender, EventArgs e)
{
checkresult(res[0]);
}
private void label2_Click(object sender, EventArgs e)
{
checkresult(res[1]);
}
private void label3_Click(object sender, EventArgs e)
{
checkresult(res[2]);
}
private void label4_Click(object sender, EventArgs e)
{
checkresult(res[3]);
}
private void label5_Click(object sender, EventArgs e)
{
checkresult(res[4]);
}
private void label6_Click(object sender, EventArgs e)
{
checkresult(res[5]);
}
private void label7_Click(object sender, EventArgs e)
{
checkresult(res[6]);
}
private void label8_Click(object sender, EventArgs e)
{
checkresult(res[7]);
}
private void label9_Click(object sender, EventArgs e)
{
checkresult(res[8]);
}
I just want to precise my code by defining only one method for all labels. How can i do it?
A pseudocode may look like this:
label1.Click += label_Click(object sender, EventArgs e);
label2.Click += label_Click(object sender, EventArgs e);//SAME HANDLER
label3.Click += label_Click(object sender, EventArgs e);//SAME HANDLER
....
and after
private void label_Click(object sender, EventArgs e)
{
if(sender == label1)
checkresult(res[0]);
else if(sender == label2)
checkresult(res[1]);
...
...
}
First let all of your labels use the same Label_Click event.
private void Label_Click(object sender, EventArgs e)
{
Label temp = sender as Label;
if (temp != null)
{
string labelName = temp.Name;
string labelId = labelName.Substring(5, labelName.Length);
int id = int.Parse(labelId) - 1;
checkresult(res[id]);
}
}
You could set anonymous delegates in when you make the event handler
label1.Click += (s,e) => {checkresult(res[0]); };
label2.Click += (s,e) => {checkresult(res[1]); };
label3.Click += (s,e) => {checkresult(res[2]); };
In WinForms, set your Index to Tag of Label and set each OnClick event to same EventHandler
private void lbl_Click(object sender, EventArgs e)
{
checkresult(res[Convert.ToInt32((sender as Label).Tag)]);
}

.NET/C#: How to remove/minimize code clutter while 'triggering' Events

I just wanna find out if there's a way I could minimize code clutter in my application.
I have written code/s similar to this:
private void btnNext_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnNext.Opacity = 1;
}
private void btnNext_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnNext.Opacity = 0.5;
}
private void btnShowAll_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnShowAll.Opacity = 1;
}
private void btnShowAll_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnShowAll.Opacity = 0.5;
}
private void btnPrev_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnPrev.Opacity = 1;
}
private void btnPrev_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnPrev.Opacity = 0.5;
}
private void btnSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearch.Opacity = 1;
}
private void btnSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearch.Opacity = 0.5;
}
private void btnSearchStore_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearchStore.Opacity = 1;
}
private void btnSearchStore_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnSearchStore.Opacity = 0.5;
}
private void btnCloseSearch_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnCloseSearch.Opacity = 1;
}
private void btnCloseSearch_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnCloseSearch.Opacity = 0.5;
}
private void btnHome_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
btnHome.Opacity = 1;
}
private void btnHome_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
btnHome.Opacity = 0.5;
}
and so on and so forth...
Do I need to create a 'function' that will run initially? Or do I have to create another class just so I can 'organize' them?
Any suggestions?
You could rewrite all those functions into 2:
private void FadeBtn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Button btn = (Button)sender;
btn.Opacity = 1;
}
private void FadeBtn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
Button btn = (Button)sender;
btn.Opacity = 0.5;
}
And then point all of the buttons MouseEnter and MouseLeave events to those functions.
You need to have ChangeButtonOpacity method:
private void ChangeButtonOpacity(Button button, double newOpacity)
{
button.Opacity = newOpacity;
}
And you can implement your handlers as:
private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
ChangeButtonOpacity((Button)sender, 1);
}
private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
ChangeButtonOpacity((Button)sender, 0.5);
}
In this way you will need only two handlers.
Create a Mouse Enter Event and register all the buttons with it. Inside the method you will notice I cast the sender object as a button. So what ever button calls it you can perform this opacity action on.
private void ButtonMouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
Button button = (Button) sender;
button.Opacity = 1;
}
As far I can see, in your case you can shorten to:
private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = 1;
}
private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = 0.5;
}
In the designer, you can choose these event handlers then instead of creating new ones for each button.
Perhaps you can use the Tag property of the button if your not using it for anything else, Then you can do the following.
private void btn_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = (double)((sender as Button).Tag);
}
private void btn_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
(sender as Button).Opacity = 0.5;
}
This would allow you to setup different values for different buttons using only two handlers.

Categories

Resources