background preloader

C# Programming

Facebook Twitter

Pass value between forms using events. This comes up so many times in the Forums/Quick Answers I thought it was time for a Tip that I can point people to.

Pass value between forms using events

Passing a value from a child form to the parent is best achieved using an event. To a noob, creating events, especialy when passing a value, can seem unnecessarily complicated. The code below shows a simple implementation that can be adapted as required. The example is using Forms, but the priciple applies to any object type. All the code is commented, any questions - just ask! Hide Copy Code using System; public class TextEventArgs : EventArgs { private string text; public TextEventArgs(string text) { this.text = text; } public string Text { get { return text; } } } C# Corner: Multithreading in WinForms. C# Corner Multithreading in WinForms 11/18/2010 Get Code Download.

C# Corner: Multithreading in WinForms

Object and Collection Initializers (C# Programming Guide) Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.

Object and Collection Initializers (C# Programming Guide)

The object initializer syntax enables you to specify arguments for a constructor or omit the arguments (and parentheses syntax). The following example shows how to use an object initializer with a named type, Cat and how to invoke the default constructor. Note the use of auto-implemented properties in the Cat class. For more information, see Auto-Implemented Properties. Finally, an interactive tutorial series that WILL teach you C# for Unity3D. Making a Simple Game in Unity (Part 1) - Unity C# Tutorial. C# Tutorial for Absolute Beginners. PID process control, a “Cruise Control” example. Overview This code is a working example of a PID (Proportional, Integral, Derivative) control.

PID process control, a “Cruise Control” example

This type of a control is used when processes change due to inertia. (A car's cruise control is a PID controller.) The PID algorithm is surprisingly simple, and can be implemented in five lines of code. There are three constants that must be determined in order to shape the control's output. How to: Write Text to a File. .NET Framework 4.6 and 4.5 The following examples show how to write to a text file from a console app, a Windows Presentation Foundation (WPF) app and a Windows Store app.

How to: Write Text to a File

This example reads all the text files from the user's My Documents folder by searching for "*.txt", and synchronously writes them into a large text file. GUI Windows Forms « C# / CSharp Tutorial.  C# Language. C# Essentials. Creating a Database in Microsoft Visual Studio 2010. Hello friends, hope you all are enjoying good health.

Creating a Database in Microsoft Visual Studio 2010

Around two to three months ago, I have shared a topic on Microsoft Visual Basic 2010 – Com Port in which I have explained completely how to make the serial communication in Microsoft Visual studio using visual basic language. Sending Email in Microsoft Visual Studio 2010. Hello friends, today’s tutorial is about adding functionality in Microsoft Visual Studio 2010 basic email form.

Sending Email in Microsoft Visual Studio 2010

In the previous part of this tutorial How to Send Email in Microsoft Visual Studio 2010, we have seen a basic email form which just sends email to constant email address which is specified in the coding. Today we will make this email form a bit flexible and will be able to send the emails to any email id we want. If you have read the previous part of this tutorial, then this article will be nothing for you, you will got it just like a piece of cake instantly. So, I suppose that you have read the previous tutorial and you have the knowledge of smtp etc so I am not gonna explain it further, so i suggest again to read it first if you have any doubt in configuration part, lets come to this part. Steps to follow: Now the last part is adding code so go into your code editor and remove all the previous code and place this code in it.

Microsoft Visual Basic 2010 – Com Port Tutorial. Update: Few bugs were reported by some readers, which I have corrected and updated the code.

Microsoft Visual Basic 2010 – Com Port Tutorial

Now, its fully tested and 100% working. I have also added comments in code. Computer Science Design Patterns - Wikibooks, open books for an open world. The term Design Patterns can confuse you at first, or it can seem like something incredibly difficult.

Computer Science Design Patterns - Wikibooks, open books for an open world

In fact it is nothing more than convenient ways of identifying, labelling and coding general solutions to recurring design problems. So design patterns are nothing more than commonly occurring patterns in design that are repeatable and generalist enough to be written down and named as software design constructs that all can commonly identify and apply. Note however that a design pattern refers to the logical structure of the code, what it does and how it addresses the issues, not on direct code portability across projects, but in the portability of the way design issues can be addressed.

Imagine yourself working in a project team and someone just wrote a class and is trying to explain to you that only one instance is allowed. Instead the person could simply say "The class I just wrote is a Singleton". Development stages. The Sandpit: Bing Web Search in C# This blog post contains a C# wrapper of the Bing API (Microsoft’s internet search engine).

The Sandpit: Bing Web Search in C#

In my previous post I presented a similar wrapper for Google’s Custom Search API, successor to the Web Search API. I was frankly less than impressed with Google custom search API as it appears to be engineered to favor specific themes and/or domains. The Sandpit: Google Custom Search in C# This post contains a C# class to search the internet using Google’s custom search API. Before using this class you must first create a Google account (link) and generate an API key (link). In this sample, I called the Google search API from a WPF application, but it would also work in a WinForms or ASP.NET application as suggested above.

Here is the XAML of the Window that displays the results of the Google search. public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); And here is the class that calls Google’s custom search API, desterilizes the JSON response and returns the result as single .NET objects. // Submit Request WebClient w = new WebClient(); w.DownloadStringCompleted += (a, b) => { // Check for errors if (b == null) { return; } if (b.Error ! Download C# Code Samples for the Live Search API from Official Microsoft Download Center. <a id="b7777d05-f9ee-bedd-c9b9-9572b26f11d1" target="_self" class="mscom-link download-button dl" href="confirmation.aspx? Id=7950" bi:track="false"><span class="loc" locid="46b21a80-a483-c4a8-33c6-eb40c48bcd9d" srcid="46b21a80-a483-c4a8-33c6-eb40c48bcd9d">Download</span></a> Example code for calling the Live Search SOAP web service in your C# applications DetailsCSharp Live Search Samples.zip This code sample demonstrates how to use the Live Search SOAP web service in your C# application, whether they are ASP.Net, Win32 or WPF based.

It includes examples of all major functionality provided by the web service and includes a reusable module to simplify the API calls.For the latest updates and news on the Live Search API, please subscribe to the Live Search Developer's Blog. Electronic Component API download. Overview - Octopart API. The Octopart API is the easiest way for developers to access up-to-date pricing and availability information, datasheets, compliance documents and technical specs for electronic components from distributors and manufacturers around the world. Using the Octopart API, you can access stocking information, price breaks, lead times, minimum order quantities and much more information from over 100 distributors including Digi-Key, Mouser, Newark, Premier Farnell, Arrow, RS Components, Future Electronics, Grainger and many others. You can also access millions of datasheets and compliance documents for parts from over 4000 manufacturers.

Authentication The Octopart API does not require any usernames or passwords to use, however it does require a valid API key for every request. Quick-start sample app for C# / .NET   This quick-start sample app is built in .NET and lets you get started with the Google+ platform in a few minutes. The app demonstrates: Using the Google+ Sign-In button to get an OAuth 2.0 refresh token. Exchanging the refresh token for an access token. Making Google+ API requests with the access token, including getting a list of people that the user has circled.

Disconnecting the app from the user's Google account and revoking tokens. System requirements Supported operating systems:WindowsVisual Studio 2010 and later Step 1: Enable the Google+ API Create a Google Developers Console project, OAuth 2.0 client ID, and register your JavaScript origins: Google/google-api-dotnet-client. C# Essentials. How to: Write Text to a File. The following examples show how to write text to a text file. The first example reads all the text files from the user's My Documents folder by searching for "*.txt", and synchronously writes them into a large text file. using System; using System.IO; using System.Text; using System.Collections.Generic; class Program { static void Main(string[] args) { string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); StringBuilder sb = new StringBuilder(); foreach (string txtName in Directory.EnumerateFiles(mydocpath,"*.txt")) { using (StreamReader sr = new StreamReader(txtName)) { sb.AppendLine(txtName.ToString()); sb.AppendLine("= = = = = ="); sb.Append(sr.ReadToEnd()); sb.AppendLine(); sb.AppendLine(); } } using (StreamWriter outfile = new StreamWriter(mydocpath + @"\AllTxtFiles.txt")) { outfile.Write(sb.ToString()); } } } The next example shows how to asynchronously write user input from a text box to a file.

Tasks Reference Other Resources. GUI Windows Forms « C# / CSharp Tutorial. C# Applet