RoutedEventArgs cannot be founded in the System.Windows - c#

I have a program on the Windows Forms (VS13). In the code:
using System.Windows;
namespace WF
{
public partial class Form1 : Form
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//...
}
}
}
RoutedEventArgs cannot be found, but it exists in the namespace System.Windows. What can be reason? I have a reference to the WindowsBase.

RoutedEventArgs is a WPF-specific thing. In Winforms, nothing like that exists. You should remove your reference to WindowsBase ans stick to Winforms specific things.
For instance, the Loaded event doesn't exists. You must use the Load event from the Form object instead. The designer really helps here in adding all the bits of code you need and attaching the event handlers.

I did add Windows.UI.Xaml namespace and problem solved.

Related

Windows form doesn't show Form_Load in visual studio

using System;
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;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
I am watching tutorial where Form1_Load() is included
I am watching tutorial where Form1_Load() is included, is there another way to include TextBox items by coding?
CSharp and VB.NET are a bit different on how to write event handlers. In your case, you need to tell the forms designer you want to handle the .Load event.
Find the properties window in the designer for the form, and click on the lightning bolt.
Now the properties window displays all the events that the form can trigger. Find Load and double-click on the space next to it.
The designer will now generate the Form1.Load() method you are missing
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
An alternative way to do this same thing.
In the form constructor type this.Load += and then press the [Tab] button.
Visual studio will fill in the code needed to handle the event wit the option to rename the event handler
A third way of handling the Load event
My preferred way is to override the OnLoad() method in the form.
Type override OnLoad inside the class, and IntelliSense is going to prompt you the method to override. Select OnLoad and press [Tab].
Now you can write the same code you would have before, but inside the OnLoad() method which gets called automatically every time the .Load event triggers.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// Write code here
}
}
Yes in the 20+ years of WinForms programming and VisualStudio development some redundancies have developed. It is not a bad thing, as VS needs to accommodate different coding styles.
There is a 4th way, that applies to VB.NET and involves the Handles keyword which makes more logical sense IMHO than the way C# does things, but that is just me.
You can "wire up" events via code.
Here's an example with the Load() event of the Form:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
This can be done with any control and event, but it's easier to do via the IDE as described by Jeroen van Langen. If you're creating dynamic controls (created at run-time) then this is the way to do it.
Go to your form designer and make sure the form control self is selected. press F4 for the properties control. Press on the lightning icon for events and search for the Load event.

WinForms AutoSave To Database (vs. Save Button)

I'm writing a WinForms (C#) using Entity Framework. application
Currently, I have a big fancy Save Button on my form that works just as you'd hope and expect it to.
I have been asked to change the application to save automatically as the the user changes each field.
I know I can do this by adding code to the Value_Changed event in each of my controls. But is there an easier way? There are a large number of controls on this form. Some way to overload the OnChange event for multiple control types (Text, DropDown, CheckBox, RadioButton)?
In the form's constructor, loop through all of its child controls (this.Controls) recursively and add the (same) callback method to the ValueChanged event for each of them. That will associate it with each control on the form regardless of any changes to the form in the future.
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
foreach (Control c in this.Controls)
{
c.Validated += C_Validated;
}
}
private void C_Validated(object sender, EventArgs e)
{
Debug.Print($"{sender.GetType().FullName} had Validated event called");
}
}
}

usercontrol wpf static memory

I'm new to WPF and coming from a C++ background so maybe I'm worry about memory management too much here.
Anyways, I've got a UserControl (NewContact) that has a grid with 2 columns, upper column displays 3 radio buttons and depending on which is selected it loads the appropriate UserControl into the lower section of the grid.
private void newMilitaryContactRadioButton_Checked(object sender, RoutedEventArgs e)
{
UserControl NMC = new NewMilitaryContact();
NewContactWindowGridDisplay.Children.Insert(1, NMC);
}
private void newMilitaryContactRadioButton_Unchecked(object sender, RoutedEventArgs e)
{
NewContactWindowGridDisplay.Children.RemoveAt(1);
}
private void newLegalContactRadioButton_Checked(object sender, RoutedEventArgs e)
{
UserControl NLC = new NewLegalContact();
NewContactWindowGridDisplay.Children.Insert(1, NLC);
}
private void newLegalContactRadioButton_Unchecked(object sender, RoutedEventArgs e)
{
NewContactWindowGridDisplay.Children.RemoveAt(1);
}
private void newFirmContactRadioButton_Checked(object sender, RoutedEventArgs e)
{
UserControl NFC = new NewFirmContact();
NewContactWindowGridDisplay.Children.Insert(1, NFC);
}
private void newFirmContactRadioButton_Unchecked(object sender, RoutedEventArgs e)
{
NewContactWindowGridDisplay.Children.RemoveAt(1);
}
Now my question is whether I should be, and how to, unload the UserControls I create, when a radio button is unchecked. I did some searching around MSDN documentation and saw that the using the remove method from the parent object would unload the usercontrol. If that is the case is the code I'm using to in the various "unchecked" methods correct so as not to pile up a ton of NFC/NLC/NMC UserControl objects if someone were to click amongst the three radio buttons over and over and over again?
Much thanks to anyone to who can explain this to me :)
Actually you need to read more about .Net memory management and know how it works. In your case it depends on what your UserControls are doing? If they are using system resources it will good to dispose their references in UserControl unloaded events, otherwise GC will take care of them.
Read this article :
Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework
Also the way you are going is not so good, because soon you will find out you need to do more with your UserControl like setting its DataContext, Styles handling events and etc... and this will hard to do with code.

Handling a click over a balloon tip displayed with TrayIcon's ShowBalloonTip()

I use the ShowBalloonTip method of a TrayIcon class to display a balloon tip. Is there a way to handle a click over this balloon?
When I click over the balloon, no event seem to be generated, and it only closes the balloon.
I think you mean NotifyIcon . Use following pattern...
NotifyIcon notifyIcon = null;
public Form1()
{
InitializeComponent();
notifyIcon = new NotifyIcon();
// Initializing notifyIcon here...
notifyIcon.BalloonTipClicked += new EventHandler(notifyIcon_BalloonTipClicked);
}
void notifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
// Operation you want...
}
I hope it feed your needs...
Have you tried the following snippet? I managed to find it whilst doing a quick google search:
private void TrayNotifyIcon_BalloonClick(object sender, EventArgs e)
{
//Perform Action
}
Obviously you'll need to make sure you specify the correct name in the method signature for your own application.
I think this was written in an older version of the .Net Framework and there's probably a newly named method for it.
Source: Build a C# Notification System

Can't fire MouseWheel event in C# Windows Forms

First off, the mousewheel event is not listed in Visual Studio 2008's events pane which is very annoying.
I found the correct format online though, and wrote this into my code:
private void Form1_MouseWheel(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
...from which I'm getting no response when the mousewheel is rotated.
I'm doing this in my code's main class area, and the designer contains only the one form/window/whatever so the mouse isn't losing focus.
namespace BlahBlah
{
public partial class Form1 : Form
{
And by contrast, I have this method right above the mousewheel one and it works fine:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Debug.WriteLine("Foo");
}
If I had to guess, I'm thinking I'm not correctly linking the code to the form (aka: all the stuff that visual studio would do for me if I added this event through the designer's events panel). But I could be wrong or just be making some silly error.
Can you help me get ANY kind of response when the mouse wheel is rotated? Thanks!
The mousewheel event needs to be set up.
Add this to Form1's constructor (After InitalizeComponents();)
this.MouseWheel+= new MouseEventHandler(Form1_MouseWheel);
I don't have enough reputation to respond with a comment, but the answer to your side question is that the delegates do need to be setup. However when you create one by double clicking it in the properties pane the code is automatically generated and placed in the *.designer.cs file in the InitializeComponent method.
It's best in this case to override the OnMouseWheel member function rather than register to the event. For example:
public class MyFrm : Form
{
protected override void OnMouseWheel(MouseEventArgs e)
{
/*Handle the mouse wheel here*/
base.OnMouseWheel(e);
}
}
I don't know how to subscribe to that particular event but you can override the appropriate method on the form, instead.

Categories

Resources