Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Graphics.DrawLine(...) over a listView

Status
Not open for further replies.

arbin

Programmer
Jan 9, 2006
4
SE
Hello

I wan't to draw a line shown over a listView in a panel. Please help me, I can draw a line on the panel, but when I add the listView the line disapears (it is probobly under the listView). How do I get it to be shown in front of the listView?
 
You probably need to override the OnPaint method, and in there, call the base.OnPaint, and then call your method that draws the line - otherwise, when you draw the line over the listview, it becomes invalidated, and it's onpaint gets called again, drawing itself back on top of the line.

In order to override the OnPaint method for a base class though, you may have to create your own listview class and inherit from the system.windows.forms.listview class, and then define your override in this class.

So it might look something like this

Code:
namespace MyApp
{
		public class MyListView: System.Windows.Forms.ListView
	{
		
		private System.ComponentModel.Container components = null;

		public MyListView()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();
		}
protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint(e);
			DrawLine();  //Method to do your drawing
		}
     }
}

I have never done this with base classes, but I have done something like this with user controls that I have created, and I think it'll work. Perhaps there is a better way? Please jump in if you have a solution!
 
ListView contains ListViewItems. So unless your listview contains no items, you will not see the graphics.

For each item that you add to the listview - point its Paint event to your paint event and you should start to see results.

Example:
ListViewItem item = lstMyList.Items.Add("Hi");
item.Paint += new PaintEventHandler(lstitem_paint);
 
I dont manage to execute the OnPaint method, I have tried with Invalidate() and this.SetStyle(ControlStyles.ResizeRedraw, true); But the OnPaint method doesen't get called.

class ListViewGuide : ListView
{
private System.ComponentModel.Container components = null;

public ListViewGuide(Panel targetPanel)
{
this.SetStyle(ControlStyles.ResizeRedraw, true);
targetPanel.Controls.Add(this);
InitializeComponent();
}

private void InitializeComponent()
{
this.GridLines = true;
this.Dock = DockStyle.Top;
this.HeaderStyle = ColumnHeaderStyle.None;
//this.Name = item.ChannelName;
this.Scrollable = false;
this.Size = new System.Drawing.Size(452, 40);
this.View = View.Details;
this.BringToFront();

}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

protected override void OnPaint(PaintEventArgs e)
{

base.OnPaint(e);
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Red);
e.Graphics.DrawLine(pen, 0, 0, 200, 200);
//DrawLine();
}
}
 
arbin,

Is it possible that you need to draw your line first, then call the base.onpaint(e)?

Try drawing your line first, then calling base.onpaint and let me know what happens.

I went back and looked at some of my code, and that is how I had it set up

Code:
protected override void OnPaint(PaintEventArgs e)
		{
			AnimateBoxes(e);
			base.OnPaint(e);
		}


private void AnimateBoxes(PaintEventArgs e)		//Draws Red border around active selection method controls
	{
	graphics = e.Graphics;
	Pen penCurrent = new Pen(Color.Red);

	switch (SampleType)
	{
	case 1:
	graphics.DrawRectangle(penCurrent, new Rectangle(this.TimeRangeStart.Location.X-4, this.TimeRangeStart.Location.Y-20, 314, 50));
					this.lblEndTime.Text="Time Range End:";
	break;
	case 2:
					graphics.DrawRectangle(penCurrent, new Rectangle(this.TimeRangeEnd.Location.X-4,this.TimeRangeEnd.Location.Y-20, 228, 50));
					this.lblEndTime.Text="Time Range End:";
	break;
	}
	graphics.Dispose();
	penCurrent.Dispose();
}
 
Oh, and don't forget that you have to use YOUR ListView class (ListViewGuide), not the base class on the forms. I'm not sure what level of programmer you are, so I don't want to talk down to you here - just let me know if you need more help. This would explain why you aren't seeing the OnPaint fire if that were the case.
 
I have tried to draw it before base.OnPaint(e), but with no success.

I am using my ListViewGuide
ListViewGuide lvg = new ListViewGuide(panelCenter);

I am realy confused why this dont work
 
Solved it!


ListView control is just a wrapper around the control ComCtl and it doesn't fire the OnPaint event.

Instead I needed to override the WndProc event and look for the message 20 (means redraw backgroun)

To force a redraw I use Invalidate()

Thanks anyway
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top