"System.Windows.Forms.Timer" could not be resolved

Error 1 The base class or interface 'System.ComponentModel.Component' in assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' referenced by type 'System.Windows.Forms.Timer' could not be resolved

k:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Windows.Forms.dll App2

I got this error message after adding the system.windows.forms link.

+4
source share
3 answers

Since you are using Wpf, I made a quick working example. Make sure your project links look like this.

enter image description here

Mainwindow

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Forms; namespace WpfApplication1 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { Timer tmr = new Timer(); public MainWindow() { InitializeComponent(); tmr.Interval = 2000; tmr.Tick += new EventHandler(tmr_Tick); tmr.Start(); } void tmr_Tick(object sender, EventArgs e) { tmr.Stop(); throw new NotImplementedException(); } } } 

And as indicated, it would be easier if you could use Wpf Dispatcher Timer. IN, looking at an example link, there is no need to use the Windows Form Timer, the dispatcher timer will work fine in this case, since it is a WPF program.

Edit Modified based on your link

 public partial class MainWindow : Window { System.Windows.Threading.DispatcherTimer tmrStart = new System.Windows.Threading.DispatcherTimer(); System.Windows.Threading.DispatcherTimer tmrStop = new System.Windows.Threading.DispatcherTimer(); public MainWindow() { InitializeComponent(); tmrStart.Interval = TimeSpan.FromSeconds(2); //Delay before shown tmrStop.Interval = TimeSpan.FromSeconds(3); //Delay after shown tmrStart.Tick += new EventHandler(tmr_Tick); tmrStop.Tick += new EventHandler(tmrStop_Tick); } void tmrStop_Tick(object sender, EventArgs e) { tmrStop.Stop(); label1.Content = ""; } void tmr_Tick(object sender, EventArgs e) { tmrStart.Stop(); label1.Content = "Success"; tmrStop.Start(); } private void button1_Click(object sender, RoutedEventArgs e) { tmrStart.Start(); } } 
+4
source

If you use WPF exclusively, there is no reason to refer to System.Windows.Forms (WinForms). These are two different technologies, and I would not recommend mixing them if it is not necessary.

If you use a WinForms timer, consider using WPF DispatcherTimer .

+3
source

Try adding the System link manually.

1) Right-click on your project. Click Unload Project .

2) Right-click on the downloaded project. Click Edit <YourProjectName>.csproj

3) Find the ItemGroup that contains all the <Reference Include="AssemblyName"> and add <Reference Include="System" /> in a new line.

4) Save the file and right click on your project and click Reload Project

0
source

All Articles