Windows Develop Bookmark and Share   
 index > Windows Forms General > User Control
 

User Control

I have created a user control (listbox, text box, add button and cancel). What I am trying to do is load the listbox with data from the db and the user will select 1 item from the listbox, enter a number in the text box then click the add button which will add that to the listbox and text box on the main form.

I have searched for examples on using user controls but haven't found any except for ASP.Net and this is a windows app.

Thanks for any help in advance,

Mark

ski_freak  Tuesday, July 17, 2007 11:41 AM

Hi skifreak,

We can add two propertiesfor the UserControl, onefor theListBox.Items and the otherfor TextBox.Text.Something like the following code.

Code Snippet

public ListBox.ObjectCollection Items

{

get { return this.listBox1.Items; }

}

public string TextBoxText

{

get { return this.textBox1.Text; }

}

Then you can get data from this UserControl using these properties.

Hope this helps.

Regards

You can create your user control using Adding User Control from the Project Manu

Main Menu> Project> Add User Control

It will show a workspace to work with the user control. it is just like a form.

Add your controls and do your code. After that compile.

After that you can use just like a Label and TextBox

Muthu Krishnan. R  Wednesday, July 18, 2007 6:09 AM

As my first post says I have already 'Created the control'. I am trying to figure how to use it. I would like to use it like a dialog box where I can pass it data to load the listbox and then get the values from the listbox and text box that the user enters and pass it back to the calling class.

ski_freak  Wednesday, July 18, 2007 6:20 PM

Hi skifreak,

We can add two propertiesfor the UserControl, onefor theListBox.Items and the otherfor TextBox.Text.Something like the following code.

Code Snippet

public ListBox.ObjectCollection Items

{

get { return this.listBox1.Items; }

}

public string TextBoxText

{

get { return this.textBox1.Text; }

}

Then you can get data from this UserControl using these properties.

Hope this helps.

Regards

That is exactly what I was looking for. Thank you!! I thought it was something simple Smile

ski_freak  Thursday, July 19, 2007 10:24 AM

You can use google to search for other answers

Custom Search

More Threads

• Get Common Event handler.
• Issue w/ Property Grid control and unhandled exceptions
• creation of large bitmap
• Checkboxes
• Form responding slow
• Curious operators
• I want to Adjust scrren pixel position run time with vb.net
• Animated Gif with ImageAttributes
• App.Config with File= option does not work in 2005
• How to make minimized application flash in Task Bar?