Custom controls

SLG29

Member
Joined
May 5, 2005
Messages
13
Programming Experience
Beginner
Could anyone help with this one

Have posted this before on a previous thread called database application but what I'm required to do now is run 3 custom controls instead which are as follows

Customer details (5 textboxes 1 combobox)

DataGrid displaying order information

DataGrid displaying order details

I've created Windows Control Library projects for the 3 controls but need some guidance on the next thing to do, I think that because these are custom controls you have to set the properties yourself as you've inherited from control

Any tips/examples which could help me would be appreciated

Thanks
 
This really depends, have you inherited from the 'USER CONTROL' Class or the 'CONTROL' Class. They do differ.
However setting properties would be the same in either.
You would simply use

[Public Property Someproperty As Somevalue
Get
Return MyVariable [Variable that you have defined to hold the value(i.e color,integer etc)
End Get
Set (Byval Value as Somevalue)
Myvariable = value
End Set
End Property

Is This What You Are After. Or Have I Mis-Understood
 
Thanks for the reply

Having looked at the assignment again and the requirements of the controls the first control will be a user control as it consists of multiple controls and the second and third will both consist of inheriting from the datagrid control
If possible could you answer the following

Do I still have to set the properties manually for these controls
Can I create all 3 controls in 1 Windows Control Library
Also another problem I've got is how to amend the code I've already got in order to cater for the user controls

Any tips/coding would be great if possible, I can give you more information if required

Thanks for your time
 
the control will inherit/have the properties they have.

u can also make your properties in addition to the original properties.

Yes u can add as many controls.

can't understand third Q's??
 
custom controls

thanks for the reply

I have done an application already but the difference is now I have to create the controls myself and not use the controls already on the toolbox so I may need to alter the coding to reflect this

I can attach the coding I have done previously if that is helpful to you hopefully you can understand where I'm coming from now

Thanks
 
Sorry i didn't get back to you sooner, right. To answer your first two questions....

No, some properties will be inertited from the base class.
Yes

And for the third it would be helpful if i could see you code so i can get a better idea of what your tring to do here.
 
Hi vis781 thanks for the reply

To give you a better idea of what I'm trying to achieve I'll show you 2 lots of coding, the first lot is the completed version 1 I've done and the second lot is the version 2 that I've done so far. The only difference between the 2 versions is that I have to create custom controls for version 2

Version 1 code

Imports System.Data.OleDb
Imports System.IO

Public
Class frmorders
Inherits System.Windows.Forms.Form
Public Class InvalidCustomerIDException
Inherits ApplicationException
Public Sub New()
MyBase.New("Invalid CustomerID")
End Sub
End Class

Private Sub frmorders_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Declares and instantiates a command object
Dim SqlCustomers As String = "SELECT * FROM customers"
'Sets the connection string
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=The Doughnut Shop.mdb"
'Declares and instantiates a new OleDbconnection object
Dim myconnection As OleDbConnection = New OleDbConnection(connString)
'Sets the connection string property
myconnection.ConnectionString = connString
'Declares and instantiates a data adapter
Dim dacustomers As OleDbDataAdapter = New OleDbDataAdapter(SqlCustomers, myconnection)
'Declares and instantiates a new data set
Dim ds As DataSet = New DataSet
'Fills the data adapter with the information from the customers table
dacustomers.Fill(ds, "customers")
'Declares and instantiates a command object
Dim sqlorders As String = "SELECT * FROM Orders "
'Declares and instantiates a datadapter
Dim daorders As OleDbDataAdapter = New OleDbDataAdapter(sqlorders, myconnection)
'Fills the data adapter with the orders table
daorders.Fill(ds, "orders")
'Declares and instantiates a new data relation linking together the customers and the orders table
Dim relation As DataRelation = New DataRelation("CustomersOrders", ds.Tables("Customers").Columns("CustomerID"), ds.Tables("Orders").Columns("CustomerID"))
'Adds the relation to the collection
ds.Relations.Add(relation)
'Sets the display member property
lstcustid.DisplayMember = "CustomerID"
'Instantiates a new data view for the customers table
lstcustid.DataSource = New DataView(ds.Tables("customers"))
'Binds the customers table details to the text boxes
txtfirstname.Text = ds.Tables("customers").Rows(0).Item("FirstName")
txtlastname.Text = ds.Tables("customers").Rows(0).Item("LastName")
txtcity.Text = ds.Tables("customers").Rows(0).Item("City")
txtstate.Text = ds.Tables("customers").Rows(0).Item("State")
txtzipcode.Text = ds.Tables("customers").Rows(0).Item("ZipCode")
End Sub

Private Sub lstcustid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstcustid.SelectedIndexChanged
' Try catch statement
Try
ValidateCustomerID(Integer.MinValue = 1)
ValidateCustomerID(
Integer.MaxValue = 6)
Catch ex As InvalidCustomerIDException
End Try
'Declares a data row view
Dim drv As DataRowView = CType(lstcustid.SelectedItem, DataRowView)
'Declares a data row
Dim childRows() As DataRow = drv.Row.GetChildRows("CustomersOrders")
txtfirstname.Text = drv("FirstName")
txtlastname.Text = drv("LastName")
txtcity.Text = drv("City")
txtstate.Text = drv("State")
txtzipcode.Text = drv("ZipCode")
'Sets the data grid data source property
dgorders.DataSource = drv.CreateChildView("CustomersOrders")
End Sub
Private Sub ValidateCustomerID(ByVal CustomerID As Integer)
If CustomerID < 0 Or CustomerID > 6 Then
Dim myException As New InvalidCustomerIDException
Throw New InvalidCustomerIDException
End If
End Sub

Private Sub dgorders_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgorders.DoubleClick
'Declares and instantiates a new form
Dim myForm As New frmitems
'This sets the index of the current row and the column number
Dim int As Integer = CStr(dgorders.Item(dgorders.CurrentRowIndex, 0).ToString)
'Sets the value of the customer ID property
myForm.CustID = int
'Displays the second form
myForm.ShowDialog()
End Sub

End
Class

Imports System.Data.OleDb
Imports System.IO
Public Class frmitems
Inherits System.Windows.Forms.Form
' Declares an integer variable to hold the value of the property
Private ID As Integer

Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
End
End Sub
'A property which can be set once the form is created
Public Property CustID() As Integer
Get
Return ID
End Get
Set(ByVal Value As Integer)
ID = Value
End Set
End Property

Private Sub frmitems_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Declares and instantiates command objects
Dim sqlOrderItems As String = "SELECT * FROM Items"
Dim sqlOrders As String = "SELECT * FROM orders"
'Sets the connection string
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=The Doughnut Shop.mdb"
'Declares and instantiates a new OleDbconnection object
Dim myconnection As OleDbConnection = New OleDbConnection(connString)
'Sets the connection string property
myconnection.ConnectionString = connString
'Declares and instantiates a data adapter
Dim daOrderItems As OleDbDataAdapter = New OleDbDataAdapter(sqlOrderItems, myconnection)
Dim daOrders As OleDbDataAdapter = New OleDbDataAdapter(sqlOrders, myconnection)
'Declares and instantiates a new data set
Dim ds As DataSet = New DataSet
'Fills the data adapter with the items table
daOrderItems.Fill(ds, "Items")
'Fills the data adapter with the orders table
daOrders.Fill(ds, "Orders")
'Declares and instantiates a new data relation linking together the items table and the orders table
Dim relation As DataRelation = New DataRelation("OrdersItems", ds.Tables("Orders").Columns("OrderID"), ds.Tables("Items").Columns("OrderID"))
ds.Relations.Add(relation)
'A DataView based on the Items table
Dim dv As New DataView(ds.Tables("Items"))
'Sets the RowFilter to the value of the CustID property
dv.RowFilter = "OrderID = '" & CustID & " ' "
'This sets the DataSource of the DataGrid to the DataView
dgitems.DataSource = dv


End Sub
End
Class

Version 2 code so far

Public
Class ctlCustomerDetails
Inherits System.Windows.Forms.UserControl
Private ReadOnly strFirstName As String
Private ReadOnly strLastName As String
Private ReadOnly strCity As String
Private ReadOnly strState As String
Private ReadOnly strZipCode As String
Private strCustomerID As String

Public ReadOnly Property FirstName() As String
Get
Return strFirstName
End Get

End Property
Public ReadOnly Property LastName() As String
Get
Return strLastName
End Get

End Property
Public ReadOnly Property City() As String
Get
Return strCity
End Get

End Property
Public ReadOnly Property State() As String
Get
Return strState
End Get

End Property
Public ReadOnly Property ZipCode() As String
Get
Return strZipCode
End Get

End Property
Public Property CustomerID() As String
Get
Return strCustomerID
End Get
Set(ByVal Value As String)
strCustomerID = Value
End Set
End Property
End
Class

If you need any more info let me know

Thanks for your help

 
Ok, so can i assume then that version 2 is the user control than is going to have the 5 textboxes and the combobox on it. If i'm right then you are heading in the right direction. You have started to add the properties to the user control. All this from what i can see is fine. What you will need to do now is to start to add functionality to your user control to make it do, what you want it to do. For example, for your combo box 'OnSelectedIndexChanged' You could add some functionality you do the things that you have programmed it to do inside version 1.

making any sense?
 
Thanks for the reply

Yes got a better idea of it now but trying to do things one at a time

I'm sure I need to raise a custom load method for the first user control which contains the textboxes and the combobox, so that the default values appear in the textboxes and combobox at the start

Not sure on how to do this, again any tips/coding would be great

Thanks for your time
 
ok, im not entirely sure what you mean by default values, but if you mean something such as a value that you personally want to put in there then you can place them in, in the constructor of the usercontrol i.e

[Public Sub New()
Mytextbox1. text = YourDefaultvalue
End Sub]

Or if you mean that you want it to automatically bind to the datasource and present some information then your right, this could happen in the 'On Load' event of your usercontrol. In there you could write some code to retrieve a connection string, connect to a database, and bind the controls to the data that you want. I would rather not try to give you some code at the moment as i'm not at my pc right now, and i dont want to give you false information, but i'll try to sort something out, if you could give me an idea on what exactly what you want this usercontrol to do in the onload event.
 
Thanks for the reply

What I mean by the default values is when I did the first application, when the form initially loaded the first customer id is shown in the listbox with the corresponding info in the 5 textboxes

So in the first user control. I want to show the details for the first customer id in the listbox and in the 5 textboxes then allow the user to select different customer id's with the different info shown in the textboxes

I'm trying to get this done first before worrying about the other 2 user controls which inherit from datagrid

If you need any more info let me know

Thanks again
 
So based on what you have said it sounds to me that you are going in the right direction. I'm not so sure i would put this in the on load event though. I think i would be more inclined to create a public function that can say, accept a connection string and the fields that you want your user control to bind to. This way atleast your control will be more flexible. An example may be....

[Public Function setup(ByVal ConnString As String, ByVal dept as string......)

Then inside this function you can then begin to manipulate the information passed in, and retun the values that you want. There will be literally hundreds of different ways to do this, so best just to play around and see what gives the best results.
 
Thanks for the reply

Is it just a matter of putting all the arguments in then using the code in the previous assignment to load the values in the textboxes

Thanks again
 
Back
Top