classes

  • Thread starter Thread starter Maarten
  • Start date Start date
M

Maarten

hi all,

I have a usercontrol with a checked listbox

this usercontrol is placed on a form1

is there a way to get the checked items of the checkedlisbox in the
usercontrol with a command from form1?

i beleave i have to do this with "get" and "set in a class

i've never worked with classes before

the way to get the items from the listbox is the code below.

thanks Maarten.

public void getchecked()

{

for (int x = 0; x <= (lstChannels.Items.Count-1); x++)

{

if (lstChannels.GetItemChecked(x))

{

}


else

{

}

}
 
Maarten said:
hi all,

I have a usercontrol with a checked listbox

this usercontrol is placed on a form1

is there a way to get the checked items of the checkedlisbox in the
usercontrol with a command from form1?

i beleave i have to do this with "get" and "set in a class

i've never worked with classes before

the way to get the items from the listbox is the code below.

thanks Maarten.

public void getchecked()

{

for (int x = 0; x <= (lstChannels.Items.Count-1); x++)

Note: a usual way to write the mid-part is:
x < lstChannels.Items.Count
("<" instead of "<=" and no "-1")
{

if (lstChannels.GetItemChecked(x))

do you mean "lstChannels.Items[x].Checked" ?
{

}


else

{

}

}

You want to access values from the UserControl from within the
containing aspx page (if I understand correctly).
Usually (meaning: the way I write them :-) ) an ascx is self-contained:
all processing is done internally, a surrounding control or page
does not need to know anything about it.

When you add a textbox to a page (or ascx), you get a declaration
in the codebehind. This is not done automatically for ascx'es,
so you have to add it yourself.
After that you can access any properties within that control,
IF the access modifiers are set correctly ("public").
 
Hi Hans,

thanks for the reply

i have some public functions in that usercontrole, and i have been able to
call them from the main form1
with:
usercontrol1.getchecked();

but this isn't realy the problem.

i was wondering if i can put this getcheched void in a class, so i can
rertieve the data (in this case the checked itmes) of that class with
something like "get" and "set" (ive seen some exaples with this code).

i'm asking this question rather because i'm trying to understand classes
more than actualy
let let this whole thing work.

i've been looking for a long time on the net trying to understand, but i
just wont get it.

Can you please post a simple example.

kind regards Maarten


Hans Kesting said:
Maarten said:
hi all,

I have a usercontrol with a checked listbox

this usercontrol is placed on a form1

is there a way to get the checked items of the checkedlisbox in the
usercontrol with a command from form1?

i beleave i have to do this with "get" and "set in a class

i've never worked with classes before

the way to get the items from the listbox is the code below.

thanks Maarten.

public void getchecked()

{

for (int x = 0; x <= (lstChannels.Items.Count-1); x++)

Note: a usual way to write the mid-part is:
x < lstChannels.Items.Count
("<" instead of "<=" and no "-1")
{

if (lstChannels.GetItemChecked(x))

do you mean "lstChannels.Items[x].Checked" ?
{

}


else

{

}

}

You want to access values from the UserControl from within the containing
aspx page (if I understand correctly).
Usually (meaning: the way I write them :-) ) an ascx is self-contained:
all processing is done internally, a surrounding control or page
does not need to know anything about it.

When you add a textbox to a page (or ascx), you get a declaration
in the codebehind. This is not done automatically for ascx'es,
so you have to add it yourself.
After that you can access any properties within that control,
IF the access modifiers are set correctly ("public").
 
I think what you want is to expose the Items property of the listbox. Inside
your usercontrol class you could do something like
public virtual ListItemCollection Items
{
get{ return listBox.Items; }
}
(I am not sure about the type of the items collection?)

If you want them to be editable in the designer, you could attributes like

[Description("Gets the collection of items displayed in the list."),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor("System.Windows.Forms.Design.StringCollectionEditor,
System.Design", typeof(UITypeEditor))]

If you don't want them to be seen in the designer, you could set
[Browsable(false)]

Hope this helps,
-Rachel
 
BTW...
You only need (can use) the "get" in this property because the collection
itself is readonly. However, you can add to the collection by using
Items.Add(), so you have all the functionality with only a get on the items.

Rachel Suddeth said:
I think what you want is to expose the Items property of the listbox. Inside
your usercontrol class you could do something like
public virtual ListItemCollection Items
{
get{ return listBox.Items; }
}
(I am not sure about the type of the items collection?)

If you want them to be editable in the designer, you could attributes like

[Description("Gets the collection of items displayed in the list."),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor("System.Windows.Forms.Design.StringCollectionEditor,
System.Design", typeof(UITypeEditor))]

If you don't want them to be seen in the designer, you could set
[Browsable(false)]

Hope this helps,
-Rachel

Maarten said:
hi all,

I have a usercontrol with a checked listbox

this usercontrol is placed on a form1

is there a way to get the checked items of the checkedlisbox in the
usercontrol with a command from form1?

i beleave i have to do this with "get" and "set in a class

i've never worked with classes before

the way to get the items from the listbox is the code below.

thanks Maarten.

public void getchecked()

{

for (int x = 0; x <= (lstChannels.Items.Count-1); x++)

{

if (lstChannels.GetItemChecked(x))

{

}


else

{

}

}
 
Thans for answering my question

I still didn't fugured it out yet, but thanks anyway

Rachel Suddeth said:
BTW...
You only need (can use) the "get" in this property because the collection
itself is readonly. However, you can add to the collection by using
Items.Add(), so you have all the functionality with only a get on the
items.

Rachel Suddeth said:
I think what you want is to expose the Items property of the listbox. Inside
your usercontrol class you could do something like
public virtual ListItemCollection Items
{
get{ return listBox.Items; }
}
(I am not sure about the type of the items collection?)

If you want them to be editable in the designer, you could attributes
like

[Description("Gets the collection of items displayed in the list."),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Editor("System.Windows.Forms.Design.StringCollectionEditor,
System.Design", typeof(UITypeEditor))]

If you don't want them to be seen in the designer, you could set
[Browsable(false)]

Hope this helps,
-Rachel

Maarten said:
hi all,

I have a usercontrol with a checked listbox

this usercontrol is placed on a form1

is there a way to get the checked items of the checkedlisbox in the
usercontrol with a command from form1?

i beleave i have to do this with "get" and "set in a class

i've never worked with classes before

the way to get the items from the listbox is the code below.

thanks Maarten.

public void getchecked()

{

for (int x = 0; x <= (lstChannels.Items.Count-1); x++)

{

if (lstChannels.GetItemChecked(x))

{

}


else

{

}

}
 
Back
Top