Post: C# MDI Form
09-28-2014, 10:17 PM #1
K3-
Bounty hunter
(adsbygoogle = window.adsbygoogle || []).push({}); Hey NGU today i learned about (MDI) Multiple Document Interface so i decided on making a tutorial on it to help other, may not be handy for some but some people would like to know in case for future programming. :yes: Enjoy

A Multiple Document Interface (MDI) programs can display multiple child windows inside them. This is in contrast to single document interface (SDI) applications, which can manipulate only one document at a time. Visual Studio Environment is an example of Multiple Document Interface (MDI) and notepad is an example of an SDI application. MDI applications often have a Window menu item with submenus for switching between windows or documents.

You must login or register to view this content.

Any windows can become an MDI parent, if you set the IsMdiContainer property to True.

    IsMdiContainer = true;


The following C# program shows a MDI form with two child forms. Create a new C# project, then you will get a default form Form1 . Then add two more forms in the project (Form2 , Form 3) . Create a Menu on your form and call these two forms on menu click event.

NOTE: If you want the MDI parent to auto-size the child form you can code like this.

    form.MdiParent = this;
form.Dock=DockStyle.Fill;
form.Show();


    using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
IsMdiContainer = true;
}

private void menu1ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
frm2.MdiParent = this;
}

private void menu2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 frm3 = new Form3();
frm3.Show();
frm3.MdiParent = this;
}
}
}



Factor Analysis (FA) QuickStart Sample (C#)

Illustrates how to perform a Factor Analysis using classes in the Extreme.Statistics.Multivariate namespace in C#.

    using System;

using Extreme.Mathematics;
using Extreme.Statistics;
using Extreme.Statistics.IO;
using Extreme.Statistics.Multivariate;

namespace Extreme.Statistics.Quickstart.CSharp {
/// <summary>
/// Demonstrates how to use classes that implement
/// Factor Analysis.
/// </summary>
class FactorAnalysisExample {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args) {
// This QuickStart Sample demonstrates how to perform
// a factor analysis on a set of data.
//
// The classes used in this sample reside in the
// Extreme.Statistics.Multivariate namespace.

// First, our dataset, 'm255.dta', from Professor James Sidanius.
// See https://www.ats.ucla.edu/stat/sas/output/factor.htm

// Note: tolerances used to test for convergence in factor analysis
// algorithms are usually set very low (around 0.001). As a result,
// when comparing results from different programs, usually only
// about the first 3 digits will be equal.

// The data is in Stata format. Use a matrix reader to load it into a matrix.
StataFileReader reader = new StataFileReader(@"..\..\..\..\Data\m255.dta");
VariableCollection col = reader.ReadVariables();
// We'll use only these columns:
string[] names = { "item13", "item14", "item15", "item16",
"item17", "item18", "item19", "item20", "item21",
"item22", "item23", "item24" };
// First, filter out any rows with missing values:
col.FilterMissingValues(names);

//
// Factor analysis
//

// We can construct FA objects in many ways. Since we have the data in a matrix,
// we use the constructor that takes a data matrix as input.
FactorAnalysis fa = new FactorAnalysis(col, names);
// We set the number of factors:
fa.NumberOfFactors = 3;
// and immediately perform the analysis:
fa.Compute();

// We can get the unrotated factors:
var unrotatedFactors = fa.GetUnrotatedFactors();
// We can get the contributions of each factor:
Console.WriteLine(" # Eigenvalue Difference Contribution Contrib. %");
foreach (Factor factor in unrotatedFactors) {
// and write out its properties
Console.WriteLine("{0,2}{1,12:F4}{2,11:F4}{3,14:F3}{4,10:F3}",
factor.Index, factor.Eigenvalue, factor.EigenvalueDifference,
factor.ProportionOfVariance,
factor.CumulativeProportionOfVariance);
}

Console.WriteLine("\nVarimax rotation");

// Here are the loadings for each of the variables:
Console.WriteLine("\nUnrotated loadings:");
Console.WriteLine("Variable 1 2 3 Uniqueness");
for (int i = 0; i < names.Length; i++) {
Console.WriteLine(" {0,8}{1,10:F5} {2,10:F5} {3,10:F5}{4,10:F5}",
names[i],
unrotatedFactors[0].Loadings[i],
unrotatedFactors[1].Loadings[i],
unrotatedFactors[2].Loadings[i],
fa.Uniqueness[i]);
}

// Now we'll look at the rotated factors:
var rotatedFactors = fa.GetRotatedFactors();
Console.WriteLine(" # Variance Difference Proportion Cumulative");
foreach (Factor factor in rotatedFactors) {
Console.WriteLine("{0,2}{1,12:F4}{2,11:F4}{3,13:F4}{4,11:F4}",
factor.Index, factor.VarianceExplained, "-",
factor.ProportionOfVariance,
factor.CumulativeProportionOfVariance);
}

// Here are the rotated loadings for each of the variables:
Console.WriteLine("\nRotated loadings (Varimax):");
Console.WriteLine("Variable 1 2 3 Uniqueness");
for (int i = 0; i < names.Length; i++) {
Console.WriteLine(" {0,8}{1,10:F5} {2,10:F5} {3,10:F5}{4,10:F5}",
names[i],
rotatedFactors[0].Loadings[i],
rotatedFactors[1].Loadings[i],
rotatedFactors[2].Loadings[i],
fa.Uniqueness[i]);
}

// And the matrix that rotates the factors
Console.WriteLine("Factor transformation matrix:\n{0:F4}",
fa.FactorTransformationMatrix);

Console.WriteLine("\nPromax rotation (power = 3)");

// Now let's use an (oblique) Promax rotation:
fa.RotationMethod = FactorRotationMethod.Promax;
fa.PromaxPower = 3;
fa.Compute();

// Now we'll look at the rotated factors:
Console.WriteLine("\nRotated factor variance explained:");
rotatedFactors = fa.GetRotatedFactors();
Console.WriteLine(" # Variance");
foreach (Factor factor in rotatedFactors) {
Console.WriteLine("{0,2}{1,12:F4}",
factor.Index, factor.VarianceExplained);
}


// Here are the rotated loadings for each of the variables:
Console.WriteLine("\nRotated loadings/pattern (Promax):");
Console.WriteLine("Variable 1 2 3 Communality Uniqueness");
for (int i = 0; i < names.Length; i++) {
// and write out its properties
Console.WriteLine(" {0,8}{1,10:F5}{2,10:F5}{3,10:F5}{4,10:F5} {5,10:F5}",
names[i],
rotatedFactors[0].Loadings[i],
rotatedFactors[1].Loadings[i],
rotatedFactors[2].Loadings[i],
fa.Communalities[i],
fa.Uniqueness[i]);
}

// Here are the rotated loadings for each of the variables:
Console.WriteLine("\nRotated factor structure:");
Console.WriteLine("Variable 1 2 3");
for (int i = 0; i < names.Length; i++) {
// and write out its properties
Console.WriteLine(" {0,8}{1,10:F5} {2,10:F5} {3,10:F5}",
names[i],
rotatedFactors[0].Structure[i],
rotatedFactors[1].Structure[i],
rotatedFactors[2].Structure[i]);
}

// For oblique rotations, the factors are usually correlated:
Console.WriteLine("Factor correlation matrix:\n{0:F4}",
fa.FactorCorrelationMatrix);

//
// Factor analysis on a correlation matrix
//

Console.WriteLine("\nUsing a correlation matrix");

// This example is from Exploratory Factor Analysis
// https://www.oup.com/us/companion.websites/9780199734177/supplementary/example/
double[] values = {
1.000, 0.666, 0.150, 0.617, 0.541, 0.653, 0.473, 0.549, 0.566,
0.666, 1.000, 0.247, 0.576, 0.510, 0.642, 0.425, 0.544, 0.488,
0.150, 0.247, 1.000, 0.222, 0.081, 0.164, 0.091, 0.181, 0.120,
0.617, 0.576, 0.222, 1.000, 0.409, 0.560, 0.338, 0.448, 0.349,
0.541, 0.510, 0.081, 0.409, 1.000, 0.667, 0.734, 0.465, 0.754,
0.653, 0.642, 0.164, 0.560, 0.667, 1.000, 0.596, 0.540, 0.672,
0.473, 0.425, 0.091, 0.338, 0.734, 0.596, 1.000, 0.432, 0.718,
0.549, 0.544, 0.181, 0.448, 0.465, 0.540, 0.432, 1.000, 0.412,
0.566, 0.488, 0.120, 0.349, 0.754, 0.672, 0.718, 0.412, 1.000
};
var R = Matrix.CreateSymmetric(9, values, MatrixTriangle.Upper, true);
fa = new FactorAnalysis(R, FactorMethod.Correlation);
fa.NumberOfFactors = 2;
fa.ExtractionMethod = FactorExtractionMethod.MaximumLikelihood;
fa.RotationMethod = FactorRotationMethod.Varimax;
fa.Compute();

names = new string[] { "Hugs", "Comps", "PerAd", "SocAd", "ProAd",
"ComSt", "PhyHlp", "Encour", "Tutor" };

// Here are the initial:
Console.WriteLine("\nRotated factor loadings:");
Console.WriteLine("Variable Initial Extracted");
for (int i = 0; i < names.Length; i++) {
// and write out its properties
Console.WriteLine(" {0,8}{1,10:F5} {2,10:F5}",
names[i],
fa.InitialCommunalities[i],
fa.Communalities[i]);
}

// Here are the rotated loadings for each of the variables:
// Note that in the SPSS output, the ordering of the variables
// is different.
unrotatedFactors = fa.GetUnrotatedFactors();
Console.WriteLine("\nUnrotated factor loadings:");
Console.WriteLine("Variable 1 2");
for (int i = 0; i < names.Length; i++) {
// and write out its properties
Console.WriteLine(" {0,8}{1,10:F5} {2,10:F5}",
names[i],
unrotatedFactors[0].Loadings[i],
unrotatedFactors[1].Loadings[i]);
}

// Here are the rotated loadings for each of the variables:
rotatedFactors = fa.GetRotatedFactors();
Console.WriteLine("\nRotated factor loadings:");
Console.WriteLine("Variable 1 2");
for (int i = 0; i < names.Length; i++) {
// and write out its properties
Console.WriteLine(" {0,8}{1,10:F5} {2,10:F5}",
names[i],
rotatedFactors[0].Loadings[i],
rotatedFactors[1].Loadings[i]);
}

Console.Write("Press any key to exit.");
Console.ReadLine();
}
}
}


Pichu Told me that you can alter whether a form is MDI or not through the form properties options in the "Window Style" menu selection. wich is something i didn't realise, thanks.
Last edited by K3- ; 10-12-2014 at 05:24 PM.

The following user thanked K3- for this useful post:

Pichu
10-01-2014, 06:30 AM #2
Pichu
RIP PICHU.
Good tutorial although you can alter whether a form is MDI or not through the form properties options in the "Window Style" menu selection.
10-01-2014, 10:03 PM #3
K3-
Bounty hunter
Originally posted by Pichu View Post
Good tutorial although you can alter whether a form is MDI or not through the form properties options in the "Window Style" menu selection.

aw thanks Pichu i never actually realised, i'll add that into the thread.
10-01-2014, 11:13 PM #4
Pichu
RIP PICHU.
Originally posted by K3
aw thanks Pichu i never actually realised, i'll add that into the thread.


Yea, it's very handy. TBH, I forgot all about MDI forms up until this thread aha. I was working on a personal project not too long ago and this would have been helpful to remember, XD
10-01-2014, 11:15 PM #5
K3-
Bounty hunter
Originally posted by Pichu View Post
Yea, it's very handy. TBH, I forgot all about MDI forms up until this thread aha. I was working on a personal project not too long ago and this would have been helpful to remember, XD

maybe you could continue that project now Smile
10-01-2014, 11:31 PM #6
Pichu
RIP PICHU.
Originally posted by K3
maybe you could continue that project now Smile


I finished it already but just without MDI.

You should expand this tutorial for others and show integration factors with other forms interacting with one another while in the MDI state.

It's actually a good thing for people to know how to do, especially when dealing with data and charts/graphs.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo