Tag: c# 3.0

抽绳自动换行或显示整个文本

这是我使用DrawString时得到的输出。 I = Smith,John II = Johnson,Mark III = Anderson,James IV = William,Craig V = Ford,他… page是一个float数据类型,该值基于e.PageSettings.Margins.Left; e.Graphics.DrawString(Text,new System.Drawing.Font(“Arial”,8F,FontStyle.Regular),Brushes.Black,page,30); 在上面的例子中,它是 e.Graphics.DrawString(Text, new System.Drawing.Font(“Arial”, 8F, FontStyle.Regular), Brushes.Black, page, 30); 我试过用这个 StringFormat format = new StringFormat(); format.FormatFlags = StringFormatFlags.FitBlackBox; e.Graphics.DrawString(Text, new System.Drawing.Font(“Arial”, 8F, FontStyle.Regular), Brushes.Black, page, 30, format); 我如何扩展/自动换行以便我可以在最后使用整个单词而不是“…”? I = Smith,John II = Johnson,Mark III = […]

不允许使用默认参数说明符

我有以下代码给出错误 不允许使用默认参数说明符 怎么解决这个问题? bool listSubscribe(string apikey, string id, string email_address, string [] merge_vars, string email_type=”html”, bool double_optin=false, bool replace_interests=true, bool send_welcome=false); bool listUnsubscribe(string apikey, string id, string email_address, bool delete_menber=false, bool send_goodbye=true, bool send_notify=true);

在基础构造函数中使用lambdas表达式的例子

在我们构建的框架中,我们需要以下模式: public class BaseRenderer { Func renderer; public BaseRenderer(Func renderer) { this.renderer = renderer; } public string Render() { return renderer(); } } public class NameRenderer : BaseRenderer { public string Name{ get; set; } public NameRenderer () : base(() =>this.Name) {} } 如您所见,我们在调用基础构造函数时创建了一个lambda。 public class Program { public static void Main() { Console.WriteLine(new NameRenderer(){Name […]

C#变长args,这更好,为什么:__ arglist,params数组或Dictionary ?

我最近阅读了以下溢出post: C#的隐藏function 其中一个特征是arglist。 为什么选择这个或替代方法作为对方法使用可变长度参数列表的方法? 另外,请注意我可能不会在我的代码中使用这种构造,除非角落案例需要这样做。 这更像是一个语义问题,而不是甚至使用可变长度参数是否实际或谨慎。 那么有谁知道哪个更好,为什么? [Test] public void CanHandleVariableLengthArgs() { TakeVariableLengthArgs(__arglist(new StringBuilder(), 12)); object[] arr = { new StringBuilder() }; TakeVariableLengthArgs2(arr); TakeVariableLengthArgs3( new Dictionary { { “key”, new StringBuilder() } }); } public void TakeVariableLengthArgs(__arglist) { var args = new ArgIterator(__arglist); var a = (StringBuilder)TypedReference.ToObject(args.GetNextArg()); a.Append(1); } public void TakeVariableLengthArgs2(params object[] args) { […]

编写一个接受out参数的lambda或匿名函数

我的代码中定义了一个委托: public bool delegate CutoffDateDelegate( out DateTime cutoffDate ); 我想创建委托并使用lambda或匿名函数初始化,但这些都没有编译。 CutoffDateDelegate del1 = dt => { dt = DateTime.Now; return true; } CutoffDateDelegate del2 = delegate( out dt ) { dt = DateTime.Now; return true; } 有办法做到这一点吗?

如何获得图像的分辨率? (JPEG,GIF,PNG,JPG)

我找到了这个方法: Graphics g = e.Graphics; Bitmap bmp = new Bitmap(“winter.jpg”); g.DrawImage(bmp, 0, 0); Console.WriteLine(“Screen resolution: ” + g.DpiX + “DPI”); Console.WriteLine(“Image resolution: ” + bmp.HorizontalResolution + “DPI”); Console.WriteLine(“Image Width: ” + bmp.Width); Console.WriteLine(“Image Height: ” + bmp.Height); SizeF s = new SizeF(bmp.Width * (g.DpiX / bmp.HorizontalResolution), bmp.Height * (g.DpiY / bmp.VerticalResolution)); Console.WriteLine(“Display size of image: […]

参数化DllImport用于C#应用程序

我们有一个供应商,提供访问其硬件的库。 不幸的是,如果您有多个设备,则需要多次导入其库,并使用不同的dll名称。 因此,我们有一吨重复的代码,我担心它很快就会成为维护的噩梦。 我们现在所拥有的是: namespace MyNamespace { public static class Device01 { public const string DLL_NAME = @”Device01.dll”; [DllImport(DLL_NAME, EntryPoint = “_function1”)] public static extern int Function1(byte[] param); … [DllImport(DLL_NAME, EntryPoint = “_function99″)] public static extern int Function99(int param); } …. public static class Device16 { public const string DLL_NAME = @”Device16.dll”; [DllImport(DLL_NAME, EntryPoint = […]

绑定到DataGridView.Datasource时加载DataTable慢

我到处搜索过,我无法想出这个。 我正在开发一个Winforms UI,它正在拉动我需要在DataGridView中显示的大量行。 我已经阅读了有关限制行计数和分页的所有内容,对我来说绝对没有好办法。 基本上我正在编写我在Codeplex上编写的SQL Server 2008扩展事件管理器的TargetDataViewer控件。 http://extendedeventmanager.codeplex.com/ 基于特定目标以及它如何呈现数据,我只能做我能做的事情。 我想要做的是将从目标读取的数据流式传输到DataGridView,类似于Profiler或SQL Server Management Studio在流入时显示数据的方式。我重写了很多代码,并使用BackgroundWorker提取数据并将其处理为DataTable。 如果我没有设置DataGridView.DataSource = DataTable,我可以在几分钟内将300K +行数据加载到DataTable中,它确实运行得很快。 一旦我将DataTable添加到DataSource,它就会慢慢停止(而不是几分钟,相同的300K行可能需要1/2小时)。 我知道问题不是我的处理代码,它特定于绑定到DataGridView.DataSource,我有时间码来certificate这一点。 我无法弄清楚如何解决这个问题。 对于性能,我可以在加载数据后将控件绑定到DataTable,但这是一个非常糟糕的用户体验。 我看到很多人在加载数据时抱怨DataGridView的性能影响,所以这可能只是我遇到的限制? 有任何想法吗?

获取WPF窗口的最小化框

如何获取WPF窗口的最小化框单击事件?

如果一个操作数是可空类型,则Expression.GreaterThan失败,另一个操作数是非可空的

我正在创建一些动态linq并遇到以下exception的问题: 没有为类型’System.Nullable`1 [System.DateTime]’和’System.DateTime’定义二元运算符GreaterThanOrEqual 我明白了,因为我的字段类型是可以为空的,而且我实际上是在DateTime.Now中传递的。 所以在试图解决这个问题时我已经尝试过了 System.Nullable now; now = DateTime.Now; 但结果类型是一个不可为空的对象,因此仍然给我上述exception。 有什么建议?! 更新:为了进一步说明, now变量在设置时变为非可空类型,而不是保留为可空的DateTime,因此匹配会引发exception 更新:可以在CodePlex项目中看到实际代码: http://webquarters.codeplex.com/SourceControl/changeset/view/36529#574700 违规线约为145 fExp = Expression.GreaterThanOrEqual(fExpLeft, fExpRight);