As I said in the last thread about this subject, OnPaint()
and UserPaint
don't work with ListView. The painting is handled by the underlying control and cannot be intercepted in that fashion. This is different to other Controls
So, when ControlStyles.UserPaint
is true
, the underlying control is not told to redraw itself. Instead, all drawing is routed to the OnPaintBackground()
and OnPaint()
methods, which -- as you have found -- do nothing.
There are two ways to do what you asked (the second is better than the first):
First way: Intercept the WM_PAINT
, do the base processing, and then draw onto the listview. Something like this:
public class MyListView : ListView
{
protected override void WndProc(ref Message m) {
switch (m.Msg) {
case 0x0F: // WM_PAINT
this.HandlePaint(ref m);
break;
default:
base.WndProc(ref m);
break;
}
}
protected virtual void HandlePaint(ref Message m) {
base.WndProc(ref m);
using (Graphics g = this.CreateGraphics()) {
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;
g.DrawString("Some text", new Font("Tahoma", 13),
SystemBrushes.ControlDark, this.ClientRectangle, sf);
}
}
}
But this gives repaint problems when what you draw is outside the area that the listview thinks holds the control's contents -- it doesn't trigger paint events.
Second manner: Intercept the CustomDraw notification (this is not the same as OwnerDraw), and listen for the CDDS_POSTPAINT
stage. In that stage you can safely draw onto the list view. You can look at the code of ObjectListView to see how it is done.
You could also just save yourself a lot of bother and use an ObjectListView directly :)
An official example notebook demonstrating local file upload/download and integration with Drive and sheets is available here:
https://colab.research.google.com/notebooks/io.ipynb
The simplest way to share files is to mount your Google Drive.
To do this, run the following in a code cell:
from google.colab import drive
drive.mount('/content/drive')
It will ask you to visit a link to ALLOW "Google Files Stream" to access your drive. After that a long alphanumeric auth code will be shown that needs to be entered in your Colab's notebook.
Afterward, your Drive files will be mounted and you can browse them with the file browser in the side panel.
Here's a full example notebook
Best Answer
easiest way I found is mounting google drive in colab:
then use '/content/gdrive/My Drive/' as prefix of the file path. suppose you have a text file in data directory of you google drive. Then you can access it with following code: