RSS 2.0 Feed
2007-12 Entries
摘要:[原文作者]: Kit George [原文链接]: LINQ Cookbook, Recipe 1: Change the font for all labels on a windows form   我们打算启动一个LINQ“菜谱”项目,并逐步将它完成。这个项目的目的是为你在编程和用查询时遇到的一些特殊情况提供解决的方案。我再也想不出一个更好的方式,来关联你能利用LINQ所做的每件事情了。   这个系列项目主要是为您展示LINQ可以做到的各式各样的事情。像任何一本好菜谱一样,我们将为这本菜谱做分类,那样你之后就可以很容易地查询。但是不要希望一个特定的“菜谱”会有大量的描述,尽管它会包含重现步骤。请尽管向我们要求任何你想要看到的菜谱,而我们将尽可能多地创造新的。   材料: -      Visual Studio 2008 (Beta2 或更高版本)   类别: -      LINQ-To-Objects,LINQ with Windows Forms,LINQ with controls,Label   制作方法: -      打开 Visual Studio 2008,点击“文件/新建项目”,找到并双击“Windows窗体应用程序”图标。 -      增加窗体的大小,使其足够容纳一定数量的控件。 -      从工具箱中拖放几个控件到窗体上,确保至少添加3-4个标签控件。       o  我个人比较喜欢的是两个分组控件(GroupBox),在文本框(TextBox)前放两个标签(Label)(标签在分组控件之上),并在每个分组控件中放一对单选框(RadioButton)控件。这样可以确保测试递归代码。 -      添加一个按钮到窗体上并把它的Text属性改为“Go”。双击这个按钮,然后修改事件处理代码并添加下面的方法:       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click         For Each label In GetLabels(Me)             label.Font = New Font("Comic Sans MS", 12, _                                   FontStyle.Bold Or FontStyle.Underline)         Next     End Sub       Private Function GetLabels(ByVal sourceControl As Control) _                                As IEnumerable(Of Control)         If sourceControl.Controls.Count > 0 Then             Dim labels = From cont As Control In sourceControl.Controls _                          Where TypeOf......[阅读全文]

posted @ | Feedback (11) | Filed Under [ VB Team Blog LINQ Cookbook ]

摘要:[原文作者]:Kit George [原文链接]:LINQ Cookbook, Recipe 2: Find all capitalized words in a phrase and sort by length (then alphabetically) (Kit George)   准备材料: -         Visual Studio 2008 (Beta2 或更高版本) -         一些需要搜索的字符串   类别:  LINQ-To-Objects, LINQ and string, LINQ and WinForms   制作方法: -         打开Visual Studio 2008,点击菜单“文件/新建项目”,找到并双击 ”Windows 窗体应用程序” 图标 -         拖放一个Listbox 到窗体上,调整它的高度,然后拖放一个按钮到窗体上 -         双击这个按钮,并将下面的代码添加到按钮的事件处理函数中:           Dim text = "Good morning everyone. I'd like to welcome " & _                    "you to today's presentation on LINQ. My " & _                    "name is Kit George and I'm a Program Manager " & _                    "for Microsoft, on the Visual Basic team. You " & _                    "might be wondering where my......[阅读全文]

posted @ | Feedback (1) | Filed Under [ VB Team Blog LINQ Cookbook ]

昨天我答应写一些大家在以前的博客中没有看到的关于Visual Basic和Visual Studio 2008的新东西,我把它叫做隐藏宝石。我曾提到过在VS2008中的一些特性我很喜欢,还炫耀过智能感知(Intellisense )的改善,尤其是对关键字、本地变量和表达式,自动完成语句的改进。在过去,我还提到过一个新的语法特性,称之为Relaxed Delegates,它允许你为事件(event)提供一个可替代的签名(alternate signatures)。以上这些都是很好的特性,但我今天要说的不是这些,是隐藏宝石(可能这些你在一些C#的文章中见过了,在Visual Basic 2008中一样炫)。

posted @ | Feedback (1) | Filed Under [ VB Team Blog ]

摘要: [原文作者]:Beth Massi [原文链接]:LINQ to SQL and One-To-Many Relationships   最近,有人问我关于发表在creating a one-to-many form with LINQ to SQL 上的一个视频的一个问题。在这个视频示例中,我用Northwind数据库,在Cusomers和Orders两个实体间建立了一对多的关联。我们总是喜欢用Northwind做示例,但是有时候我自己都忘记了它的设计有多么不切实际,例如随处可见的nullable foreign keys,child table的外健值(例如Orders表中的CusomerID或者Product 表中的CategoryID)被设置为nullable也就表示这些child rows在parent table中可以没有对应的row存在。 通常情况下,对于主外健关联,外健值是设置为non-nullable,就是为了child rows 与parent table中的row相关联的,这也是为了保证数据库中数据的完整性。外健值为nullable这种情况一般用于查询,这时查询的关键字可以为空。外健值设置为non-nullable,也表示在删除child table中child rows前,首先要先删除parent table中所对应的row(除非你明确设置主外健关联的delete rule 为"Cascade")。下图是Customers和Orders的典型实例:两个表建立主外健关联,外健值设置为non-nullable , 阅读全文]

posted @ | Feedback (4) | Filed Under [ Beth Massi的blog ]

摘要:[原文作者]:Beth Massi [原文链接]:An Example of Dynamic Programming in VB 在论坛里已经有很多人问我要VB动态编程的实例。大家都已经了解使用动态编程的优势,但对于如何在VB中使用的一些细节仍不太清楚。由于动态编程这个词是在最近才开始流行的,现在仍有很多读者不熟悉这个术语,对于这些读者,我想首先要简单介绍一下什么是“动态”。 其实我的背景也是来自于一种类似Visual Basic的动态编程语言,但是相比于VB,他有更好的数据处理和综合查询能力,并且从1995年后,他也更倾向于面向对象编程。这个语言就是Visual FoxPro。VFP和VB都具有的一些优势包括他们都很容易对对象进行操作,并且在设计时并不要求完全明确数据的类型,也就是所谓的弱类型的语言。VFP所特有的优势是能够动态执行代码,较强的交互性和支持游标,对矩阵数据源的综合查询能力。它也丰富了基于元数据的编程风格,使它能够在运行过程中改变应用程序的行为而不需要重编译。VFP 的缺点是它是完全的动态,而没有任何的静态输入检查。大家都知道你要是不仔细的话,它会给你带来很大的麻烦。。 现在VB 9既有集成查询(Linq,并且它比VFP更完善)又有静态类型检查,所以我会选择VB。VB支持动态类型,当需要的时候,也支持动态类型。这是我所知的唯一一种兼具静态和动态类型能力的语言。对于那些我曾经专注于开发的基于数据和信息系统的应用程序来说,这是一个巨大的胜利。我们需要能够简单的对这类应用程序进行设置和个性化而不需要重编译,同样我们也需要一种语言能够让我们在简单的做这些操作的同时也能够减少写代码的工作。 Visual Basic动态编程的构建和交互性仍有很大的拓展空间,并且我认为在VB 10里我们就能够看到这些改进,不过,我还是想向你展示一下使用VB 8 (VS 2005)和VB 9 (VS 2008)来做动态编程的优势。为了证实这点,我会创建一个简单的动态UI界面,他会通过读取一个XML文件来在Windows Form界面上显示一些控件。我们将用VB 8来做这个工作,同时我会在使用VB 9和VS 2008开发程序的时候,演示一些XLinq的使用。 一个VB 8.0, VS 2005动态编程实例 我已经创建了一个名为questions.xml的XML文件,该文件包含了一些关于一个调查表格的信息,我想通过这些信息动态的创建这个调查表。它不仅包含了这些问题的本身,同时也包含了他需要显示的控件类型(和一个能够找个这个控件的集合),还有一些其它的附加属性如前景色和背景色: <?xml version="1.0" encoding="utf-8" ?> <questions>   <question>     <assembly>System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</assembly>     <control>System.Windows.Forms.TextBox</control>     <text>This is the first survey question.</text>     <height>35</height>     <forecolor>Blue</forecolor>     <backcolor>Control</backcolor>   </question>   <question>     <assembly>System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</assembly>     <control>System.Windows.Forms.TextBox</control>     <text>This is the second survey question.</text>     <height>100</height>     <forecolor>Red</forecolor>     <backcolor>Pink</backcolor>   </question>   <question>     <assembly>System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</assembly>     <control>System.Windows.Forms.Label</control>     <text>This is the third survey question.</text>     <height>80</height>     <forecolor>Cornsilk</forecolor>     <backcolor>Black</backcolor>   </question>   <question>     <assembly>System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</assembly>     <control>System.Windows.Forms.Button</control>     <text>This is the fourth survey question.</text>     <height>30</height>     <forecolor>HotPink</forecolor>    ......[阅读全文]

posted @ | Feedback (1) | Filed Under [ Beth Massi的blog ]

摘要:[原文作者]:Beth Massi [原文链接]:Making our Code More Dynamic 在前一篇文章中,我演示了如何动态的创建一个基于XML的用户UI界面,可是在那个程序里,因为是我们自己写的那个xml文件,所以我们早就知道了想要操作的是哪些属性。现在我想对原先的那个示例做一些扩展,对于那些我们动态创建的对象也能通过从XML中读取其属性名称的方法,来动态的对这些属性赋值。 为了能够动态的获取和设置属性,我们需要重写Dynamic类。在这儿,我们用到了函数CallByName(这和VFP中的SetProperty()方法类似)。这个函数在很多场合很有用,可惜的是,目前它只能应用于简单类型,所以我们无法用它来动态的设置颜色(Color类型)。请看以下代码: 我修改过的XML文件是这样的: <?xml version="1.0" encoding="utf-8" ?> <questions>   <question>     <assembly>System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</assembly>     <control>System.Windows.Forms.TextBox</control>     <text>This is the first survey question.</text>     <height>35</height>     <readonly>true</readonly>     <tabstop>false</tabstop>     <multiline>true</multiline>   </question>   <question>     <assembly>System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</assembly>     <control>System.Windows.Forms.TextBox</control>     <text>This is the second survey question.</text>     <height>100</height>     <readonly>true</readonly>     <tabstop>false</tabstop>     <multiline>true</multiline>   </question>   <question>     <assembly>System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</assembly>     <control>System.Windows.Forms.Label</control>     <text>This is the third survey question.</text>     <height>80</height>     <tabstop>false</tabstop>   </question>   <question>     <assembly>System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</assembly>     <control>System.Windows.Forms.Button</control>     <text>This is the fourth survey question.</text>     <height>30</height>     <tabstop>false</tabstop>   </question> </questions>   现在我们只从文件中读出一列属性(Properties)。这个可以用VB8的DataSet轻松做到: 'Read the xml into a dataset for easier processing of the elements Dim myData As New DataSet() myData.ReadXmlSchema(CurDir() & "\questions.xsd") myData.ReadXml(CurDir() & "\questions.xml") Dim survey......[阅读全文]

posted @ | Feedback (1) | Filed Under [ Beth Massi的blog ]

VB.NET:在多行字符串中插入代码和避免下划线

posted @ | Filed Under [ Beth Massi的blog ]

摘要:[原文作者]:Beth Massi [原文链接]:Getting Started with LINQ to XML   正如Rob所指出的(实际上,我今天在Dev Center上看到了他的帖子),这里有一些关于Visual Studio 2008的教程视频。其中一个就是关于LINQ to XML的概览(不过很抱歉,我并不能给出该视频的直接的链接,因为它被掩盖在精巧的Silverlight控件之下,滚动下视频自己找找吧) Chris Pels 给出了一个很好的XML programming介绍,但仍有两点个人非常喜欢的地方遗漏掉了(他们是内嵌表达式和XML智能感知)。这两个特性也非常易于使用。所以,请先看视频然后再回来,我将带你们浏览下面的内容。   内嵌表达式(Embedded Expression) 这里,我们通过直接复制XML文本,创建一个XDocument对象 Dim contactlist1 = <?xml version="1.0" encoding="utf-8"?>                    <contacts>                        <contact>                            <lastname>Davolio</lastname>                            <firstname>Nancy</firstname>                            <state>WA</state>                            <phone>(206) 555-9857</phone>                        </contact>                        <contact>                            <lastname>Buchanan</lastname>                            <firstname>Steven</firstname>                            <state>CA</state>                            <phone>(925) 555-4848</phone>                        </contact>                        <contact>                            <lastname>Suyama</lastname>                            <firstname>Michael</firstname>                            <state>CA</state>                            <phone>(925) 555-7773</phone>                        </contact>                        <contact>                            <lastname>Callahan</lastname>                            <firstname>Laura</firstname>                            <state>WA</state>                            <phone>(206) 555-1189</phone>                        </contact>                    </contacts> 有趣的是,你可以通过<%=%>创建内嵌表达式,并且任何VB代码都可以放在这里。下面的一个例子将把现在时间放在lastUpdated的属性里 Dim contactlist2 = <?xml version="1.0" encoding="utf-8"?>                    <contacts lastUpdated=<%= Now() %>> ...                    </contacts> 内嵌表达式有各种各样的使用方式。你还可以通过内嵌LINQ查询(比如LINQ to SQL)来创建一个XML对象。 下面的例子将展示如何将Employee表中的雇员信息查询出来并建立一个XDocument对象 Dim contactlist3 = <?xml version="1.0"?>                    <contacts>                        <%= From employee In db.Employees _                            Select <contact>                                       <lastname><%= employee.LastName %></lastname>                                       <firstname><%= employee.FirstName %></firstname>                                      ......[阅读全文]

posted @ | Feedback (3) | Filed Under [ Beth Massi的blog ]