相关疑难解决方法(0)

你最喜欢的C#扩展方法是什么?(codeplex.com/extensionoverflow)

让我们列出你发布优秀和最喜欢的扩展方法的答案.

要求是必须发布完整代码并提供示例和如何使用它的说明.

基于对该主题的高度关注,我在Codeplex上设置了一个名为extensionoverflow的开源项目.

请标记您的答案并接受将代码放入Codeplex项目中.

请发布完整的源代码而不是链接.

Codeplex新闻:

24.08.2010 Codeplex页面现在在这里:http://extensionoverflow.codeplex.com/

11.11.2008 XmlSerialize/XmlDeserialize现在已实现并且已经过单元测试.

11.11.2008仍有更多开发人员的空间.;-) 现在加入!

11.11.2008第三个贡献者加入了ExtensionOverflow,欢迎来到BKristensen

11.11.2008 FormatWith现在已实现且已经过单元测试.

09.11.2008第二个贡献者加入了ExtensionOverflow.欢迎来到chakrit.

09.11.2008我们需要更多开发人员.;-)

09.11.2008 ThrowIfArgumentIsNull现在在Codeplex上实现单元测试.

.net c# extension-methods open-source

478
推荐指数
57
解决办法
12万
查看次数

如何防止Windows窗体TextBox在调整大小时闪烁?

有很多文章解决Windows窗体中的闪烁问题.大多数人建议设置DoubleBuffered = true或设置一堆ControlStyle标志.但是,这些都不能帮助减少TextBox闪烁.

以下是一些相关问题:

要重现该问题,请创建一个新的WinForms项目,添加一个TextBox,启用多行,禁用自动换行,添加一堆文本,设置Anchor为Left + Right + Top + Bottom.现在运行并调整大小.文字闪烁.对于几个嵌套TableLayoutPanels中的文本框,调整大小时的闪烁更糟.

应用上述问题中提出的解决方案最好不要修复闪烁; 如果我得到实验并设置受保护ControlStyle,TextBox我可以完全打破它(通过启用UserPaint),但不能消除闪烁.

那么,有没有办法解决TextBox中文本的闪烁问题?

.net flicker winforms

17
推荐指数
2
解决办法
1万
查看次数

调整ListView列以适应WinForms

我有面对listview列的调整大小问题.如果您将listview 锚定/停靠到正常winform而不是listview锚点或对接工作正常.我的意思是listview将调整大小并适合winforms,因为winforms最大化但是你设计的列没有使用listview调整大小.

我的问题是:有没有办法用listview调整listview列以适应winform大小?

Listview设计代码:

 private void Form1_Load(object sender, EventArgs e)
    {

        listView1.View = View.Details;
        listView1.LabelEdit = true;
        listView1.BackColor = Color.GreenYellow;
        listView1.Columns.Add("Date", 100, HorizontalAlignment.Left);
        listView1.Columns.Add("TransID", 50, HorizontalAlignment.Left);
        listView1.Columns.Add("voucher", 100, HorizontalAlignment.Right);
        listView1.Columns.Add("particulars", 300, HorizontalAlignment.Left);
        listView1.Columns.Add("deposit", 100, HorizontalAlignment.Right);
        listView1.Columns.Add("withdrawal", 100, HorizontalAlignment.Right);

        string connstr = "server=.;initial catalog=DataBase;uid=UID;pwd=PWD";
        SqlConnection con = new SqlConnection(connstr);
        con.Open();
        listView1.Items.Clear();
        listView1.Refresh();
        string sql = "select date=convert(varchar,date,103),transID,max(particulars)as particulars,sum(deposit)as deposit,sum(withdrawal) as withdrawal,voucher from debank group by date,transID,voucher";
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataAdapter dap = new SqlDataAdapter(cmd);
        DataSet ds …
Run Code Online (Sandbox Code Playgroud)

c# listview winforms

12
推荐指数
3
解决办法
6万
查看次数

Win32:如何自定义绘制编辑控件?

我需要实现EM_SETCUEBANNER的功能,其中文本提示出现在Edit控件中:

编辑控件中的提示横幅示例

问题是我无法使用通用控件的第6版,这是获得Microsoft提供的cue横幅实现所需的.

我已经研究过简单地改变编辑控件的文本和字体格式

Dark Gray Italic Text
Run Code Online (Sandbox Code Playgroud)

但它会抛出Change事件(由更高的组件库提供的组件包装器),我无法找到避免的方法.

所以我改为自定义绘制文本,在控件未聚焦且为空时绘制Cue Banner文本,否则依赖默认绘制.

Edit控件不能很好地公开自定义绘图机制,如ListView,TreeView和其他提供的.

其他人已经研究过它,但这似乎是一项几乎不可能完成的任务:

从事情的方式来看,我将不得不处理以下消息:

  • WM_ERASEBKGND,WM_PAINT(原因很明显)
  • WM_SETFOCUS,WM_KILLFOCUS(防止白条显示 - 如上所述)
  • WM_CHAR(处理和更新控件中的文本)

而且我还需要找到一种在控件中显示插入符号的方法,因为我还没有找到一种方法允许Windows为我这样做而不用画我提到的白条.

这将会非常好玩.:翻白眼:

鉴于Windows Edit控件从未打算自定义绘制:有没有人知道如何自定义绘制Windows编辑控件?


注意:我也会接受解决问题的答案,而不是回答我的问题.但是其他任何想要自定义绘制编辑控件的人,遇到这个问题,都可能会有一个答案.

windows winapi custom-draw

11
推荐指数
3
解决办法
1万
查看次数

什么可能导致双缓冲杀死我的应用程序?

我有一些使用GDI +绘制到屏幕的自定义(winforms)组件.

为了防止重绘时出现闪烁,我决定启用双缓冲,所以我在构造函数中添加了一行:

public ColourWheel()
{
    InitializeComponent();
    this.DoubleBuffered = true;
}
Run Code Online (Sandbox Code Playgroud)

哪个适用于此组件(ColourWheel).当我将相同的行添加到我的另外两个(结构相似的)组件的构造函数中时,我会得到一些奇怪的症状:

  1. 当我尝试运行组件打开的表单时,我得到一个Argument Exception Application.Run(new Form());.
  2. 如果我切换到设计模式,我得到一个错误,关于具有未处理异常的组件与参数.

我是否对其中一个或全部进行双缓冲似乎并不重要,它仍然适用于ColourWheel,但不适用于其他.

为了记录,我还尝试了一些其他的 缓冲技术.

什么可能导致双缓冲在一个组件上工作,而不是其他组件?


编辑:这是运行时症状的异常细节:

System.ArgumentException未处理Message = Parameter无效.Source = System.Drawing StackTrace:System.Drawing.Graphics.GetHdc(),位于System.Windows.Forms.Control的System.Drawing.BufferedGraphics.Render()的System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC,BufferedGraphics缓冲区)中.系统中System.Windows.Forms.UserControl.WndProc(Message&m)的System.Windows.Forms.ScrollableControl.WndProc(Message&m)处的System.Windows.Forms.Control.WndProc(Message&m)处的.WmPaint(Message&m) .Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam, IntPtr lparam)在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)处于System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)at at System.Windows.Forms.Application.ThreadContext.Run MessageLoopInner(Int32 reason,ApplicationContext context),位于TestForm.Program.Main()的System.Windows.Forms.Application.Run(Form mainForm)的System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)中D:\ Documents and Settings\Tom Wright\My Documents\Visual Studio 2010\Projects\ColourPicker\TestForm\Program.cs:在System.AppDomain.ExecuteAssembly的System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly,String [] args)的第18行(String assemblyFile,Evidence assemblySecurity,String [] args)在System.Threading.ExecutionContext.Run的System.Threading.ThreadHelper.ThreadStart_Context(Object state)中的Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()(ExecutionContext executionContext,ContextCallback callback) System.Threading.ThreadHelper.ThreadStart()InnerException上的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态),对象状态,布尔值ignoreSyncCtx):


编辑2:导致问题的两个组件中的一个(更复杂)的OnPaint处理程序:

private void ValueSlider_Paint(object sender, PaintEventArgs e)
{ …
Run Code Online (Sandbox Code Playgroud)

c# components gdi+ double-buffering

8
推荐指数
1
解决办法
1694
查看次数

如何在不滚动和丢失选择的情况下将文本附加到RichTextBox?

我需要将文本附加到RichTextBox,并且需要在不使文本框滚动或丢失当前文本选择的情况下执行它,是否可能?

.net c# winforms

7
推荐指数
1
解决办法
7348
查看次数

如何使用许多控件加载窗体在Winform上平滑?

我在表单上有一个带有DataGridView的C#程序.

当用户选择一行并按下按钮时,我向控制面板显示一些从DataGridView填充的文本框.

当显示控制面板时,我看到屏幕上的控件构建,并且它不悦目.

如何立即显示此控制面板?

提前致谢

c# winforms

6
推荐指数
1
解决办法
8467
查看次数

自定义控件中的双缓冲子控件(C#)

我想双缓冲包含按钮的自定义控件.我已经尝试了各种方法来双重缓冲控件; SetStyle,BufferedGraphicsContext和绘制到位图.这些都适用于控件的自定义绘图,但它们都不会处理将按钮绘制到后台缓冲区.我该如何实现这一目标?

c# drawing doublebuffered winforms

5
推荐指数
1
解决办法
2941
查看次数

双缓冲C#

我正在尝试实现以下方法:void Ball :: DrawOn(Graphics g);

该方法应该绘制球的所有先前位置(存储在队列中)并最终绘制当前位置.我不知道这是否重要,但我使用g.DillEllipse(...)打印以前的位置,使用g.FillEllipse(...)打印当前位置.

问题是,你可以想象有很多绘图要做,因此显示开始闪烁很多.我曾寻找过双缓冲的方法,但我能找到的就是这两种方式:

1)System.Windows.Forms.Control.DoubleBuffered = true;

2)SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint,true);

在尝试使用第一个时,我得到一个错误,解释说在此方法中,由于其保护级别,属性DoubleBuffered无法访问.虽然我无法想象如何使用SetStyle方法.

是否有可能将缓冲区加倍,而我拥有的所有访问权限都是我在方法中输入的图形对象?

提前致谢,

编辑:我创建了以下类

namespace doubleBuffer
{
    class BufferedBall : System.Windows.Forms.Form{
        private Ball ball;
        public BufferedBall(Ball ball)
        {
            this.ball = ball;
        }

    public void DrawOn(Graphics g){
        this.DoubleBuffered = true;
        int num = 0;
        Rectangle drawArea1 = new Rectangle(5, 35, 30, 100);
        LinearGradientBrush linearBrush1 =
        new LinearGradientBrush(drawArea1, Color.Green, Color.Orange, LinearGradientMode.Horizontal);
        Rectangle drawArea2 = new Rectangle(5, 35, 30, 100);
        LinearGradientBrush linearBrush2 =
           new LinearGradientBrush(drawArea2, Color.Black, …
Run Code Online (Sandbox Code Playgroud)

c# graphics doublebuffered

5
推荐指数
1
解决办法
7252
查看次数

Windows 7上的C#Winform网格渲染速度很慢

我注意到我的Windows 7 64位机器上的C#winform datagrid非常慢.对于具有1000行+足够的列/文本以适应屏幕宽度的标准网格,我看到滚动显着的渲染延迟(即滚动/滚动条移动滞后约0.5秒而不是平滑).当最大化到全屏时网格特别慢并且随着显示尺寸减小而变得更快.

通过将DataTable绑定到DataGridView实例,GUI是一个简单的设置; 我已经研究了双缓冲等常见的罪魁祸首并没有看到太多改进.该机器在nvidia quadro nvs 420上采用Xeon四核和2 x 23英寸屏幕赢得了64位.

任何人都知道为什么会这样吗?

c# performance winforms

5
推荐指数
2
解决办法
2738
查看次数

c#使用gdi32.dll和System.Drawing.Graphics绘图

所以我有一些代码在gdi32.dll的图片框顶部创建一个高亮效果,我想知道是否有更简单的方法来做到这一点System.Drawing.Graphics?基本上使用gdi32.dll,我必须在绘制后捕获屏幕截图,将其发布到我的图片框然后我能够绘制更多的东西并更改我使用的笔的颜色.如果我只是尝试更改笔的粗细和颜色并再次在屏幕上绘图,如果我改变了已经绘制的内容.

现在我有一个使用System.Drawing.Graphics和大量数学的版本FillPolygon但如果我画了一个我已经画过的区域,它只会使我画的区域更暗.只要你还没有使用鼠标遮住区域,它就不能用gdi32.dll进行justr阴影处理.有什么建议?

public partial class Form9 : Form
{
    private bool is_mouse_down { get; set; } // Will check if the mouse is down or not.

    private Color Pen_Color = new Color();

    private int Pen_Type { get; set; }

    private int Thickness { get; set; }

    private bool Start { get; set; }

    List<Point> Points = new List<Point>();

    public Form9()
    {
        InitializeComponent();

        pictureBox1.Dock = DockStyle.Fill;

        Pen_Color = Color.Blue;
        Pen_Type = 13; // Type = …
Run Code Online (Sandbox Code Playgroud)

c# system.drawing gdi graphic

4
推荐指数
1
解决办法
1208
查看次数

使用ownerdraw和virtualmode在listview中闪烁

我正在使用listview控件并设置以下参数:

        this.listView1.BackColor = System.Drawing.Color.Gainsboro;
        this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnHeader1,
        this.columnHeader2});
        this.listView1.FullRowSelect = true;
        this.listView1.HideSelection = false;
        this.listView1.Location = new System.Drawing.Point(67, 192);
        this.listView1.Name = "listView1";
        this.listView1.Size = new System.Drawing.Size(438, 236);
        this.listView1.TabIndex = 0;
        this.listView1.UseCompatibleStateImageBehavior = false;
        this.listView1.View = System.Windows.Forms.View.Details;
        this.listView1.DrawColumnHeader += new System.Windows.Forms.DrawListViewColumnHeaderEventHandler(this.listView1_DrawColumnHeader);
        this.listView1.RetrieveVirtualItem += new System.Windows.Forms.RetrieveVirtualItemEventHandler(this.listView1_RetrieveVirtualItem);
        this.listView1.DrawSubItem += new System.Windows.Forms.DrawListViewSubItemEventHandler(this.listView1_DrawSubItem);
Run Code Online (Sandbox Code Playgroud)

提供两行随机文本.拥有者绘图很简单:

    private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            e.DrawBackground();
            e.DrawText();                
        }
        else
            e.DrawDefault = true;
        //Console.WriteLine("{0}\t\tBounds:{1}\tItem:{2}\tSubitem:{3}", (i++).ToString(), e.Bounds.ToString(), e.Item, e.SubItem);
    }
Run Code Online (Sandbox Code Playgroud)

问题是:当我将鼠标悬停在listview的内容上时,我会看到第一列的闪烁.调试显示DrawSubItem在鼠标悬停时不断调用.

是bug吗?如何避免这种行为?

c# listview winforms

2
推荐指数
1
解决办法
5375
查看次数

启用双缓冲

我已经看到以下代码在winform上启用双缓冲:

// Activates double buffering 
this.SetStyle(ControlStyles.DoubleBuffer |
   ControlStyles.OptimizedDoubleBuffer |
   ControlStyles.UserPaint |
   ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
Run Code Online (Sandbox Code Playgroud)

这与设置Form.DoubleBuffering = true有什么不同?

doublebuffered visual-studio-2008 winforms

1
推荐指数
1
解决办法
3907
查看次数