Logo

XML and Text-Based Controls

 

Introduction

Besides the data grid, you can display the elements of an XML file in other controls of your application, although some controls make it easier than others.

 

Text-Based Controls

Text-based controls such as the Label and the TextBox are equipped with a property called DataBindings. The DataBindings property is a collection type which has other internal properties. Therefore, you can use the Text field of the DataBindings property to select the element that would be displayed in the control.

  1. Start Microsoft Visual C++ .Net or MS Visual Studio .Net and create a new Windows Forms Application named IntroXML2 with its default form
  2. To create an XML file, on the main menu, click Project -> Add New Item...
  3. In the Templates list, click XML File (.xml)
  4. In the Name box, replace the string with employees
     
    Add New Item
  5. Click Open
  6. Complete the file as follows:
     
    <?xml version="1.0" encoding="utf-8"?>
    <fullrecord>
      <employee>
        <fullname>Sylvie Aronson</fullname>
        <title>Accountant</title>
        <emailaddress>aronsons@bastia.edu</emailaddress>
        <maritalstatus>Married</maritalstatus>
      </employee>
      <employee>
        <fullname>Bertrand Yamaguchi</fullname>
        <title>Junior Programmer</title>
        <emailaddress>yamob1250@yahoo.com</emailaddress>
        <maritalstatus>Married</maritalstatus>
      <employee>
      </employee>
        <fullname>Anselme Bean</fullname>
        <title>Sales Representative</title>
        <emailaddress>binama@hotmail.com</emailaddress>
        <maritalstatus>Widow</maritalstatus>
      </employee>
      <employee>
        <fullname>Mauricette Thomas</fullname>
        <title>Communications Director</title>
        <emailaddress>youngintern@netscape.net</emailaddress>
        <maritalstatus>Divorced</maritalstatus>
      </employee>
      <employee>
        <fullname>Hermine Gray</fullname>
        <title>Sales Representative</title>
        <emailaddress>graynotdavis@emailcity.net</emailaddress>
        <maritalstatus>Single</maritalstatus>
      </employee>
    </fullrecord>
  7. Display the form and design it as follows:
     
    Control Name Text
    Label   Full Name:
    TextBox txtFullName  
    Label   Title:
    TextBox txtTitle  
    Label   Email Address:
    TextBox txtEmailAddress  
    Label   Marital Status:
    TextBox txtMaritalStatus  
  8. Using the same approach we followed for the grid, double-click an empty area of the form and change the file as follows:
     
    #pragma once
    
    
    namespace IntroXML2
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    	private: System::Windows::Forms::Label *  label1;
    	private: System::Windows::Forms::Label *  label2;
    	private: System::Windows::Forms::Label *  label3;
    	private: System::Windows::Forms::Label *  label4;
    	private: System::Windows::Forms::TextBox *  txtFullName;
    	private: System::Windows::Forms::TextBox *  txtTitle;
    	private: System::Windows::Forms::TextBox *  txtMaritalStatus;
    	private: System::Windows::Forms::TextBox *  txtEmailAddress;
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    		DataSet __gc *dsEmployees;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			. . . 
    			
    
    		}	
    private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    	 {
    		dsEmployees = __gc new DataSet("employees");
    
    		dsEmployees->ReadXml(S"employees.xml");
    				 
    		this->txtFullName->DataBindings->Add(new System::Windows::Forms::Binding(S"Text", this->dsEmployees, S"employee.fullname"));
    		this->txtTitle->DataBindings->Add(new System::Windows::Forms::Binding(S"Text", this->dsEmployees, S"employee.title"));
    		this->txtEmailAddress->DataBindings->Add(new System::Windows::Forms::Binding(S"Text", this->dsEmployees, S"employee.emailaddress"));
    		this->txtMaritalStatus->DataBindings->Add(new System::Windows::Forms::Binding(S"Text", this->dsEmployees, S"employee.maritalstatus"));
    	 }
    };
    }
  9. Test the application and notice that it displays the values of only the first record
  10. Close the form and return to MSVC
  11. If you want to navigate through records, add four buttons named txtFirstRecord, txtPreviousRecord, txtNextRecord, and txtLastRecord
     
  12. Double-click each of the navigation buttons and implement them as follows:
     
    private: System::Void btnFirstRecord_Click(System::Object *  sender, System::EventArgs *  e)
    {
        this->BindingContext->get_Item(this->dsEmployees, S"employee")->Position = 0;
    }
    
    private: System::Void btnPreviousRecord_Click(System::Object *  sender, System::EventArgs *  e)
    {
        this->BindingContext->get_Item(this->dsEmployees, S"employee")->Position = this->BindingContext->get_Item(this->dsEmployees, S"employee")->Position - 1;
    }
    
    private: System::Void btnNextRecord_Click(System::Object *  sender, System::EventArgs *  e)
    {
        this->BindingContext->get_Item(this->dsEmployees, S"employee")->Position = this->BindingContext->get_Item(this->dsEmployees, S"employee")->Position + 1;
    }
    
    private: System::Void btnLastRecord_Click(System::Object *  sender, System::EventArgs *  e)
    {
        this->BindingContext->get_Item(this->dsEmployees, S"employee")->Position = this->BindingContext->get_Item(this->dsEmployees, S"employee")->Count - 1;	
    }
  13. Test the application
 

Home Copyright © 2004-2010 FunctionX, Inc.