Focus on a custom listview

D

Daisy

I've started writing a listview, which currently draws my list to a pane,
and onclick, works out which row you've clicked on, from the co-ordinates of
the mouse, sets the .Selected property to the index of the item in my list,
and onpaint, I draw this with a selected box over it.

It's dawned on me, that this "list", being just text written to the pane,
won't be able to recieve focus, and therefore, there's probably a better way
of doing this, where each row is a "thing", within my control... Would I be
right? And if so, can someone point me in the right direction of a
sample/doc about it? :blush:)
 
C

Chris Capel

You should forget the pane. Use a UserControl instead. Make a class like
this:

class MyList : UserControl {
....
}

This will have its own Control collection. So next, do this:

class MyListItem : UserControl {
....
}

This will have its own drawing code and logic. Your AddItem method will look
something like this:

class MyList : UserControl {
public void AddItem(string text) {
this.Controls.Add(new MyListItem(text));
}
}

Each of these MyListItem things will be a full control, able to receive
focus and everything. You can make them internal, so that the end user won't
be able to see them, and maybe make another class 'MyListItemPublic' that
you let the end user use to access it. Does this give you enough of a lead?

Chris
 
D

Daisy

Chris Capel said:
You should forget the pane. Use a UserControl instead. Make a class like
this:

class MyList : UserControl {
...
}

That's what I've got, it's a class inside a control :))

This will have its own Control collection. So next, do this:

class MyListItem : UserControl {
...
}

This will have its own drawing code and logic. Your AddItem method will look
something like this:

Now that, I didn't think of. I was drawing the whole lot in my one control.

Ok, few questions :)

Firstly, wouldn't this make the tab key go through them all? This isn't how
windows works, I tab to a list, and use the cursor keys to move about...?

Secondly, I'll be "selecting" items. Since my list items are just gonne be a
row of text (I guess the onpaint method will just DrawString() for each
column at the correct ypos), should I just lash a blue box over them
OnPaint, or is there a better way (such as defining how a control with
focus, should look?)

And thirdly... Resizable columns...! Say I've got my headings (possibly
buttons, possibly just another control drawn from rectangles & 3d borders),
what's trhe best way to do this. I imagined onclick, checking the xpos to
find the end of a column heading within a few pixels, change the cursor to a
resize, and onmouseup, resize the column, however Windows does cleverer
things, such as running a line down the panel with the cursor - is there an
easy way to do that, which wouldn't involve a load of repaints?

Thanks :blush:)
 
D

Daisy

Chris Capel said:
You should forget the pane. Use a UserControl instead. Make a class like
this:

class MyList : UserControl {
...
}

This will have its own Control collection. So next, do this:

class MyListItem : UserControl {
...
}

This will have its own drawing code and logic. Your AddItem method will look
something like this:

class MyList : UserControl {
public void AddItem(string text) {
this.Controls.Add(new MyListItem(text));
}
}

Each of these MyListItem things will be a full control, able to receive
focus and everything. You can make them internal, so that the end user won't
be able to see them, and maybe make another class 'MyListItemPublic' that
you let the end user use to access it. Does this give you enough of a
lead?

Ok, I went ahead with your suggestion. To test the speed, vs the way I was
doing it, I set it to create 5000 items in the list. Well, that didn't work.
It took about 30 seconds to create the 5000 controls, and attach them to the
parent control. Redrawing was fine (I wrote code to Hide/Show controls,
depending on the scroll position), but there's no way I can use it as is.
It's an NNTP client I'm writing, and if someone downloads 3000 new posts, I
can't have the GUI lock up while it adds controls...

Please tell me I'm doing something wrong, because the way I did it
originally, although I was drawing my own select rectangle, and working out
what was clicked from co-ordinates, it was lightning fast to draw :-\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top