473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

ItemCollections and custom UserControl...

Hello all,
[I've been programming in C# for several months now, so bare with me... I'm
still a newbie]

I've create a custom control (UserControl) and have a custom Item
Collection. The control is a custom calendar which is draw using the
Graphics Rectangle etc. functions. It is drawn when the control is painted
or resized. When the control is drawn it draws also the items found in the
collection.

So far so good.... I have 3 questions which I'm unable to find a solution
for yet (googled for several days now :-( ). Hopefull someone of you can
help me?

1. With a ListView control you can remove items using the
listView1.Items[1].Remove() or listView1.Remove(item). How does one
implement the first option (the second one I know how)?

2. The UserControl is drawn when the control is painted or resized (in the
event I call the function that draws the control). Every time I add a item
to the collection I have to call the control.Refresh() function to update my
control with the added item. How can I make it so that whenever I add an
item to my collection the control is (re)drawn automatically?

3. The drawn control flickers when I do a Refresh() or when I redraw the
control. I've already tried to remove the Graphics.Clear(Color) function but
that did not help :-( How can I reduce the flickering?

Below I placed the item collection code (simplified). Hopefully someone in
this newsgroup can tell me what to do with the first 2 questions.... they
are really bugging me.

Thanks in advance!
Regards,
Tinus
(The Netherlands)
----
public class Items : System.Collections.CollectionBase
{
public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();

this.List.Add(item);

return this;
}

public virtual Items Add(string name, int number)
{
return this.Add(new Data(name, number));
}

public virtual Item this[int index]
{
get
{
if (index < 0 || index > this.List.Count)
throw new ArgumentOutOfRangeException();
return (Item)this.List[index];
}
}
}

public class Item
{
private string name;
private int number;

public string Name
{
get
{
return this.name;
}
}

public int Number
{
get
{
return this.number;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
this.number = value;
}
}

public Item(string name, int number)
{
this.name = name;
this.number = number;
}
}
Nov 16 '05 #1
8 3146
Hi Tinus,

First of all, if you're creating an owner-drawn custom calender (drawing
using the graphics rectangle etc), it's better to derive your control from
the Control class instead of UserControl. UserControl is best suited if you
want to combine several basic/constituent controls into a single logical
unit.

As to your questions:
1. The 'Items[1].Remove()' expression is basically calling the Remove()
method of the Item object returned by the indexer. So you have to implement
a Remove() method in your Item class. You can just call the Remove(item)
method of the Items collection from the Remove() method of the Item class.
It's not hard to implement this. I'll leave the detail for your exercise.
Hint: you may need to define a private field in the Items collection and
Item class to point to the their owner (i.e. an instance of your control).

2. After adding the item to the collection, just call the Invalidate()
method of your control.

3. There are several techniques in reducing flicker. One of them is a
technique called 'Double Buffering'. You can just do some googling to learn
more about double buffering. One site that you may find useful is
http://www.codeproject.com/cs/media/...reeDrawing.asp.

Hope it helps. Good luck.

-- Ricky Lee
============================================
^o^ "When all doors are closed, God will open a Windows" ^o^
============================================

"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in message
news:Ov**************@TK2MSFTNGP15.phx.gbl...
Hello all,
[I've been programming in C# for several months now, so bare with me... I'm still a newbie]

I've create a custom control (UserControl) and have a custom Item
Collection. The control is a custom calendar which is draw using the
Graphics Rectangle etc. functions. It is drawn when the control is painted
or resized. When the control is drawn it draws also the items found in the
collection.

So far so good.... I have 3 questions which I'm unable to find a solution
for yet (googled for several days now :-( ). Hopefull someone of you can
help me?

1. With a ListView control you can remove items using the
listView1.Items[1].Remove() or listView1.Remove(item). How does one
implement the first option (the second one I know how)?

2. The UserControl is drawn when the control is painted or resized (in the
event I call the function that draws the control). Every time I add a item
to the collection I have to call the control.Refresh() function to update my control with the added item. How can I make it so that whenever I add an
item to my collection the control is (re)drawn automatically?

3. The drawn control flickers when I do a Refresh() or when I redraw the
control. I've already tried to remove the Graphics.Clear(Color) function but that did not help :-( How can I reduce the flickering?

Below I placed the item collection code (simplified). Hopefully someone in
this newsgroup can tell me what to do with the first 2 questions.... they
are really bugging me.

Thanks in advance!
Regards,
Tinus
(The Netherlands)
----
public class Items : System.Collections.CollectionBase
{
public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();

this.List.Add(item);

return this;
}

public virtual Items Add(string name, int number)
{
return this.Add(new Data(name, number));
}

public virtual Item this[int index]
{
get
{
if (index < 0 || index > this.List.Count)
throw new ArgumentOutOfRangeException();
return (Item)this.List[index];
}
}
}

public class Item
{
private string name;
private int number;

public string Name
{
get
{
return this.name;
}
}

public int Number
{
get
{
return this.number;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
this.number = value;
}
}

public Item(string name, int number)
{
this.name = name;
this.number = number;
}
}

Nov 16 '05 #2
Thanks Ricky!

Question 1 is now solved.... and it was so easy! But I really needed the
hint ;-)

As for question 2; How can I Invalidate() my control from either the Item
class of Items class? If I just make an instance to my control and then
Invalidate this instance then the results are: nothing happens. This is
because the instance does not have any items in it I think.

So can you once again give me one of your helpfull hints?

Also thanks for the flickerfree link... I'm going to look at it today.

Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42**********************@news.wanadoo.nl...
Hi Tinus,

First of all, if you're creating an owner-drawn custom calender (drawing
using the graphics rectangle etc), it's better to derive your control from
the Control class instead of UserControl. UserControl is best suited if
you
want to combine several basic/constituent controls into a single logical
unit.

As to your questions:
1. The 'Items[1].Remove()' expression is basically calling the Remove()
method of the Item object returned by the indexer. So you have to
implement
a Remove() method in your Item class. You can just call the Remove(item)
method of the Items collection from the Remove() method of the Item class.
It's not hard to implement this. I'll leave the detail for your exercise.
Hint: you may need to define a private field in the Items collection and
Item class to point to the their owner (i.e. an instance of your control).

2. After adding the item to the collection, just call the Invalidate()
method of your control.

3. There are several techniques in reducing flicker. One of them is a
technique called 'Double Buffering'. You can just do some googling to
learn
more about double buffering. One site that you may find useful is
http://www.codeproject.com/cs/media/...reeDrawing.asp.

Hope it helps. Good luck.

-- Ricky Lee
============================================
^o^ "When all doors are closed, God will open a Windows" ^o^
============================================

"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
message
news:Ov**************@TK2MSFTNGP15.phx.gbl...
Hello all,
[I've been programming in C# for several months now, so bare with me...

I'm
still a newbie]

I've create a custom control (UserControl) and have a custom Item
Collection. The control is a custom calendar which is draw using the
Graphics Rectangle etc. functions. It is drawn when the control is
painted
or resized. When the control is drawn it draws also the items found in
the
collection.

So far so good.... I have 3 questions which I'm unable to find a solution
for yet (googled for several days now :-( ). Hopefull someone of you can
help me?

1. With a ListView control you can remove items using the
listView1.Items[1].Remove() or listView1.Remove(item). How does one
implement the first option (the second one I know how)?

2. The UserControl is drawn when the control is painted or resized (in
the
event I call the function that draws the control). Every time I add a
item
to the collection I have to call the control.Refresh() function to update

my
control with the added item. How can I make it so that whenever I add an
item to my collection the control is (re)drawn automatically?

3. The drawn control flickers when I do a Refresh() or when I redraw the
control. I've already tried to remove the Graphics.Clear(Color) function

but
that did not help :-( How can I reduce the flickering?

Below I placed the item collection code (simplified). Hopefully someone
in
this newsgroup can tell me what to do with the first 2 questions.... they
are really bugging me.

Thanks in advance!
Regards,
Tinus
(The Netherlands)
----
public class Items : System.Collections.CollectionBase
{
public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();

this.List.Add(item);

return this;
}

public virtual Items Add(string name, int number)
{
return this.Add(new Data(name, number));
}

public virtual Item this[int index]
{
get
{
if (index < 0 || index > this.List.Count)
throw new ArgumentOutOfRangeException();
return (Item)this.List[index];
}
}
}

public class Item
{
private string name;
private int number;

public string Name
{
get
{
return this.name;
}
}

public int Number
{
get
{
return this.number;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
this.number = value;
}
}

public Item(string name, int number)
{
this.name = name;
this.number = number;
}
}


Nov 16 '05 #3
Ricky,

This 'flickerfree' link was awesome! No more flickering. Now the custom
control looks and feels really professional.

Thanks a lot!
Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42**********************@news.wanadoo.nl...
Hi Tinus,

First of all, if you're creating an owner-drawn custom calender (drawing
using the graphics rectangle etc), it's better to derive your control from
the Control class instead of UserControl. UserControl is best suited if
you
want to combine several basic/constituent controls into a single logical
unit.

As to your questions:
1. The 'Items[1].Remove()' expression is basically calling the Remove()
method of the Item object returned by the indexer. So you have to
implement
a Remove() method in your Item class. You can just call the Remove(item)
method of the Items collection from the Remove() method of the Item class.
It's not hard to implement this. I'll leave the detail for your exercise.
Hint: you may need to define a private field in the Items collection and
Item class to point to the their owner (i.e. an instance of your control).

2. After adding the item to the collection, just call the Invalidate()
method of your control.

3. There are several techniques in reducing flicker. One of them is a
technique called 'Double Buffering'. You can just do some googling to
learn
more about double buffering. One site that you may find useful is
http://www.codeproject.com/cs/media/...reeDrawing.asp.

Hope it helps. Good luck.

-- Ricky Lee
============================================
^o^ "When all doors are closed, God will open a Windows" ^o^
============================================

"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
message
news:Ov**************@TK2MSFTNGP15.phx.gbl...
Hello all,
[I've been programming in C# for several months now, so bare with me...

I'm
still a newbie]

I've create a custom control (UserControl) and have a custom Item
Collection. The control is a custom calendar which is draw using the
Graphics Rectangle etc. functions. It is drawn when the control is
painted
or resized. When the control is drawn it draws also the items found in
the
collection.

So far so good.... I have 3 questions which I'm unable to find a solution
for yet (googled for several days now :-( ). Hopefull someone of you can
help me?

1. With a ListView control you can remove items using the
listView1.Items[1].Remove() or listView1.Remove(item). How does one
implement the first option (the second one I know how)?

2. The UserControl is drawn when the control is painted or resized (in
the
event I call the function that draws the control). Every time I add a
item
to the collection I have to call the control.Refresh() function to update

my
control with the added item. How can I make it so that whenever I add an
item to my collection the control is (re)drawn automatically?

3. The drawn control flickers when I do a Refresh() or when I redraw the
control. I've already tried to remove the Graphics.Clear(Color) function

but
that did not help :-( How can I reduce the flickering?

Below I placed the item collection code (simplified). Hopefully someone
in
this newsgroup can tell me what to do with the first 2 questions.... they
are really bugging me.

Thanks in advance!
Regards,
Tinus
(The Netherlands)
----
public class Items : System.Collections.CollectionBase
{
public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();

this.List.Add(item);

return this;
}

public virtual Items Add(string name, int number)
{
return this.Add(new Data(name, number));
}

public virtual Item this[int index]
{
get
{
if (index < 0 || index > this.List.Count)
throw new ArgumentOutOfRangeException();
return (Item)this.List[index];
}
}
}

public class Item
{
private string name;
private int number;

public string Name
{
get
{
return this.name;
}
}

public int Number
{
get
{
return this.number;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
this.number = value;
}
}

public Item(string name, int number)
{
this.name = name;
this.number = number;
}
}


Nov 16 '05 #4
Tinus,
Since you've solved question 1, I assume you now have a field in your Items
collection that points to the instance of your control owning this
collection. In the Items.Add method of your collection, call the
Invalidate() method of the owning control. For example:

public class Items : System.Collections.CollectionBase
{
// define field to point to owner control
// you can initialize it in the constructor
// or provide property accessor
private MyControl owner;

public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();
this.List.Add(item);
// invalidate the owning control
// to redraw with the new collection
if (owner != null)
owner.Invalidate();

return this;
}

Note: you have to provide a proper Paint logic for your control, to draw all
members of the Items collection.

Hope it helps.

-- Ricky Lee
==================================================
^o^ "When all doors are closed, God will open a Windows" ^o^
==================================================
"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in message
news:e$**************@TK2MSFTNGP09.phx.gbl...
Thanks Ricky!

Question 1 is now solved.... and it was so easy! But I really needed the
hint ;-)

As for question 2; How can I Invalidate() my control from either the Item
class of Items class? If I just make an instance to my control and then
Invalidate this instance then the results are: nothing happens. This is
because the instance does not have any items in it I think.

So can you once again give me one of your helpfull hints?

Also thanks for the flickerfree link... I'm going to look at it today.

Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42**********************@news.wanadoo.nl...
Hi Tinus,

First of all, if you're creating an owner-drawn custom calender (drawing
using the graphics rectangle etc), it's better to derive your control from the Control class instead of UserControl. UserControl is best suited if
you
want to combine several basic/constituent controls into a single logical
unit.

As to your questions:
1. The 'Items[1].Remove()' expression is basically calling the Remove()
method of the Item object returned by the indexer. So you have to
implement
a Remove() method in your Item class. You can just call the Remove(item) method of the Items collection from the Remove() method of the Item class. It's not hard to implement this. I'll leave the detail for your exercise. Hint: you may need to define a private field in the Items collection and
Item class to point to the their owner (i.e. an instance of your control).
2. After adding the item to the collection, just call the Invalidate()
method of your control.

3. There are several techniques in reducing flicker. One of them is a
technique called 'Double Buffering'. You can just do some googling to
learn
more about double buffering. One site that you may find useful is
http://www.codeproject.com/cs/media/...reeDrawing.asp.

Hope it helps. Good luck.

-- Ricky Lee
============================================
^o^ "When all doors are closed, God will open a Windows" ^o^
============================================

"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
message
news:Ov**************@TK2MSFTNGP15.phx.gbl...
Hello all,
[I've been programming in C# for several months now, so bare with me...

I'm
still a newbie]

I've create a custom control (UserControl) and have a custom Item
Collection. The control is a custom calendar which is draw using the
Graphics Rectangle etc. functions. It is drawn when the control is
painted
or resized. When the control is drawn it draws also the items found in
the
collection.

So far so good.... I have 3 questions which I'm unable to find a solution for yet (googled for several days now :-( ). Hopefull someone of you can help me?

1. With a ListView control you can remove items using the
listView1.Items[1].Remove() or listView1.Remove(item). How does one
implement the first option (the second one I know how)?

2. The UserControl is drawn when the control is painted or resized (in
the
event I call the function that draws the control). Every time I add a
item
to the collection I have to call the control.Refresh() function to update
my
control with the added item. How can I make it so that whenever I add
an item to my collection the control is (re)drawn automatically?

3. The drawn control flickers when I do a Refresh() or when I redraw the control. I've already tried to remove the Graphics.Clear(Color) function but
that did not help :-( How can I reduce the flickering?

Below I placed the item collection code (simplified). Hopefully someone
in
this newsgroup can tell me what to do with the first 2 questions....

they are really bugging me.

Thanks in advance!
Regards,
Tinus
(The Netherlands)
----
public class Items : System.Collections.CollectionBase
{
public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();

this.List.Add(item);

return this;
}

public virtual Items Add(string name, int number)
{
return this.Add(new Data(name, number));
}

public virtual Item this[int index]
{
get
{
if (index < 0 || index > this.List.Count)
throw new ArgumentOutOfRangeException();
return (Item)this.List[index];
}
}
}

public class Item
{
private string name;
private int number;

public string Name
{
get
{
return this.name;
}
}

public int Number
{
get
{
return this.number;
}
set
{
if (value < 0)
throw new ArgumentOutOfRangeException();
this.number = value;
}
}

public Item(string name, int number)
{
this.name = name;
this.number = number;
}
}



Nov 16 '05 #5
Ehh, no....

I changed the public Item(name, number) function in the Item class to:

private Items _item;
public Item(Items items, string name, int number)
{
this._items = items;
...
}

and the public Items Add(string name, int number) to:

public Items Add(string name, int number)
{
this.Add(new Item(this, name, number));
return this;
}

The above code gives me the Items collection in the Item class. So now I
could add a Remove() function in the Item class:

public void Remove()
{
_items.Remove(this);
}

I think you had a different solution in mind? Can you give me an example of
how to do it your way?
How can I get an instance of my control in the Item class?

Many thanks!
Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42***********************@news.wanadoo.nl...
Tinus,
Since you've solved question 1, I assume you now have a field in your
Items
collection that points to the instance of your control owning this
collection. In the Items.Add method of your collection, call the
Invalidate() method of the owning control. For example:

public class Items : System.Collections.CollectionBase
{
// define field to point to owner control
// you can initialize it in the constructor
// or provide property accessor
private MyControl owner;

public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();
this.List.Add(item);
// invalidate the owning control
// to redraw with the new collection
if (owner != null)
owner.Invalidate();

return this;
}

Note: you have to provide a proper Paint logic for your control, to draw
all
members of the Items collection.

Hope it helps.

-- Ricky Lee
==================================================
^o^ "When all doors are closed, God will open a Windows" ^o^
==================================================
"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
message
news:e$**************@TK2MSFTNGP09.phx.gbl...
Thanks Ricky!

Question 1 is now solved.... and it was so easy! But I really needed the
hint ;-)

As for question 2; How can I Invalidate() my control from either the Item
class of Items class? If I just make an instance to my control and then
Invalidate this instance then the results are: nothing happens. This is
because the instance does not have any items in it I think.

So can you once again give me one of your helpfull hints?

Also thanks for the flickerfree link... I'm going to look at it today.

Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42**********************@news.wanadoo.nl...
> Hi Tinus,
>
> First of all, if you're creating an owner-drawn custom calender
> (drawing
> using the graphics rectangle etc), it's better to derive your control from > the Control class instead of UserControl. UserControl is best suited if
> you
> want to combine several basic/constituent controls into a single
> logical
> unit.
>
> As to your questions:
> 1. The 'Items[1].Remove()' expression is basically calling the Remove()
> method of the Item object returned by the indexer. So you have to
> implement
> a Remove() method in your Item class. You can just call the Remove(item) > method of the Items collection from the Remove() method of the Item class. > It's not hard to implement this. I'll leave the detail for your exercise. > Hint: you may need to define a private field in the Items collection
> and
> Item class to point to the their owner (i.e. an instance of your control). >
> 2. After adding the item to the collection, just call the Invalidate()
> method of your control.
>
> 3. There are several techniques in reducing flicker. One of them is a
> technique called 'Double Buffering'. You can just do some googling to
> learn
> more about double buffering. One site that you may find useful is
> http://www.codeproject.com/cs/media/...reeDrawing.asp.
>
> Hope it helps. Good luck.
>
> -- Ricky Lee
> ============================================
> ^o^ "When all doors are closed, God will open a Windows" ^o^
> ============================================
>
> "Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
> message
> news:Ov**************@TK2MSFTNGP15.phx.gbl...
>> Hello all,
>> [I've been programming in C# for several months now, so bare with
>> me...
> I'm
>> still a newbie]
>>
>> I've create a custom control (UserControl) and have a custom Item
>> Collection. The control is a custom calendar which is draw using the
>> Graphics Rectangle etc. functions. It is drawn when the control is
>> painted
>> or resized. When the control is drawn it draws also the items found in
>> the
>> collection.
>>
>> So far so good.... I have 3 questions which I'm unable to find a solution >> for yet (googled for several days now :-( ). Hopefull someone of you can >> help me?
>>
>> 1. With a ListView control you can remove items using the
>> listView1.Items[1].Remove() or listView1.Remove(item). How does one
>> implement the first option (the second one I know how)?
>>
>> 2. The UserControl is drawn when the control is painted or resized (in
>> the
>> event I call the function that draws the control). Every time I add a
>> item
>> to the collection I have to call the control.Refresh() function to update > my
>> control with the added item. How can I make it so that whenever I add an >> item to my collection the control is (re)drawn automatically?
>>
>> 3. The drawn control flickers when I do a Refresh() or when I redraw the >> control. I've already tried to remove the Graphics.Clear(Color) function > but
>> that did not help :-( How can I reduce the flickering?
>>
>> Below I placed the item collection code (simplified). Hopefully
>> someone
>> in
>> this newsgroup can tell me what to do with the first 2 questions.... they >> are really bugging me.
>>
>> Thanks in advance!
>> Regards,
>> Tinus
>> (The Netherlands)
>> ----
>> public class Items : System.Collections.CollectionBase
>> {
>> public virtual Items Add(Item item)
>> {
>> if (this.List.Contains(item))
>> throw new InvalidOperationException();
>>
>> this.List.Add(item);
>>
>> return this;
>> }
>>
>> public virtual Items Add(string name, int number)
>> {
>> return this.Add(new Data(name, number));
>> }
>>
>> public virtual Item this[int index]
>> {
>> get
>> {
>> if (index < 0 || index > this.List.Count)
>> throw new ArgumentOutOfRangeException();
>> return (Item)this.List[index];
>> }
>> }
>> }
>>
>> public class Item
>> {
>> private string name;
>> private int number;
>>
>> public string Name
>> {
>> get
>> {
>> return this.name;
>> }
>> }
>>
>> public int Number
>> {
>> get
>> {
>> return this.number;
>> }
>> set
>> {
>> if (value < 0)
>> throw new ArgumentOutOfRangeException();
>> this.number = value;
>> }
>> }
>>
>> public Item(string name, int number)
>> {
>> this.name = name;
>> this.number = number;
>> }
>> }
>>
>>
>
>



Nov 16 '05 #6
Again I'll leave this one for your exercise. Hint: instead of using a
private field pointing to the Items collection, try using one pointing to
the owning control. Study the members of ListView, ListViewItemCollection,
and ListViewItem for ideas.

-- Ricky Lee
==================================================
^o^ "When all doors are closed, God will open a Windows" ^o^
==================================================
"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in message
news:ug**************@TK2MSFTNGP09.phx.gbl...
Ehh, no....

I changed the public Item(name, number) function in the Item class to:

private Items _item;
public Item(Items items, string name, int number)
{
this._items = items;
...
}

and the public Items Add(string name, int number) to:

public Items Add(string name, int number)
{
this.Add(new Item(this, name, number));
return this;
}

The above code gives me the Items collection in the Item class. So now I
could add a Remove() function in the Item class:

public void Remove()
{
_items.Remove(this);
}

I think you had a different solution in mind? Can you give me an example of how to do it your way?
How can I get an instance of my control in the Item class?

Many thanks!
Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42***********************@news.wanadoo.nl...
Tinus,
Since you've solved question 1, I assume you now have a field in your
Items
collection that points to the instance of your control owning this
collection. In the Items.Add method of your collection, call the
Invalidate() method of the owning control. For example:

public class Items : System.Collections.CollectionBase
{
// define field to point to owner control
// you can initialize it in the constructor
// or provide property accessor
private MyControl owner;

public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();
this.List.Add(item);
// invalidate the owning control
// to redraw with the new collection
if (owner != null)
owner.Invalidate();

return this;
}

Note: you have to provide a proper Paint logic for your control, to draw
all
members of the Items collection.

Hope it helps.

-- Ricky Lee
==================================================
^o^ "When all doors are closed, God will open a Windows" ^o^
==================================================
"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
message
news:e$**************@TK2MSFTNGP09.phx.gbl...
Thanks Ricky!

Question 1 is now solved.... and it was so easy! But I really needed the hint ;-)

As for question 2; How can I Invalidate() my control from either the Item class of Items class? If I just make an instance to my control and then
Invalidate this instance then the results are: nothing happens. This is
because the instance does not have any items in it I think.

So can you once again give me one of your helpfull hints?

Also thanks for the flickerfree link... I'm going to look at it today.

Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42**********************@news.wanadoo.nl...
> Hi Tinus,
>
> First of all, if you're creating an owner-drawn custom calender
> (drawing
> using the graphics rectangle etc), it's better to derive your control

from
> the Control class instead of UserControl. UserControl is best suited if > you
> want to combine several basic/constituent controls into a single
> logical
> unit.
>
> As to your questions:
> 1. The 'Items[1].Remove()' expression is basically calling the Remove() > method of the Item object returned by the indexer. So you have to
> implement
> a Remove() method in your Item class. You can just call the

Remove(item)
> method of the Items collection from the Remove() method of the Item

class.
> It's not hard to implement this. I'll leave the detail for your

exercise.
> Hint: you may need to define a private field in the Items collection
> and
> Item class to point to the their owner (i.e. an instance of your

control).
>
> 2. After adding the item to the collection, just call the Invalidate() > method of your control.
>
> 3. There are several techniques in reducing flicker. One of them is a
> technique called 'Double Buffering'. You can just do some googling to > learn
> more about double buffering. One site that you may find useful is
> http://www.codeproject.com/cs/media/...reeDrawing.asp.
>
> Hope it helps. Good luck.
>
> -- Ricky Lee
> ============================================
> ^o^ "When all doors are closed, God will open a Windows" ^o^
> ============================================
>
> "Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
> message
> news:Ov**************@TK2MSFTNGP15.phx.gbl...
>> Hello all,
>> [I've been programming in C# for several months now, so bare with
>> me...
> I'm
>> still a newbie]
>>
>> I've create a custom control (UserControl) and have a custom Item
>> Collection. The control is a custom calendar which is draw using the
>> Graphics Rectangle etc. functions. It is drawn when the control is
>> painted
>> or resized. When the control is drawn it draws also the items found in >> the
>> collection.
>>
>> So far so good.... I have 3 questions which I'm unable to find a

solution
>> for yet (googled for several days now :-( ). Hopefull someone of you
can
>> help me?
>>
>> 1. With a ListView control you can remove items using the
>> listView1.Items[1].Remove() or listView1.Remove(item). How does one
>> implement the first option (the second one I know how)?
>>
>> 2. The UserControl is drawn when the control is painted or resized
(in >> the
>> event I call the function that draws the control). Every time I add a >> item
>> to the collection I have to call the control.Refresh() function to

update
> my
>> control with the added item. How can I make it so that whenever I

add an
>> item to my collection the control is (re)drawn automatically?
>>
>> 3. The drawn control flickers when I do a Refresh() or when I redraw

the
>> control. I've already tried to remove the Graphics.Clear(Color)

function
> but
>> that did not help :-( How can I reduce the flickering?
>>
>> Below I placed the item collection code (simplified). Hopefully
>> someone
>> in
>> this newsgroup can tell me what to do with the first 2 questions....

they
>> are really bugging me.
>>
>> Thanks in advance!
>> Regards,
>> Tinus
>> (The Netherlands)
>> ----
>> public class Items : System.Collections.CollectionBase
>> {
>> public virtual Items Add(Item item)
>> {
>> if (this.List.Contains(item))
>> throw new InvalidOperationException();
>>
>> this.List.Add(item);
>>
>> return this;
>> }
>>
>> public virtual Items Add(string name, int number)
>> {
>> return this.Add(new Data(name, number));
>> }
>>
>> public virtual Item this[int index]
>> {
>> get
>> {
>> if (index < 0 || index > this.List.Count)
>> throw new ArgumentOutOfRangeException();
>> return (Item)this.List[index];
>> }
>> }
>> }
>>
>> public class Item
>> {
>> private string name;
>> private int number;
>>
>> public string Name
>> {
>> get
>> {
>> return this.name;
>> }
>> }
>>
>> public int Number
>> {
>> get
>> {
>> return this.number;
>> }
>> set
>> {
>> if (value < 0)
>> throw new ArgumentOutOfRangeException();
>> this.number = value;
>> }
>> }
>>
>> public Item(string name, int number)
>> {
>> this.name = name;
>> this.number = number;
>> }
>> }
>>
>>
>
>



Nov 16 '05 #7
Got it!

In my Items class I had to add:

private MyControl owner;
internal Datas(MyControl control)
{
this.owner = control;
}

And had to change Items items = new Items() in my custom control to:
Items items = new Items(this)

:-) Now it works.

Ricky, thanks for your time and hints!

Tinus

"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in message
news:ug**************@TK2MSFTNGP09.phx.gbl...
Ehh, no....

I changed the public Item(name, number) function in the Item class to:

private Items _item;
public Item(Items items, string name, int number)
{
this._items = items;
...
}

and the public Items Add(string name, int number) to:

public Items Add(string name, int number)
{
this.Add(new Item(this, name, number));
return this;
}

The above code gives me the Items collection in the Item class. So now I
could add a Remove() function in the Item class:

public void Remove()
{
_items.Remove(this);
}

I think you had a different solution in mind? Can you give me an example
of how to do it your way?
How can I get an instance of my control in the Item class?

Many thanks!
Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42***********************@news.wanadoo.nl...
Tinus,
Since you've solved question 1, I assume you now have a field in your
Items
collection that points to the instance of your control owning this
collection. In the Items.Add method of your collection, call the
Invalidate() method of the owning control. For example:

public class Items : System.Collections.CollectionBase
{
// define field to point to owner control
// you can initialize it in the constructor
// or provide property accessor
private MyControl owner;

public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();
this.List.Add(item);
// invalidate the owning control
// to redraw with the new collection
if (owner != null)
owner.Invalidate();

return this;
}

Note: you have to provide a proper Paint logic for your control, to draw
all
members of the Items collection.

Hope it helps.

-- Ricky Lee
==================================================
^o^ "When all doors are closed, God will open a Windows" ^o^
==================================================
"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
message
news:e$**************@TK2MSFTNGP09.phx.gbl...
Thanks Ricky!

Question 1 is now solved.... and it was so easy! But I really needed the
hint ;-)

As for question 2; How can I Invalidate() my control from either the
Item
class of Items class? If I just make an instance to my control and then
Invalidate this instance then the results are: nothing happens. This is
because the instance does not have any items in it I think.

So can you once again give me one of your helpfull hints?

Also thanks for the flickerfree link... I'm going to look at it today.

Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42**********************@news.wanadoo.nl...
> Hi Tinus,
>
> First of all, if you're creating an owner-drawn custom calender
> (drawing
> using the graphics rectangle etc), it's better to derive your control

from
> the Control class instead of UserControl. UserControl is best suited
> if
> you
> want to combine several basic/constituent controls into a single
> logical
> unit.
>
> As to your questions:
> 1. The 'Items[1].Remove()' expression is basically calling the
> Remove()
> method of the Item object returned by the indexer. So you have to
> implement
> a Remove() method in your Item class. You can just call the

Remove(item)
> method of the Items collection from the Remove() method of the Item

class.
> It's not hard to implement this. I'll leave the detail for your

exercise.
> Hint: you may need to define a private field in the Items collection
> and
> Item class to point to the their owner (i.e. an instance of your

control).
>
> 2. After adding the item to the collection, just call the Invalidate()
> method of your control.
>
> 3. There are several techniques in reducing flicker. One of them is a
> technique called 'Double Buffering'. You can just do some googling to
> learn
> more about double buffering. One site that you may find useful is
> http://www.codeproject.com/cs/media/...reeDrawing.asp.
>
> Hope it helps. Good luck.
>
> -- Ricky Lee
> ============================================
> ^o^ "When all doors are closed, God will open a Windows" ^o^
> ============================================
>
> "Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
> message
> news:Ov**************@TK2MSFTNGP15.phx.gbl...
>> Hello all,
>> [I've been programming in C# for several months now, so bare with
>> me...
> I'm
>> still a newbie]
>>
>> I've create a custom control (UserControl) and have a custom Item
>> Collection. The control is a custom calendar which is draw using the
>> Graphics Rectangle etc. functions. It is drawn when the control is
>> painted
>> or resized. When the control is drawn it draws also the items found
>> in
>> the
>> collection.
>>
>> So far so good.... I have 3 questions which I'm unable to find a

solution
>> for yet (googled for several days now :-( ). Hopefull someone of you

can
>> help me?
>>
>> 1. With a ListView control you can remove items using the
>> listView1.Items[1].Remove() or listView1.Remove(item). How does one
>> implement the first option (the second one I know how)?
>>
>> 2. The UserControl is drawn when the control is painted or resized
>> (in
>> the
>> event I call the function that draws the control). Every time I add a
>> item
>> to the collection I have to call the control.Refresh() function to

update
> my
>> control with the added item. How can I make it so that whenever I add

an
>> item to my collection the control is (re)drawn automatically?
>>
>> 3. The drawn control flickers when I do a Refresh() or when I redraw

the
>> control. I've already tried to remove the Graphics.Clear(Color)

function
> but
>> that did not help :-( How can I reduce the flickering?
>>
>> Below I placed the item collection code (simplified). Hopefully
>> someone
>> in
>> this newsgroup can tell me what to do with the first 2 questions....

they
>> are really bugging me.
>>
>> Thanks in advance!
>> Regards,
>> Tinus
>> (The Netherlands)
>> ----
>> public class Items : System.Collections.CollectionBase
>> {
>> public virtual Items Add(Item item)
>> {
>> if (this.List.Contains(item))
>> throw new InvalidOperationException();
>>
>> this.List.Add(item);
>>
>> return this;
>> }
>>
>> public virtual Items Add(string name, int number)
>> {
>> return this.Add(new Data(name, number));
>> }
>>
>> public virtual Item this[int index]
>> {
>> get
>> {
>> if (index < 0 || index > this.List.Count)
>> throw new ArgumentOutOfRangeException();
>> return (Item)this.List[index];
>> }
>> }
>> }
>>
>> public class Item
>> {
>> private string name;
>> private int number;
>>
>> public string Name
>> {
>> get
>> {
>> return this.name;
>> }
>> }
>>
>> public int Number
>> {
>> get
>> {
>> return this.number;
>> }
>> set
>> {
>> if (value < 0)
>> throw new ArgumentOutOfRangeException();
>> this.number = value;
>> }
>> }
>>
>> public Item(string name, int number)
>> {
>> this.name = name;
>> this.number = number;
>> }
>> }
>>
>>
>
>



Nov 16 '05 #8
Glad to help ;)
-- Ricky Lee
==================================================
^o^ "When all doors are closed, God will open a Windows" ^o^
==================================================
"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in message
news:OK**************@TK2MSFTNGP10.phx.gbl...
Got it!

In my Items class I had to add:

private MyControl owner;
internal Datas(MyControl control)
{
this.owner = control;
}

And had to change Items items = new Items() in my custom control to:
Items items = new Items(this)

:-) Now it works.

Ricky, thanks for your time and hints!

Tinus

"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in message news:ug**************@TK2MSFTNGP09.phx.gbl...
Ehh, no....

I changed the public Item(name, number) function in the Item class to:

private Items _item;
public Item(Items items, string name, int number)
{
this._items = items;
...
}

and the public Items Add(string name, int number) to:

public Items Add(string name, int number)
{
this.Add(new Item(this, name, number));
return this;
}

The above code gives me the Items collection in the Item class. So now I
could add a Remove() function in the Item class:

public void Remove()
{
_items.Remove(this);
}

I think you had a different solution in mind? Can you give me an example
of how to do it your way?
How can I get an instance of my control in the Item class?

Many thanks!
Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42***********************@news.wanadoo.nl...
Tinus,
Since you've solved question 1, I assume you now have a field in your
Items
collection that points to the instance of your control owning this
collection. In the Items.Add method of your collection, call the
Invalidate() method of the owning control. For example:

public class Items : System.Collections.CollectionBase
{
// define field to point to owner control
// you can initialize it in the constructor
// or provide property accessor
private MyControl owner;

public virtual Items Add(Item item)
{
if (this.List.Contains(item))
throw new InvalidOperationException();
this.List.Add(item);
// invalidate the owning control
// to redraw with the new collection
if (owner != null)
owner.Invalidate();

return this;
}

Note: you have to provide a proper Paint logic for your control, to draw all
members of the Items collection.

Hope it helps.

-- Ricky Lee
==================================================
^o^ "When all doors are closed, God will open a Windows" ^o^
==================================================
"Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
message
news:e$**************@TK2MSFTNGP09.phx.gbl...
Thanks Ricky!

Question 1 is now solved.... and it was so easy! But I really needed the hint ;-)

As for question 2; How can I Invalidate() my control from either the
Item
class of Items class? If I just make an instance to my control and then Invalidate this instance then the results are: nothing happens. This is because the instance does not have any items in it I think.

So can you once again give me one of your helpfull hints?

Also thanks for the flickerfree link... I'm going to look at it today.

Tinus

"Ricky Lee" <de****@benzine.com> wrote in message
news:42**********************@news.wanadoo.nl...
> Hi Tinus,
>
> First of all, if you're creating an owner-drawn custom calender
> (drawing
> using the graphics rectangle etc), it's better to derive your control from
> the Control class instead of UserControl. UserControl is best suited
> if
> you
> want to combine several basic/constituent controls into a single
> logical
> unit.
>
> As to your questions:
> 1. The 'Items[1].Remove()' expression is basically calling the
> Remove()
> method of the Item object returned by the indexer. So you have to
> implement
> a Remove() method in your Item class. You can just call the
Remove(item)
> method of the Items collection from the Remove() method of the Item
class.
> It's not hard to implement this. I'll leave the detail for your
exercise.
> Hint: you may need to define a private field in the Items collection
> and
> Item class to point to the their owner (i.e. an instance of your
control).
>
> 2. After adding the item to the collection, just call the Invalidate() > method of your control.
>
> 3. There are several techniques in reducing flicker. One of them is a > technique called 'Double Buffering'. You can just do some googling to > learn
> more about double buffering. One site that you may find useful is
> http://www.codeproject.com/cs/media/...reeDrawing.asp.
>
> Hope it helps. Good luck.
>
> -- Ricky Lee
> ============================================
> ^o^ "When all doors are closed, God will open a Windows" ^o^
> ============================================
>
> "Tinus" <no****************@xxx.karssemeijer.com.noadv.xxx > wrote in
> message
> news:Ov**************@TK2MSFTNGP15.phx.gbl...
>> Hello all,
>> [I've been programming in C# for several months now, so bare with
>> me...
> I'm
>> still a newbie]
>>
>> I've create a custom control (UserControl) and have a custom Item
>> Collection. The control is a custom calendar which is draw using the >> Graphics Rectangle etc. functions. It is drawn when the control is
>> painted
>> or resized. When the control is drawn it draws also the items found
>> in
>> the
>> collection.
>>
>> So far so good.... I have 3 questions which I'm unable to find a
solution
>> for yet (googled for several days now :-( ). Hopefull someone of you can
>> help me?
>>
>> 1. With a ListView control you can remove items using the
>> listView1.Items[1].Remove() or listView1.Remove(item). How does one
>> implement the first option (the second one I know how)?
>>
>> 2. The UserControl is drawn when the control is painted or resized
>> (in
>> the
>> event I call the function that draws the control). Every time I add a >> item
>> to the collection I have to call the control.Refresh() function to
update
> my
>> control with the added item. How can I make it so that whenever I add an
>> item to my collection the control is (re)drawn automatically?
>>
>> 3. The drawn control flickers when I do a Refresh() or when I redraw the
>> control. I've already tried to remove the Graphics.Clear(Color)
function
> but
>> that did not help :-( How can I reduce the flickering?
>>
>> Below I placed the item collection code (simplified). Hopefully
>> someone
>> in
>> this newsgroup can tell me what to do with the first 2 questions.... they
>> are really bugging me.
>>
>> Thanks in advance!
>> Regards,
>> Tinus
>> (The Netherlands)
>> ----
>> public class Items : System.Collections.CollectionBase
>> {
>> public virtual Items Add(Item item)
>> {
>> if (this.List.Contains(item))
>> throw new InvalidOperationException();
>>
>> this.List.Add(item);
>>
>> return this;
>> }
>>
>> public virtual Items Add(string name, int number)
>> {
>> return this.Add(new Data(name, number));
>> }
>>
>> public virtual Item this[int index]
>> {
>> get
>> {
>> if (index < 0 || index > this.List.Count)
>> throw new ArgumentOutOfRangeException();
>> return (Item)this.List[index];
>> }
>> }
>> }
>>
>> public class Item
>> {
>> private string name;
>> private int number;
>>
>> public string Name
>> {
>> get
>> {
>> return this.name;
>> }
>> }
>>
>> public int Number
>> {
>> get
>> {
>> return this.number;
>> }
>> set
>> {
>> if (value < 0)
>> throw new ArgumentOutOfRangeException();
>> this.number = value;
>> }
>> }
>>
>> public Item(string name, int number)
>> {
>> this.name = name;
>> this.number = number;
>> }
>> }
>>
>>
>
>



Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jose Michael Meo R. Barrido | last post by:
I made a custom runded rectangle usercontrol. I used a function i found on the internet. the function works fine(see "GetRoundRect" below). I use the fullowing code to make my usercontrol...
1
by: Kepler | last post by:
I have a custom control that is thrown onto a UserControl that is thrown onto a WebForm. Basically, I've got a scenario where if my UserControl sets an attribute on the custom control in the ascx,...
0
by: Daniel Friend | last post by:
Hello, I have added a custom usercontrol programically and all works fine... I would like to change some custom properties that I have set in that usercontrol. Any help would be greatly...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
4
by: DraguVaso | last post by:
Hi, I'm having some weird behaviour, and I can't find any solution to this. I made a UserControl (uclCommands) which contains a Toolbar (ToolBar1) with Modifiers = Public. I put this...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
0
by: Joe | last post by:
Anyone have any experience using custom snaplines? I have a usercontrol that acts as a container for several child controls. I wrote an inherited ControlDesigner to handle some custom actions...
5
by: Blue | last post by:
We have a custom word processing type editor built with C# and .NET 2.0 and we need to support typing in languages other than English. I want to be able to use the Windows IME to enter in text...
1
by: =?Utf-8?B?TmV3YnJv?= | last post by:
I have this program running on PDA compact framework in which it will receive paint message from server's networkstream and updates the client's screen by drawing on this custom 'Draw' userControl....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.