1 Module 1 1.1 DragDrop listboxgaatmee.dragenter += new DragEventHandler(control_DragEnter); e.effect = DragDropEffects.Move; //noodzakelijk, anders geen drop mogelijk (retarded I knows) listboxgaatmee.dragdrop += new DragEventHandler(listBox_DragDrop); ListBox from = (ListBox)e.Data.GetData(typeof(ListBox)); listboxgaatmee.allowdrop = true; // hier mag gedropt worden 1.2 Image lists Is een Control, slepen op form, komt onderaan te staan, en dan Choose images in extra menu van het control beest. 1.3 ListView, TreeView, Eerst BeginUpdate, dan EndUpdate() 1.4 TreeView TreeNode rootnode = treeview.nodes.add( UniquieKey, Visible text, afbeeldingindex); TreeNode childnode = rootnode.nodes.add( node.nodes.find(key, recurse) 1.5 ListView listview.columns.add(name, width, alignment); item = new ListViewItem( first col text ); itme.subitems.add( second col text ); listview.items.add(item) 1.6 ToolTips New ToolTip() tt instellen... tooltip.settooltip(controlthatneedstooltip, tekst om te tonen ) tooltip.settooltip(anothercontrolthatneedstooltip, tekst om te tonen ) 1.7 MDI (this) Parent.IsMDIContainer = true child.mdiparent = parent (this) child.show() parent.layoutmdi(mdilayout. ) parent.activemdichild
1.8 Toolstrip DisplayStyle = ImageAndText, ImageScaling = None, 1.9 Properties private int Lol get; set; public int Purr set mirr = value; 2 ADO (module 2) 2.1 Algemeen en most manual ADO Using System.Data.SqlClient, System.Data Id van laatste insert: SELECT @@IDENTITY of SELECT SCOPE_IDENTITY() Bv: INSERT ; SELECT @@IDENTITY met executescalar 2.1.1 App.Config <?xml version="1.0" encoding="utf-8"?> <configuration> <connectionstrings> <add name="thuis" connectionstring="data Source=.\SQLEXPRESS; AttachDbFilename=c:\...\Contactpersonen.mdf; Integrated Security=True; User Instance=True;"/> <add name="school" connectionstring="server=codd; Database=Ti2_CSharp_s50 ; Integrated Security=SSPI;"/> <add name="bis" connectionstring="data Source=codd; Initial Catalog=Ti2_CSharp_s50 ;User Id=s50 ;Password= ;"/> </connectionstrings> </configuration> See also: 3TIER > DABaseClass 2.1.2 Databinding DataSource, DisplayMember, ValueMember 2.1.3 DataReader (en SQL fetchness) SqlConnection connection = new SqlConnection(connectionString); connection.open(); SqlCommand command = new SqlCommand( Select * from Contact order by Naam", connection); SqlDataReader datareader = command.executereader(); while (datareader.read()) datareader["col"] datareader.close(); connection.close();
2.1.4 SQL Params SqlCommand command = new SqlCommand("Update Contact set naam = @naam, email = @email where id = @id", connection); // Let op: dezelfde volgorde gebruiken als voorkomen in SQL-statement command.parameters.add(new SqlParameter("@naam", textboxnaam.text)); 2.2 basic 3TIER (using sql commands directly) 2.2.1 DABaseClass using System.Configuration; using System.Data.SqlClient; class DABaseClass private static string connectionstring = ConfigurationManager.ConnectionStrings["thuis"].ConnectionString; protected SqlConnection connection; public void Open() connection = new SqlConnection(connectionString); connection.open(); public void Close() connection.close(); 2.2.2 DAKlant Class DAKlant : DABaseClass public List<Album> GetAlbumsByReeks(int reeksid) //sql command statements public void UpdateAlbum(Album album) //accepts an entity class! Delete accepts id though 2.2.3 BLKlant Class BLKlant : DAKlant //No sql in ere! Just logic stuff like, if this changes, that must change too
2.3 DataSets + DataAdapters straight to the DB 2.3.1 Datasets zelf aanmaken (hardcore) var dataset = New DataSet() DataTable tablewerknemer = new DataTable("werknemer"); DataTable tableafdeling = new DataTable("afdeling"); dataset.tables.add(tablewerknemer); dataset.tables.add(tableafdeling); DataColumn kolom = new DataColumn("id", typeof(int32)); kolom.allowdbnull = false; kolom.unique = true; tablewerknemer.columns.add(kolom); tablewerknemer.primarykey = new DataColumn[] tablewerknemer.columns["id"] ; dataset.relations.add( new DataRelation("fkAfdelingWerknemer", tableafdeling.columns["id"], tablewerknemer.columns["afdelingid"], true)); 2.3.2 Row insert DataRow rij = tablewerknemer.newrow(); rij["id"] = 1; rij["naam"] = "Jef Verboven"; tablewerknemer.rows.add(rij); 2.3.3 Navigeren met relaties DataRow afdeling foreach (DataRow werknemer in afdeling.getchildrows("fkafdelingwerknemer")) 2.3.4 Filtering table.select("afdelingid = 1", "Naam ASC"); OR DataView dataviewfilter = new DataView(dataset.Tables["werknemer"]); dataviewfilter.sort = "naam Asc"; dataviewfilter.rowstatefilter = DataViewRowState.CurrentRows; dataviewfilter.rowfilter = "afdelingid = 1"; DataTable filterrecord = dataviewfilter.totable();
2.3.5 Row editing row.beginedit(); row ["naam"] = textboxnaam.text; row.endedit(); 2.3.6 Dataset + DataAdapter SqlCommand ploegcommand = new SqlCommand("Select * From Ploeg", connection); SqlDataAdapter ploegadapter = new SqlDataAdapter(ploegCommand); 2.3.6.1 Automatisch update, insert, delete commands aanmaken op adapter // De ctor stelt automatisch insert commando s in op onze adapter, het return obj mag men negeren :/ new SqlCommandBuilder(rennerAdapter); 2.3.6.2 Data laden ploegadapter.fill(dataset, "Ploeg"); 2.3.6.3 Wegschrijven naar db try renneradapter.update(dataset, "Renner"); catch (DBConcurrencyException extransaction) 2.4 3TIER again, with datasets and dataadapters (doubt we ll need this) 2.4.1 DAL Make sql command, give to adapter, store adapter in DA. Take dataset as param. Fill, Update shizzle 2.5 More 3TIER, but with Typed Datasets now! In DAL package: Add new Item > DataSet (geef het een naam die niet hetzelfde is als iets anders in proj ) Drag stuff onto it from server explorer, modify with Configure in context menu, pick Return datatable where appropriate. 2.5.1 DAL Autogenerated by dataset.xsd thing 2.5.2 BL using ProjectManagement.DAL; using ProjectManagement.DAL.PJManagementTableAdapters; class BLTask private TasksTableAdapter adapter; public BLTask()
adapter = new TasksTableAdapter(); public PJManagement.TasksDataTable GetFilteredTasks(int projectid, int StateID, int priorityid) return adapter.getfilteredtasks(priorityid, priorityid, StateID); 2.5.3 Forms (front-end) using ProjectManagement.BLL; using ProjectManagement.DAL; Runs round using typed DataTables and DataRows and requests them from BLL. 3 Module 3 3.1 Unit testing Add Test Project Add ref to own project Check if classes in project that we want to test are public 3.1.1 Basic testing Use Assert.AreEqual, Test for exceptions with: [ExpectedException(typeof(SaldoOntoereikendException))] 3.1.1.1 Init code [ClassInitialize()] executed before First test 3.1.1.2 Cleanup code [ClassCleanup()] executed after last test 3.1.1.3 Ignore method [TestMethod, Ignore()] 3.1.2 Testing SQL DB projects Add refs to System.Data, System.Data.DataSetExtensions, System.Xml In init and cleanup use the methods of the target proj itself, this way we don t need to know a thing about the DB, the other project will need to though. ^^ 3.1.3 Data driven unit test (xml file erin pompen met test data) Go to Test Windows TestListEditor. Click test, go to properties, click data connection string. Select xml in popup, go to file Use TextContext.DataRow in test (test method is rerun for every row)
3.2 Delegates public delegate Boolean MijnFunctie(T parameter1, T parameter2); 3.3 Threading using System.Threading; thread = new Thread(someFunc); thread.start(); 3.3.1 Sleeping Thread.Sleep(250); 3.3.2 Queueing stuff on the gui thread //this refers to a gui thing, every Control has Invoke(delegate(no args)) this.invoke(new MethodInvoker(ListBoxClear));
3.3.3 Adding params to the threading public class Counter private int max; public int Max set max = value; public void Count() for (int i = 0; i <= max; i++) Console.WriteLine(Thread.CurrentThread.Name + " counts " + i); Thread.Sleep(250); Console.WriteLine(Thread.CurrentThread.Name + " stops counting..."); Counter counter = new Counter(); counter.max = aantalthreads; Thread thread = new Thread(counter.Count); thread.name = "Thread " + aantalthreads; thread.start(); 3.3.4 Cleanup nicely // mark thread as background Thread ghostshower = new Thread(ShowGhost); ghostshower.isbackground = true; // unless you add Application.Exit() to closing event, the form is just disposed of // This kills all background threads and gui threads Application.Exit() 3.3.5 Syncing object countlock = new object(); lock (countlock) // dit codeblok kan maar door 1 thread tegelijk uitgevoerd worden