Home » C# Programming

Line between draggable controls flickering

Hi all,

I am prototyping a project that required draggable controls with connecting lines.

So far I have used two buttons which i have made draggable and a connecting line, however the line doesn't appear straight and when it does it flickers when you move the mouse over the buttons or drag them. I have enabled double buffering on the form but that hasn't made a difference. 

Can anyone suggest why this might be and how to fix it, code and link below.

Thanks in advance.



I have zipped my project up (2010) here if anyone wants a look: http://dl.dropbox.com/u/889316/DraggableObjects.zip

My code is this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace ElementManagerTest
  publicpartialclass Form1 : Form
    bool mousemove;   // whether the mouse is being moved
    Point mouseorigin; // the original mouse location before the move startedpublic Form1()

    privatevoid button1_MouseDown(object sender, MouseEventArgs e)

    privatevoid button1_MouseMove(object sender, MouseEventArgs e)

    privatevoid button1_MouseUp(object sender, MouseEventArgs e)

    privatevoid ControlDown()
      if (mousemove == false)
        mousemove = true; // we are now moving a control
        mouseorigin = MousePosition;
        //controlorigin = this.Location;

    privatevoid ControlMove(Control cont)
      if ((mousemove == true) && (MousePosition != mouseorigin))
        cont.Left += (MousePosition.X - mouseorigin.X);
        cont.Top += (MousePosition.Y - mouseorigin.Y);
        mouseorigin = MousePosition;

    privatevoid UpdateLine()
      Pen pen = new Pen(Color.Blue, 2);
      Graphics g = this.splitContainer2.Panel1.CreateGraphics();
      g.SmoothingMode = SmoothingMode.HighQuality;
      g.DrawLine(pen, button1.Left + (button1.Width / 2), button1.Top + (button1.Height / 2), button2.Left + (button2.Width / 2), button2.Top + (button2.Height / 2));

    privatevoid ControlUp()
      mousemove = false;

    privatevoid button2_MouseMove(object sender, MouseEventArgs e)


2 Answers Found


Answer 1

Hi Grovesy:

Welcome to the MSDN Forum.

I downloaded your project and have fixed it. Please see this:

privatevoid UpdateLine()


    Graphics g = this.splitContainer2.Panel1.CreateGraphics();

Pen pen = newPen(Color.Blue, 2);

//this.Refresh();   --- this may cause flickers


    g.SmoothingMode = SmoothingMode.HighQuality;

    g.DrawLine(pen, button1.Left + (button1.Width / 2), button1.Top + (button1.Height / 2), button2.Left + (button2.Width / 2), button2.Top + (button2.Height / 2));



If you have any questions, please feel free to tell us.

Best Regards


Answer 2

Also, you should not use CreateGraphics in that manner.  Instead you should handle the Paint event of your splitContainer.Panel2 and do your drawing there.

See this link:



Hi Everybody ,  

I am facing a flickering issue when i am trying to refresh a chart control on the tick of a timer .I am listing

below the code of my application ,Please help me remove this problem so that chart control get refreshed without any flickers on the specified time interval

Thanks in advance.

 C# code:

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)
            MyLabel.Text = System.DateTime.Now.ToString();
            Txt1.Text = "Mohit";

    protected void Timer1_Tick(object sender, EventArgs e)

        Label1.Text = "Grid Refreshed at: " + DateTime.Now.ToLongTimeString();

    public void BindData()
        DateTime st = new DateTime(2010, 6, 1);
        int dys = 12;
        Dictionary<DateTime, long> dict = GetData(st, dys);

        foreach (DateTime d in dict.Keys)
            string xlab = d.ToString("M/d");

            DayOfWeek wd = d.DayOfWeek;
            if (wd == DayOfWeek.Sunday || wd == DayOfWeek.Saturday)
                Chart1.Series["weekends"].Points.AddXY(xlab, dict[d]);
                Chart1.Series["data"].Points.AddXY(xlab, 0);
                Chart1.Series["weekends"].Points.AddXY(xlab, 0);
                Chart1.Series["data"].Points.AddXY(xlab, dict[d]);

        //List<int> L1;
        //List<int> L2 = new List<int>();
        //Chart1.DataSource = new List<int>();
    private Dictionary<DateTime, long> GetData(DateTime stDt, int days)
        Dictionary<DateTime, long> gd = new Dictionary<DateTime, long>();
        Random r = new Random();
        DateTime wkDt = new DateTime(stDt.Year, stDt.Month, stDt.Day);
        long wkVal = 171;
        for (int i = 0; i < days; i++)
            gd.Add(wkDt, wkVal);

            wkDt = wkDt.AddDays(1);
            DayOfWeek wd = wkDt.DayOfWeek;
            if (wd != DayOfWeek.Sunday && wd != DayOfWeek.Saturday) wkVal -= r.Next(31);

        return gd;

    protected void Button1_Click(object sender, EventArgs e)


Asp.Net Code:- 

 <asp:ScriptManager ID="ScriptManager1" runat="server" />
       <asp:UpdatePanel ID="UpTimer" AssociatedUpdatePanelID="UpdatePanel1"  runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false"> 
       <ContentTemplate > 
        <asp:Timer ID="Timer1" runat="server" Interval="3000" OnTick="Timer1_Tick">

        <asp:UpdatePanel ID="UpdatePanel1"  UpdateMode="Conditional" runat="server" >


                <asp:AsyncPostBackTrigger ControlID="Timer1"  EventName="Tick" />



                <asp:Label ID="Label3" runat="server" Text="This is The Time when Chart will Referesh :" Font-Bold="true"></asp:Label> 

                <asp:Label ID="Label1" runat="server" Text="Chart not refreshed yet."></asp:Label><br />

                <asp:Label ID="Label4" runat="server" Text="(Chart Will Referesh after Every 30 Sec)" Font-Bold="true"></asp:Label> 

                <br /><br />
                    <div><asp:Chart ID="Chart1" runat="server" Height="400px" Width="550px">    
                    <asp:Series Name="weekends" ChartType="FastLine" Color="red" YValuesPerPoint="4">    
                    <asp:Series Name="data" ChartType="FastLine" Color="blue" YValuesPerPoint="4">    
                    <asp:ChartArea Name="ChartArea1">    
                        <Area3DStyle Enable3D="false"></Area3DStyle>    
                        <AxisX Interval="1"></AxisX>    
                 </asp:Chart>   </div> 


















I have built a windows form in Visual studio 2008.  The form loads and functions correctly.  On the form i have three rectangle shapes seperating various sections of the form.  when tabbing from text box to text box the rectangle shapes flicker.  i have set the double buffer on the form; does nothing.  the rectangles are purely for aesthetics and provide no functionality to the form.

any ideas on getting the flickering to stop?

I created a simple System.Windows.Forms.UserControl that does nothing but set this.DoubleBuffered = true and sets this.BackColor = Color.Plum.  I put it on my web page using an object tag like this:

<object id="simpleControl1" name="simpleControl1" classid="CLSID:3ea9993d-f99e-4bd9-b094-a65ae4e40ab3" width="100%" height="300"></object>

I then set the bgcolor of the body element to black.  It shows up fine.  But I am getting TERRIBLE flicker when I resize the page to make it smaller.

Is there any way to get around this page flicker?  Note that the user control isn't flickering, just the page.  Everything useful that I found searching around was on preventing the control from flickering.


Any help is appreciated.  Thanks!



Hello everyone,
Today I put a new problem. I'm trying to build an application with a form for smartphones with a size larger than normal. I need to increase the size of the form because I need many controls on one screen. Here you experience the following problem:

if you increase the size of the form I get the scroolbar side and moving to see the controls that are not visible in the form of all the controls are flashing (flickering) when moving.

I wanted to know if you can eliminate or reduce this flicker.

I program in VB.NET and I created a Windows Mobile 6

I await your response!



I have a form with 3 main buttons, and a panel . for each of the controls im showing in that panel different user controls with many controls on them doing different functions. Im trying to design my app to run in a single form each time different functionality .

My coding is kinda like that :

on button1 click

//im adding all my controls to that panel in the ctor, and for //each button click I use:privatevoid btnDiagnoseFiles_Click(object sender, EventArgs e)

the problem is, the whole content is flickering for each change. how do i do this correctly ?


I have  block of code that consists of a textbox a button and a dropdown. The text entered in the text box filters the dropdownlist when the user clicks the button. The Ajax works great when it is in a simple page but if I put the same code in a User control I get flicker. Can anyone assist with this issue?


I have a COleControl

I have an html page on which I put two instance of this viewer control.

but when I play video from two different sources . . videos get flicker.

Sometime one viewer's video is displayed in second viewer.

Any idea will be helpful.



I am using ESRI ArcGIS Server for a tile source and it all works correctly but there is a huge amount of flicker as the tiles get drawn in.  Watching with Fiddler it would appear that the silverlight control requests tiles from several levels up and stretches them to fit to get a fast initial draw.  As the subsequent layers come in, the streteched tile gets replaced with the new one.  As soon as there is any sort of transparent fill on the map the flickering is just horible.  Is there any way to get the silverlight control to draw like the ajax one and just fill in the correct tile as it is available without wasting time getting the wrong ones first?



I'm trying to solve this problem and would appreciate if anyone could give me a hand here. I've been looking throughout the web for a solution to this problem and have tried several possible ones to no success.

This is very easy to reproduce, create a simple Form, add a TabControl to it and a MonthCalendar to this TabControl's page. Now at this point resizing the form makes everything work fine, however if I set the Dock property of the TabControl to DockStyle.Fill when resizing the MonthCalendar starts to flicker.

Has anyone happened to notice this behaviour and knows how to handle it?

Thank you in advance.


 I am having a hard time with windows forms. I have solved most problems (though some can't be solved at all I guess), but there is one annoying thing - I have two listboxes, one listview and a label on the main form. The Listboxes are filled with quite a lot rows. When I start the application and press tab or alt or virtually any other key, the listboxes and sometimes the labels flash for a short time. It looks like they are redrawn or something like that, but it's not because of my code. The listboxes don't even have focus - but they still do the flicker.

The strange thing is that the flicker happens only when you hit a key the first time, then the flicker disappears for a moment. Showing a panel in front of the form will "revive" the flicker again. It's just quite random, when you let the application rest for a moment and press a button - then the controls will flash again.

Again - just pressing a button without having focus on the form makes the flicker. I am using a image background - if that has any impact...

PS. If anyone knows a simple way how to make the listbox scrollbar thinner, please let me know...

Thank you for your help.


I´m developing a simple winform application. The application consist in one form divided in 2 split panels (3 areas) an it works like a web frame base application. You have a menu in the left "frame" and when you click in one menu element the aplication changes the other two frames. To make this possible i add and remove users controls in the "frames". I follow the next steps:

- create the new control which im going to put inside the frame.
Control newControl = new CustomizedControl();

- remove the last control inside the frame.

- add the new control inside the panel.

When i do this the application suffer a flickering problem. I tryed to solve this using double buffer inside the controls and the main Form. To be more exactyl i´ve use the following line after InitializeComponent() function:
this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);

But that line has not solved the flickering problem.

Which is the better way to implement this kind behaviour? Could anyone please help me ?.

Thanks in advance.

I am using popup as a dialog box, this popup must be draggable.
so i used popup mouse event handlers(mouse down,move,up) to do this.
as this pop up is small, when i drag this popup faster, mosemove event is not fired.


I have a webpage with jQuery UI draggable on a div. I've tested in IE6, IE8, Firefox, Chrome.

The dragging works in all cases.

For IE9 beta, the dragging works at screen resolution of 1600 x 1200 on a friend's computer.

On my laptop with a 1366 x 768 resolution, dragging the div does not work.

Is this an IE9 bug?

Also what was strange: I had a couple of default stylesheets and then some alternate stylesheets. One of the background-images did not load from the default stylesheet
on my friend's pc.


Hi Team,

I found a strange problem with Popup in XBAP, while I tried to drag move the popup over the browser. The Popup reduced in size gradually and finally gone. :(

You can find a ready to run sample here which shows the error.


Thanks in Advance.


Regards, Jawahar

Hello All:

I am currently building a map control, using Bing Maps. The problem I am running into: the map has the ability to create a route and it draws a line from point A to point B. However, I was reading that Bing Maps now allows dragging the route line to another spot to stop at. I see that Bing Maps website has this capability. But I can't seem to find any suggestions on how to do this through the API in custom maps. Can anyone point me in the right direction?


Thank You,



I'm looking for a way to show the motion of a push-pin being dragged over the map. I've tried to modify an example proposed here: http://peteohanlon.wordpress.com/2010/10/10/draggable-pushpins/ by updating the AssociatedObject.Location in delegate added to MouseMove event handler, but this bring no results. The push-pin remains at it's place till the moment when the mouse button is released. Then it jumps to the new location.

The situation is different when I use the same behavior on a push-pin which is hard-coded into XAML. The approach works just as expected - the push-pin moves along with the mouse pointer. So probably the usage of binded push-pin (through MapItemsControl-ItemTemplate-DataTemplate) is to blame. It seems that when a Pushpin is wrapped with ContentPresenter the animation is not possible.

Any idea how can I force the MapLayer to keep track of the push-pin location during the drag and redraw it properly as mouse moves?


Hii all 

i am writing a  VB code that read from txt file line by line 

i need the app to preview the line in textbox in time interval of 1 s (can be change)  and when it will reach to the end it close the text file and and reread it after let say 1 m ....please help me out in this problem ... i write the code but it only show me the first line in text box. 


PrivateSub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
 Dim myStream As Stream = NothingDim openFileDialog1 AsNew OpenFileDialog()

 openFileDialog1.InitialDirectory = "C:\Users\Dubai-User\Desktop"
 openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
 openFileDialog1.FilterIndex = 2
 openFileDialog1.RestoreDirectory = True
 TextBox1.Text = openFileDialog1.FileName
 EndSubPrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 Dim openFileDialog1 AsNew OpenFileDialog()
 'Pass the file path and the file name to the StreamReader constructor.If TextBox1.Text = ""Then
  MsgBox("please browes for file location")
 objStreamReader = New StreamReader(TextBox1.Text)

 'Read the first line of text.
 strLine = objStreamReader.ReadLine

 EndSubPrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

 EndSubPrivateSub Timer3_tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick

 DoWhileNot strLine IsNothing'Write the line to the Console window.
  tDebug.Text = strLine
  'Read the next line.
  strLine = objStreamReader.ReadLine
 Loop'Close the file.




private void gridView_Paint(object sender, PaintEventArgs e)
            Graphics g = e.Graphics;
            Pen myPen = new Pen(Color.Red, 0.1F);


private void gridView_Paint(object sender, PaintEventArgs e)
            Graphics g = e.Graphics;
            Pen myPen = new Pen(Color.Red, 0.1F);
            PointF[] points=
            new PointF (0.0F, 300.0F),
            new PointF (355.0F, 300.0F)
            g.DrawLines(myPen, points);


1. I want to move the line to be in the center and to be start from 0 on axis Y.

2. I want to add more like 14 lines exactly the same as this line with equal spaces between them lets say the space betwee neach line should be like 24 pixels.


Thanks for the help.


How do you add a Trend Line to an ASP.NET 3.5 Chart Controls line graph?

<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure