Friday, July 8, 2011

WPF-Creating controls dynamically in WPF

Couple of days I wrote about creating ASP.Net controls dynamically in C#, following code explain how to create in WPF (using c#).



using System;

using System.Collections.Generic;

using System.Data;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Controls.Primitives;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Media.Animation;

using System.Windows.Navigation;

using System.Windows.Shapes;
namespace CompleWebCRF.Test
{

/// <summary>

/// Interaction logic for dynamicControl.xaml

/// </summary>

public partial class dynamicControl : Page

{
public dynamicControl()
{

InitializeComponent();



test();

}




private void test()
{
int iRow=0;int iCol = 0;
myGrid.RowDefinitions.Clear();
createLabel("Text Box", iRow, iCol);
CreateTextBox(iRow, iCol+1);



iRow++;
createLabel("List Box", iRow, iCol);
CreateListBox(iRow, iCol+1);



iRow++;
createLabel("Combo Box", iRow, iCol);
CreateComboBox(iRow, iCol+1);

iRow++;
createLabel("Check Box", iRow, iCol);
CreateCheckBox(iRow, iCol+1);

iRow++;
createLabel("Radio Button", iRow, iCol);
CreateRadioButton(iRow, iCol+1);

CreateRadioButton(iRow, iCol+2);

iRow++;

CreateButton(iRow, iCol);





}
private void createLabel(string lblName, int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
Label tb = new Label();tb.Name = "LabelID" + iRow;
tb.Content = lblName;

Grid.SetColumn(tb, iCol);
Grid.SetRow(tb, iRow);


tb.Width = 200;

tb.Height = 50;

myGrid.Children.Add(tb);

}
private void CreateButton(int iRow,int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());Button b = new Button();


b.Click += Button_Click;

b.Width = 70;

b.Height=30;


b.Content = "Save";

Grid.SetColumn(b, iCol);Grid.SetRow(b, iRow);
myGrid.Children.Add(b);

}




private void Button_Click(object sender, RoutedEventArgs e)
{
foreach (Control c in myGrid.Children)
{
if (c is TextBox)
{

TextBox txtResponse = c as TextBox;
MessageBox.Show("TextBox -- Name: " + txtResponse.Name.ToString() + " ,Value: " + txtResponse.Text.ToString());
}
if (c is CheckBox)
{

CheckBox objCheckBox = c as CheckBox;
MessageBox.Show("CheckBox -- Name: " + objCheckBox.Name.ToString()+ " ,Item Selected: " + objCheckBox.IsChecked.ToString() );
}
if (c is ComboBox)
{
ComboBox objComboBox = c as ComboBox;
if (objComboBox.SelectedValue==null)
MessageBox.Show("ComboBox--Name: " + objComboBox.Name.ToString() + " ,Item Selected: " + objComboBox.SelectedIndex.ToString() + " ,Value Selected: Null " );
else
MessageBox.Show("ComboBox--Name: " + objComboBox.Name.ToString() + " ,Item Selected: " + objComboBox.SelectedIndex.ToString() + " ,Value Selected: " + objComboBox.SelectedValue.ToString());
}
if (c is ListBox)
{
ListBox objListBox = c as ListBox;
if (objListBox.SelectedValue == null)
MessageBox.Show("ListBox--Name: " + objListBox.Name.ToString() + " ,Item Selected: " + objListBox.SelectedIndex.ToString() + " ,Value Selected: Null ");
else
MessageBox.Show("ListBox--Name: " + objListBox.Name.ToString() + " ,Item Selected: " + objListBox.SelectedIndex.ToString() + " ,Value Selected: " + objListBox.SelectedValue.ToString());
}


if (c is RadioButton)
{
RadioButton objRadioButton = c as RadioButton;
//if (objRadioButton.SelectedValue == null)

// MessageBox.Show("Radio Button--Name: " + objRadioButton.Name.ToString() + " ,Item Selected: " + objRadioButton.SelectedIndex.ToString() + " ,Value Selected: Null ");

//else

// MessageBox.Show("Radio Button--Name: " + objRadioButton.Name.ToString() + " ,Item Selected: " + objRadioButton.SelectedIndex.ToString() + " ,Value Selected: " + objRadioButton.SelectedValue.ToString());
String strGroupName = objRadioButton.GroupName;
MessageBox.Show(strGroupName.ToString());
MessageBox.Show(objRadioButton.IsChecked.ToString());
//lstBox.GroupName



}



}

}
private void CreateTextBox(int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());

TextBox tb = new TextBox();
tb.Name = "textBox" + iRow;


Grid.SetColumn(tb, iCol);
Grid.SetRow(tb, iRow);
tb.TextWrapping = TextWrapping.Wrap;tb.Text =iRow+
"When we enter value, they should be entered here.";
tb.Width=200;

tb.Height = 50;



myGrid.Children.Add(tb);

}
private void CreateListBox(int iRow, int iCol)
{

myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());


ListBox lstBox = new ListBox();
Grid.SetColumn(lstBox, iCol);Grid.SetRow(lstBox, iRow);
lstBox.Width = 300;

lstBox.Height = 100;


CreateListBoxItem(lstBox, "Asia");
CreateListBoxItem(lstBox, "Australia");CreateListBoxItem(lstBox,
"Antarctica");
myGrid.Children.Add(lstBox);

}
private void CreateListBoxItem(ListBox lstBox, String sItem)
{
ListBoxItem lstBoxItem1 = new ListBoxItem();
lstBoxItem1.Content = sItem;

lstBox.Items.Add(lstBoxItem1);

}




private void CreateComboBox(int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
ComboBox cmbBox = new ComboBox();
Grid.SetColumn(cmbBox, iCol);
Grid.SetRow(cmbBox, iRow);
cmbBox.Width = 100;

cmbBox.Height = 40;


CreateComboBoxItem(cmbBox, "Apples");
CreateComboBoxItem(cmbBox, "Banana");cmbBox.Name =
"ComboBox_" + iRow;
myGrid.Children.Add(cmbBox);

}
private void CreateComboBoxItem(ComboBox cmbBox,String sItem)
{
ComboBoxItem cmbBoxItem1 = new ComboBoxItem();
cmbBoxItem1.Content = sItem;

cmbBox.Items.Add(cmbBoxItem1);

}




private void CreateCheckBox(int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
CheckBox chkBox = new CheckBox();
Grid.SetColumn(chkBox, iCol);
Grid.SetRow(chkBox, iRow);
chkBox.Width = 100;

chkBox.Height = 40;
chkBox.Name = "CheckBox_" + iRow;chkBox.Content = "item 1";


myGrid.Children.Add(chkBox);



}
private void CreateRadioButton(int iRow, int iCol)
{
myGrid.RowDefinitions.Add(new RowDefinition());
myGrid.ColumnDefinitions.Add(new ColumnDefinition());
RadioButton lstBox = new RadioButton();
Grid.SetColumn(lstBox, iCol);
Grid.SetRow(lstBox, iRow);
lstBox.Width = 100;

lstBox.Height = 40;
lstBox.Content = "item 1";lstBox.GroupName = "A";


myGrid.Children.Add(lstBox);





}







}

}

No comments:

Post a Comment

Open default email app in .NET MAUI

Sample Code:  if (Email.Default.IsComposeSupported) {     string subject = "Hello!";     string body = "Excellent!";    ...