Skip to content

Commit

Permalink
Merge pull request #9107 from dotnet/main
Browse files Browse the repository at this point in the history
  • Loading branch information
BillWagner committed Jun 27, 2023
2 parents 2ff5ac2 + 70e0be5 commit 3db1b15
Show file tree
Hide file tree
Showing 83 changed files with 614 additions and 3,880 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Windows.Forms.Design;

// This example demonstrates how to implement a component editor that hosts
// component pages and associate it with a component. This example also
Expand All @@ -23,65 +18,62 @@ public class ExampleComponentEditor : System.Windows.Forms.Design.WindowsFormsCo
// This method override returns an type array containing the type of
// each component editor page to display.
protected override Type[] GetComponentEditorPages()
{
return new Type[] { typeof(ExampleComponentEditorPage),
typeof(ExampleComponentEditorPage) };
{
return new Type[] { typeof(ExampleComponentEditorPage),
typeof(ExampleComponentEditorPage) };
}
//</Snippet2>

//<Snippet3>
// This method override returns the index of the page to display when the
// component editor is first displayed.
protected override int GetInitialComponentEditorPageIndex()
{
return 1;
{
return 1;
}
//</Snippet3>
}

//<Snippet6>
// This example component editor page type provides an example
// ComponentEditorPage implementation.
internal class ExampleComponentEditorPage : System.Windows.Forms.Design.ComponentEditorPage
{
Label l1;
Button b1;
Label l1;
Button b1;
PropertyGrid pg1;

// Base64-encoded serialized image data for the required component editor page icon.
string icon = "AAEAAAD/////AQAAAAAAAAAMAgAAAFRTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj0xLjAuNTAwMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABNTeXN0ZW0uRHJhd2luZy5JY29uAgAAAAhJY29uRGF0YQhJY29uU2l6ZQcEAhNTeXN0ZW0uRHJhd2luZy5TaXplAgAAAAIAAAAJAwAAAAX8////E1N5c3RlbS5EcmF3aW5nLlNpemUCAAAABXdpZHRoBmhlaWdodAAACAgCAAAAAAAAAAAAAAAPAwAAAD4BAAACAAABAAEAEBAQAAAAAAAoAQAAFgAAACgAAAAQAAAAIAAAAAEABAAAAAAAgAAAAAAAAAAAAAAAEAAAABAAAAAAAAAAAACAAACAAAAAgIAAgAAAAIAAgADExAAAgICAAMDAwAA+iPcAY77gACh9kwD/AAAAndPoADpw6wD///8AAAAAAAAAAAAHd3d3d3d3d8IiIiIiIiLHKIiIiIiIiCco///////4Jyj5mfIvIvgnKPnp////+Cco+en7u7v4Jyj56f////gnKPmZ8i8i+Cco///////4JyiIiIiIiIgnJmZmZmZmZifCIiIiIiIiwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACw==";

public ExampleComponentEditorPage()
{
// Initialize the page, which inherits from Panel, and its controls.
this.Size = new Size( 400, 250 );
this.Icon = DeserializeIconFromBase64Text(icon);
this.Size = new Size(400, 250);
this.Icon = new Icon("myicon.ico");
this.Text = "Example Page";

b1 = new Button();
b1.Size = new Size(200, 20);
b1.Location = new Point(200, 0);
b1.Text = "Set a random background color";
b1.Click += new EventHandler(this.randomBackColor);
this.Controls.Add( b1 );
this.Controls.Add(b1);

l1 = new Label();
l1.Size = new Size(190, 20);
l1.Location = new Point(4, 2);
l1.Text = "Example Component Editor Page";
this.Controls.Add( l1 );
this.Controls.Add(l1);

pg1 = new PropertyGrid();
pg1.Size = new Size(400, 280);
pg1.Location = new Point(0,30);
this.Controls.Add( pg1 );
pg1.Location = new Point(0, 30);
this.Controls.Add(pg1);
}

// This method indicates that the Help button should be enabled for this
// component editor page.
public override bool SupportsHelp()
{
return true;
{
return true;
}

//<Snippet4>
Expand All @@ -95,9 +87,9 @@ public override void ShowHelp()

// Retrieve the Site of the component, and return if null.
ISite componentSite = selectedComponent.Site;
if(componentSite == null)
if (componentSite == null)
return;

// Acquire the IHelpService to display a help topic using a indexed keyword lookup.
IHelpService helpService = (IHelpService)componentSite.GetService(typeof(IHelpService));
if (helpService != null)
Expand All @@ -107,10 +99,10 @@ public override void ShowHelp()

// The LoadComponent method is raised when the ComponentEditorPage is displayed.
protected override void LoadComponent()
{
this.pg1.SelectedObject = this.Component;
{
this.pg1.SelectedObject = this.Component;
}

// The SaveComponent method is raised when the WindowsFormsComponentEditor is closing
// or the current ComponentEditorPage is closing.
protected override void SaveComponent()
Expand All @@ -121,32 +113,19 @@ protected override void SaveComponent()
// This method is invoked by the button on this ComponentEditorPage.
private void randomBackColor(object sender, EventArgs e)
{
if( typeof(System.Windows.Forms.Control).IsAssignableFrom( this.Component.GetType() ) )
if (typeof(System.Windows.Forms.Control).IsAssignableFrom(this.Component.GetType()))
{
// Sets the background color of the Control associated with the
// WindowsFormsComponentEditor to a random color.
Random rnd = new Random();
((System.Windows.Forms.Control)this.Component).BackColor =
((System.Windows.Forms.Control)this.Component).BackColor =
Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
pg1.Refresh();
}
}

// This method can be used to retrieve an Icon from a block
// of Base64-encoded text.
private Icon DeserializeIconFromBase64Text(string text)
{
Icon img = null;
byte[] memBytes = Convert.FromBase64String(text);
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream(memBytes);
img = (Icon)formatter.Deserialize(stream);
stream.Close();
return img;
}
}
//</Snippet6>

//<Snippet5>
// This example control is associated with the ExampleComponentEditor
// through the following EditorAttribute.
Expand All @@ -156,4 +135,4 @@ public class ExampleUserControl : System.Windows.Forms.UserControl
}
//</Snippet5>
}
//</Snippet1>
//</Snippet1>
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
//<Snippet1>
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace TypeCategoryTabExample
{
{
// This component adds a TypeCategoryTab to the property browser
// that is available for any components in the current design mode document.
[PropertyTabAttribute(typeof(TypeCategoryTab), PropertyTabScope.Document)]
Expand All @@ -26,10 +19,6 @@ public TypeCategoryTabComponent()
// category of the type of each property.
public class TypeCategoryTab : PropertyTab
{
[BrowsableAttribute(true)]
// This string contains a Base-64 encoded and serialized example property tab image.
private string img = "AAEAAAD/////AQAAAAAAAAAMAgAAAFRTeXN0ZW0uRHJhd2luZywgVmVyc2lvbj0xLjAuMzMwMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWIwM2Y1ZjdmMTFkNTBhM2EFAQAAABVTeXN0ZW0uRHJhd2luZy5CaXRtYXABAAAABERhdGEHAgIAAAAJAwAAAA8DAAAA9gAAAAJCTfYAAAAAAAAANgAAACgAAAAIAAAACAAAAAEAGAAAAAAAAAAAAMQOAADEDgAAAAAAAAAAAAD///////////////////////////////////9ZgABZgADzPz/zPz/zPz9AgP//////////gAD/gAD/AAD/AAD/AACKyub///////+AAACAAAAAAP8AAP8AAP9AgP////////9ZgABZgABz13hz13hz13hAgP//////////gAD/gACA/wCA/wCA/wAA//////////+AAACAAAAAAP8AAP8AAP9AgP////////////////////////////////////8L";

public TypeCategoryTab()
{
}
Expand Down Expand Up @@ -75,22 +64,10 @@ public override System.Drawing.Bitmap Bitmap
{
get
{
Bitmap bmp = new Bitmap(DeserializeFromBase64Text(img));
Bitmap bmp = new Bitmap(@"myproperty.bmp", true);
return bmp;
}
}

// This method can be used to retrieve an Image from a block of Base64-encoded text.
private Image DeserializeFromBase64Text(string text)
{
Image img = null;
byte[] memBytes = Convert.FromBase64String(text);
IFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream(memBytes);
img = (Image)formatter.Deserialize(stream);
stream.Close();
return img;
}
}
}
//</Snippet1>
20 changes: 1 addition & 19 deletions snippets/csharp/System.Data/EntityKey/Overview/Program.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
using System.Data.Common.CommandTrees;
using System.Data.Metadata.Edm;
using System.Data.EntityClient;
using System.ComponentModel;
using System.Data.SqlClient;

namespace ObjectServicesConceptsCS
namespace ObjectServicesConceptsCS
{
class Program
{
Expand Down
114 changes: 7 additions & 107 deletions snippets/csharp/System.Data/EntityKey/Overview/Source.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
//<snippetUsingSerialization>
//<snippetUsing>
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
//<snippetUsingEvents>
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
//</snippetUsingEvents>
using System.Data.SqlClient;
//</snippetUsing>
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Linq;
//</snippetUsingSerialization>
using System.Xml.Serialization;
using System.Data.Common.CommandTrees;
using System.Data.Metadata.Edm;
using System.Data.EntityClient;
//<snippetUsingEvents>
using System.ComponentModel;
//</snippetUsingEvents>
using System.Data.SqlClient;

namespace ObjectServicesConceptsCS
{
Expand Down Expand Up @@ -1581,102 +1577,6 @@ public static void SerializeToXml()
Console.WriteLine(writer.ToString());
}
}
#region StreamToBinary
//<snippetStreamToBinary>
public static void ReadFromBinaryStream()
{
BinaryFormatter formatter = new BinaryFormatter();
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
try
{
// Get the object graph for the selected customer
// as a binary stream.
MemoryStream stream = SerializeToBinaryStream(@"Adams");

// Read from the begining of the stream.
stream.Seek(0, SeekOrigin.Begin);

// Deserialize the customer graph from the binary stream
// and attach to an ObjectContext.
Contact contact = (Contact)formatter.Deserialize(stream);
context.Attach(contact);

// Display information for each item
// in the orders that belong to the first contact.
foreach (SalesOrderHeader order in contact.SalesOrderHeaders)
{
Console.WriteLine(String.Format("PO Number: {0}",
order.PurchaseOrderNumber));
Console.WriteLine(String.Format("Order Date: {0}",
order.OrderDate.ToString()));
Console.WriteLine("Order items:");
foreach (SalesOrderDetail item in order.SalesOrderDetails)
{
Console.WriteLine(String.Format("Product: {0} "
+ "Quantity: {1}", item.ProductID.ToString(),
item.OrderQty.ToString()));
}
}
}

catch (SerializationException ex)
{
Console.WriteLine("The object graph could not be deserialized from "
+ "the binary stream because of the following error:");
Console.WriteLine(ex.ToString());
}
}
}
private static MemoryStream SerializeToBinaryStream(string lastName)
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();

using (AdventureWorksEntities context = new AdventureWorksEntities())
{
//<snippetQueryTimeout>
// Specify a timeout for queries in this context, in seconds.
context.CommandTimeout = 120;
//</snippetQueryTimeout>

// Define a customer contact.
Contact customer;

// Create a Contact query with a path that returns
// orders and items for a contact.
ObjectQuery<Contact> query =
context.Contacts.Include("SalesOrderHeaders.SalesOrderDetails");

try
{
// Return the first contact with the specified last name
// along with its related orders and items.
customer = query.Where("it.LastName = @lastname",
new ObjectParameter("lastname", lastName)).First();

// Serialize the customer object graph.
formatter.Serialize(stream, customer);
}
catch (EntitySqlException ex)
{
throw new InvalidOperationException("The object query failed", ex);
}
catch (EntityCommandExecutionException ex)
{
throw new InvalidOperationException("The object query failed", ex);
}
catch (SerializationException ex)
{
throw new InvalidOperationException("The object graph could not be serialized", ex);
}

// Return the streamed object graph.
return stream;
}
}
//</snippetStreamToBinary>
#endregion

public static Boolean CleanupOrders()
{
Expand Down
Loading

0 comments on commit 3db1b15

Please sign in to comment.