Microsoft KB Archive/250577

= How To Use the Picture Box in a UserControl and in a DataRepeater Control =

Article ID: 250577

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition Service Pack 3
 * Microsoft ActiveX Data Objects 2.1
 * Microsoft ActiveX Data Objects 2.1 Service Pack 1
 * Microsoft ActiveX Data Objects 2.1 Service Pack 2
 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7

-



This article was previously published under Q250577



SUMMARY
This article demonstrates how to display a picture from a database in a picture box in a UserControl and in the Microsoft DataRepeater control.



MORE INFORMATION
When you bind a UserControl or DataRepeater control that contains a Picture Box, set the DataFormat of the bound picture box to Picture.

NOTE: To bind a picture to a field in a database, the field must be stored without the header and footer information of a Microsoft Access OLE field. An Access OLE field that contains a bitmap also contains header and footer information, which prevents binding a picture box to the field.

Sample Code
To create this sample project you must:
 * Create a sample database.


 * Create the UserControl.


 * Create the test project.

Create a Sample Database
The following project creates a Microsoft Access 2000 database and a table in the database. It then populates the table with a record that contains a Picture object from your Windows directory. The picture is inserted as binary data, not as an Access OLE field.

Note that you only have to run this project once to create the database.

 In Visual Basic, create a new Standard EXE project. Form1 is created by default. From the Project menu, select References. In the list of available references, select the following:  Microsoft ActiveX Data Objects Library

 Microsoft ADO Ext. for DDL and Security

  Paste the following code into the code window of Form1's Form_Load Event: 'This code creates an Access 2000 database named C:\Picture.mdb. 'It then creates a table within the .mdb named PicTable with two fields 'one named Picture and the other named Description. 'Finally, it loads one record into the table.

'Create a new Access 2000 database. Dim cat As ADOX.Catalog Set cat = New ADOX.Catalog cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Picture.mdb" 'Create a new table in the new Access 2000 database. Dim tbl As ADOX.Table Set tbl = New ADOX.Table tbl.Name = "PicTable" tbl.Columns.Append "Picture", adLongVarBinary tbl.Columns.Append "Description", adVarWChar, 50 cat.Tables.Append tbl

'Open the new table. Dim RS As ADODB.Recordset Set RS = New ADODB.Recordset RS.CursorLocation = adUseClient RS.Open "Select * from PicTable","Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Picture.mdb", adOpenStatic, adLockOptimistic

'Check the following paths and make sure there is a valid path to the .bmp files. Dim MyArray As Byte 'If you use Windows 95 or Windows 98 then use This Line Open "C:\Windows\Bubbles.bmp" For Binary As #1 'If you use Windows NT then use this line 'Open "C:\Winnt\Blue Monday.bmp" For Binary As #1 ReDim MyArray(LOF(1)) Get #1,, MyArray Close #1

'Add a Record containing a Picture to the new table. RS.AddNew RS(0).AppendChunk MyArray RS(1) = "This is Picture 1" RS.Update

'Clean up  RS.Close Set RS = Nothing Set tbl = Nothing Set cat = Nothing </li> If you are running on Microsoft Windows NT, comment out the line:

Open "C:\Windows\Bubbles.bmp" For Binary As #1

and uncomment the line:

'Open "C:\Winnt\Blue Monday.bmp" For Binary As #1

If needed, further modify the path to the Bubbles.bmp file or the Blue Monday.bmp file to the folders on your system.</li> Run this code once.

You should now have a new Access 2000 Database that has one record in it.</li> Exit Visual Basic.</li></ol>

Create the UserControl
<ol> In Visual Basic, create a new ActiveX control project. UserControl1 is created by default.</li> Right-click the UserControl1 and then select Properties. Change the text in the Name property of UserControl1 to MyUserControl.</li> Place a PictureBox control onto MyUserControl. Change the PictureBox's Name property to MyPicture.</li> Place a TextBox control onto MyUserControl. Change the TextBox's Name property to MyText.</li> From the Add-Ins menu, select Add-In Manager. From the list of available add-ins, select:

VB 6 ActiveX Ctrl Interface Wizard.

</li> Clear the Loaded/Unloaded check box and then click OK.</li> From the Add-Ins menu, select the ActiveX Control Interface Wizard..</li> In the Introduction dialog box of the ActiveX Control Interface Wizard, select Next.</li> In the Select Interface Members dialog box, click << to clear the list of names, and then click Next.</li> In the Create Custom Interface Members dialog box, click New. <ol style="list-style-type: lower-alpha;"> In the Name text box, type MyPictureData. Note that this is a property.

</li> Click OK.</li></ol> </li> <li>In the Create Custom Interface Members dialog box, click New. <ol style="list-style-type: lower-alpha;"> <li>In the Name text box, type MyPictureText. Note that this is a property.

</li> <li>Click OK and then click Next.</li></ol> </li> <li>In the Set Mapping dialog box, select MyPictureData from the list of Public Names. <ol style="list-style-type: lower-alpha;"> <li>In the Maps to Control list, select MyPicture.</li> <li>In the Member list, select Picture.</li></ol> </li> <li>Select MyPictureText from the list of Public Names. <ol style="list-style-type: lower-alpha;"> <li>In the Maps to Control list, select MyText.</li> <li>In the Member list, select Text.</li></ol> </li> <li>Click Next and then click Finish.</li> <li> In the Summary dialog box, click Close. The code created by the ActiveX Control Interface Wizard should look like the following: 'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES! 'MappingInfo=MyPicture,MyPicture,-1,Picture Public Property Get MyPictureData As Picture Set MyPictureData = MyPicture.Picture End Property

Public Property Set MyPictureData(ByVal New_MyPictureData As Picture) Set MyPicture.Picture = New_MyPictureData PropertyChanged "MyPictureData" End Property

'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES! 'MappingInfo=MyText,MyText,-1,Text Public Property Get MyPictureText As String MyPictureText = MyText.Text End Property

Public Property Let MyPictureText(ByVal New_MyPictureText As String) MyText.Text = New_MyPictureText PropertyChanged "MyPictureText" End Property

'Load property values from storage Private Sub UserControl_ReadProperties(PropBag As PropertyBag) Set Picture = PropBag.ReadProperty("MyPictureData", Nothing) MyText.Text = PropBag.ReadProperty("MyPictureText", "Text1") End Sub

'Write property values to storage Private Sub UserControl_WriteProperties(PropBag As PropertyBag) Call PropBag.WriteProperty("MyPictureData", Picture, Nothing) Call PropBag.WriteProperty("MyPictureText", MyText.Text, "Text1") End Sub </li> <li>On the Tools menu, select Procedure Attributes. <ol style="list-style-type: lower-alpha;"> <li>Select MyPictureData in the Name list. Click Advanced.</li> <li>In the Data Binding area, select the Property Is Data Bound check box.</li> <li>Select the Show DataBindings Collection At Design Time check box.</li> <li>Select MyPictureText in the Name list. Select the Property Is Data Bound and Show DataBindings Collection At Design Time check boxes again.</li> <li>Click Apply.</li> <li>Click OK.</li></ol> </li> <li>On the File menu, select Make Project1.ocx. Change the file name to MyUserControl.ocx, and then click OK.

NOTE: Do not exit Visual Basic, because you need to add a test project.</li></ol>

Create the Test Project
<ol> <li>On the File menu, select Add Project. Create a new Standard EXE project. Form1 is created by default.</li> <li>On the Project menu, select Components. From the list of available Components, select the following: <ul> <li>Microsoft ADO Data Control (OLE DB)</li> <li>Microsoft DataRepeater Control (OLE DB) to the tool bar.</li></ul>

</li> <li>Add a ADO DataControl to Form1. ADODC1 is created by default. <ol style="list-style-type: lower-alpha;"> <li>Set the ConnectionString property of ADODC1 to the Pictures database created previously, using Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Picture.mdb.</li> <li>Set the RecordSource property of ADODC1 to the PicTable table.</li></ol> </li> <li>Add a DataRepeater control to the form. DataRepeater1 is created by default. <ol style="list-style-type: lower-alpha;"> <li>Set the DataSource of DataRepeater1 to ADODC1.</li> <li>Select the RepeatedControlName property. From the list of available controls, select MyUserControl.UserControl1.</li> <li>Select the Custom property. Click the ellipsis (...) button to open the Property dialog box.</li> <li>In the Property dialog box, select the RepeaterBindings tab. From the list of PropertyNames, select MyPictureData. From the list of DataFields, select Picture and then click Add.</li> <li>Select the Format tab. From the list of Format Items, select MyPictureData. For the Format Type, select Picture.</li> <li>Again, select the RepeaterBindings tab. From the list of PropertyNames, select MyPictureText. From the list of DataFields, select Text and then click Add. Click Apply and then click OK.</li></ol> </li> <li>In the Project Explorer, select the test project. Right-click the test project and then select Set as Startup.</li> <li>Run the project. There should be a picture and a description in the control.</li></ol>

<div class="references_section">