Click here to Skip to main content
15,886,864 members
Articles / Programming Languages / C#

MyXAML--XAML-style gui generator (added styles)

Rate me:
Please Sign up or sign in to vote.
4.85/5 (76 votes)
9 Mar 2004CPOL11 min read 395.3K   165  
Generate controls, set property values, and wire up event handlers at runtime from an XML definition.
<?xml version="1.0" encoding="utf-8"?>
<UI xmlns="System.Windows.Forms, System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
    xmlns:ka="OutlookBar, OutlookBarAssembly, Version=1.0.1.2, Culture=neutral, PublicKeyToken=null"
    xmlns:def="Definition">
  <Canvas Name='PropertyTest' FormBase='Form' Location='10, 10' Size='600, 330' Visible='false' BackColor='LightSteelBlue' Closing='OnCloseEvent' Load='PropertyTestLoaded' Text='Simple Test Form' AcceptButton='{OKButton}' CancelButton='{CancelButton}'>
    <Menu>
      <MenuItems>
        <MenuItem Text='&amp;File' Popup='OnPopup'>
          <MenuItems>
            <MenuItem Text='New'/>
            <MenuItem Text='-'/>
            <MenuItem Text='E&amp;xit' Click='ExitApp'/>
          </MenuItems>
        </MenuItem>
        <MenuItem Text='&amp;Edit'/>
        <MenuItem Text='&amp;View'/>
      </MenuItems>
    </Menu>
    <Controls>
      <Button def:Name='OKButton' Location='225, 10' Size='80, 25' Visible='true' BackColor='Control' Click='OnOK' FlatStyle='System' Text='&amp;OK'/>
      <Button def:Name='CancelButton' Location='225, 35' Size='80, 25' Visible='true' BackColor='Control' Click='OnCancel' FlatStyle='System' Text='&amp;Cancel'/>
      <Button Location='225, 100' Size='80, 25' Visible='true' BackColor='Control' Click='OnMyEvent' FlatStyle='System'>Click Event!</Button>
      <Label Location='225, 65' Size='120, 40' Visible='true' Font='French Script MT, 22pt, style=Bold'>Font Test!</Label>
      <Label Location='10, 12' Size='50, 15' Visible='true'>Edit 1:</Label>
      <Label Location='10, 37' Size='50, 15' Visible='true'>Edit 2:</Label>
      <TextBox Location='60, 10' Size='100, 20' Visible='true'/>
      <TextBox Location='60, 35' Size='100, 20' Visible='true' Enabled='false'>Disabled</TextBox>
      <Panel BorderStyle='Fixed3D' Top='70' Left='20' Size='200, 178' Visible='true' BackgroundImage='jojo.jpg' Anchor='Bottom, Left'/>
      <GroupBox Location='225, 130' Size='120, 60' Visible='true' Text='First Group Box' Font='MS Sans Serif, 8pt, style=Bold'>
        <Controls>
          <RadioButton Location='10, 15' Size='70, 15' Visible='true' Font='MS Sans Serif, 8pt' Load='InitialRadioButtonState'>Like</RadioButton>
          <RadioButton Location='10, 30' Size='70, 15' Visible='true' Font='MS Sans Serif, 8pt'>Don't Like</RadioButton>
        </Controls>
      </GroupBox>
      <GroupBox Canvas="GroupBox2"/>
      <TabControl Location='350, 10' Size='240, 250' Visible='true'>
        <TabPages Canvas='TabPage1'/>
        <TabPages Canvas='TabPage2'/>
        <TabPages Name='TabPage3' Text='ListView' Load='PopulateListView'>
          <Controls>
            <ListView Location='5, 10' Size='222, 210' HeaderStyle='Nonclickable' FullRowSelect='true' GridLines='true' View='Details'>
              <Columns Width='150' TextAlign='Left'>State</Columns>
              <Columns Width='50' TextAlign='Center'>Abbr.</Columns>
            </ListView>        
          </Controls>
        </TabPages>
        <TabPages Canvas='TabPage4'/>
      </TabControl> 
    </Controls>
  </Canvas>
  
  <Canvas Name='GroupBox2' FormBase='GroupBox' Location='225, 200' Size='120, 60' Visible='true' Text='Second Group Box' Font='MS Sans Serif, 8pt, style=Bold'>
    <Controls>
      <RadioButton Location='10, 15' Size='70, 15' Visible='true' Font='MS Sans Serif, 8pt' Load='InitialRadioButtonState'>To Click</RadioButton>
      <RadioButton Location='10, 30' Size='100, 15' Visible='true' Font='MS Sans Serif, 8pt'>Or Not To Click</RadioButton>
    </Controls>
  </Canvas>

  <Canvas Name='TabPage1' FormBase='TabPage' Text='Custom Control'>
  <def:Code language='C#'>
  <reference assembly="System.Drawing.dll"/>
  <reference assembly="System.Windows.Forms.dll"/>
  <reference assembly="OutlookBarAssembly.dll"/>
    <![CDATA[
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Windows.Forms;
      
      using OutlookBar;
      
      class MyForm
      {
        private OutlookBar.OutlookBar outlookBar;
        
        // controls in the tab page do not exist yet when the code-behind is instantiated!
        public MyForm() {}
        
        public void OnLoad(object sender, EventArgs e)
        {
            outlookBar=(OutlookBar.OutlookBar)sender;
			IconPanel iconPanel1=new IconPanel();
			IconPanel iconPanel2=new IconPanel();
			IconPanel iconPanel3=new IconPanel();
			outlookBar.AddBand("Outlook Shortcuts", iconPanel1);
			outlookBar.AddBand("My Shortcuts", iconPanel2);
			outlookBar.AddBand("Other Shortcuts", iconPanel3);

			iconPanel1.AddIcon("Outlook Today", Image.FromFile("img1.ico"), new EventHandler(PanelEvent));
			iconPanel1.AddIcon("Calendar", Image.FromFile("img2.ico"), new EventHandler(PanelEvent));
			iconPanel1.AddIcon("Contacts", Image.FromFile("img3.ico"), new EventHandler(PanelEvent));
			iconPanel1.AddIcon("Tasks", Image.FromFile("img4.ico"), new EventHandler(PanelEvent));
			outlookBar.SelectBand(0);
        }
        
		public void PanelEvent(object sender, EventArgs e)
		{
			Control ctrl=(Control)sender;
			PanelIcon panelIcon=ctrl.Tag as PanelIcon;
			MessageBox.Show("#"+panelIcon.Index.ToString(), "Panel Event");
		}
      }
    ]]>
  </def:Code>
  <Controls>
    <ka:OutlookBar Location='10, 10' Size='150, 210' Visible='true' BorderStyle='FixedSingle' Load='MyForm.OnLoad'/>
  </Controls>
  </Canvas>

  <Canvas Name='TabPage2' FormBase='TabPage' Text='ListBox'>
  <def:CodeBehind Src="InitDataSource.cs">
    <reference assembly="System.Windows.Forms.dll"/>
  </def:CodeBehind>
    <Controls>
      <ListBox Name='States' Location='10, 10' Size='130, 210' DataSource='ListBoxDS.DataSource' DisplayMember='LongName' ValueMember='ShortName'
               SelectedValueChanged='ListBoxDS.ShowValue'/>
      <Label Location='150, 10' Size='60, 15' Visible='true'>By event:</Label>
      <TextBox Name='ByEvent' Location='150, 25' Size='75, 20' Visible='true' ReadOnly='true'/>
      <Label Location='150, 55' Size='60, 15' Visible='true'>By binding:</Label>
      <TextBox Location='150, 70' Size='75, 20' Visible='true' ReadOnly='true'>
        <DataBindings>Text, ListBoxDS.DataSource, ShortName</DataBindings>
	  </TextBox>               
    </Controls>
  </Canvas>

  <Canvas Name='TabPage4' FormBase='MyCodeBehindClass' Text='Inherit' Load='MyCodeBehindClass.OnLoad'>
  <def:CodeBehind Src="MyCodeBehind.cs">
    <reference assembly="System.Windows.Forms.dll"/>
  </def:CodeBehind>
    <Controls>
      <Label Location='10, 10' Size='150, 15' Visible='true'>Test</Label>
      <Label Location='10, 42' Size='50, 15' Visible='true'>Colors</Label>
      <ComboBox Name='Colors' Location='60, 40' Size='100, 100' DisplayMember='text' ValueMember='value'>
        <DataSource>
          <item value='1' text='Red'/>
          <item value='2' text='Green'/>
          <item value='3' text='Blue'/>
        </DataSource>
      </ComboBox>
    </Controls>
  </Canvas>
</UI>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions