Skip to content

WinForms -> XAML

February 10, 2007

I’ve thought about writing a converter that walked through InitializeComponent in a Form class and created the corresponding XAML.  Today, I spent an hour and wrote a quick routine meant to be embedded into a form to create xaml from it.  It is very basic…works on my one example that I tested…but was fun to write none the less…

I belive I remember seeing a commercial package that did this, but I couldn’t find it with a few web searches.

The form I was trying to convert was a VB.Net 1.1 calculator that a computer science teacher in Florida uses in his teaching.  I found his post because I was searching for blogs that mentioned VB and WPF.

  • Calculator Skeleton (vb.net 1.1 project)
  • Teaching A Windows Form Application (Calculator) (slide deck)

    I opened that project, created a Form Load event and created the following code:

    Basic Converter Code:

        Private Sub Calculator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim sb As System.Text.StringBuilder
            sb = New System.Text.StringBuilder()
            sb.AppendLine("<Window xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""&quot;)
            sb.AppendLine("    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""&quot;)
            WriteBrushAttribute(sb, "Background", Me.BackColor, "0")
            WriteBrushAttribute(sb, "Foreground", Me.ForeColor, "ControlText")
            WriteAttribute(sb, "Canvas.Left", Me.Location.X.ToString(), "0")
            WriteAttribute(sb, "Canvas.Top", Me.Location.Y.ToString(), "0")
            WriteAttribute(sb, "Width", Me.Size.Width.ToString(), "0")
            WriteAttribute(sb, "Height", Me.Size.Height.ToString(), "0")
            sb.AppendLine("    >")

            WalkControls(sb, Me)

            sb.AppendLine("</Window>")
            Clipboard.SetData(DataFormats.Text, sb.ToString())

        End Sub

        Private Sub WalkControls(ByVal sb As System.Text.StringBuilder, ByVal parentControl As Control)
            sb.AppendLine("    <Canvas>")
            For Each control As Control In parentControl.Controls
                sb.Append("        <" + control.GetType().Name)
                WriteBrushAttribute(sb, "Background", control.BackColor, "0")
                WriteBrushAttribute(sb, "Foreground", control.ForeColor, "ControlText")
                sb.Append(" FontSize=""" + control.Font.SizeInPoints.ToString() + "pt""")
                sb.Append(" FontFamily=""" + control.Font.FontFamily.Name + """")
                WriteAttribute(sb, "Canvas.Left", control.Location.X.ToString(), "0")
                WriteAttribute(sb, "Canvas.Top", control.Location.Y.ToString(), "0")
                WriteAttribute(sb, "Width", control.Size.Width.ToString(), "0")
                WriteAttribute(sb, "Height", control.Size.Height.ToString(), "0")
                WriteAttribute(sb, "TabIndex", control.TabIndex.ToString(), "0")
                WriteAttribute(sb, "Content", control.Text, "")
                WriteAttribute(sb, "Name", control.Name, "")

                If control.Controls.Count = 0 Then
                    sb.AppendLine("/>")
                Else
                    sb.AppendLine(">")
                    WalkControls(sb, control)
                    sb.AppendLine("</" + control.GetType().Name + ">")
                End If
            Next
            sb.AppendLine("    </Canvas>")
        End Sub

        Private Sub WriteAttribute(ByVal sb As System.Text.StringBuilder, ByVal name As String, ByVal value As String, ByVal defaultValue As String)
            If value <> defaultValue Then
                sb.Append(" " + name + "=""" + value + """")
            End If
        End Sub

        Private Sub WriteBrushAttribute(ByVal sb As System.Text.StringBuilder, ByVal name As String, ByVal value As Color, ByVal defaultValue As String)
            Dim finalValue As String
            finalValue = value.ToKnownColor.ToString()

            If value.IsNamedColor Then
                If finalValue = "Window" Then
                    finalValue = "{x:Static SystemColors.WindowBrush}"
                ElseIf finalValue = "ControlText" Then
                    finalValue = defaultValue
                ElseIf finalValue = "ActiveCaption" Then
                    finalValue = "{x:Static SystemColors.ActiveCaptionBrush}"
                ElseIf finalValue = "WindowText" Then
                    finalValue = "{x:Static SystemColors.WindowTextBrush}"
                End If
            End If

            If value.IsNamedColor Then
                WriteAttribute(sb, name, finalValue, defaultValue)
            Else
                Dim colorBuilder As System.Text.StringBuilder
                colorBuilder = New System.Text.StringBuilder
                colorBuilder.Append("#")
                colorBuilder.AppendFormat("{0:X2}", value.R)
                colorBuilder.AppendFormat("{0:X2}", value.G)
                colorBuilder.AppendFormat("{0:X2}", value.B)
                WriteAttribute(sb, name, colorBuilder.ToString(), defaultValue)
            End If
        End Sub

    Output XAML:

    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
    Background="#00C0C0" Canvas.Left="92" Canvas.Top="92" Width="440" Height="337"    >
        <Canvas>
            <GroupBox Background="#00C0C0" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="16" Canvas.Top="184" Width="304" Height="80" TabIndex="20" Name="grpone">
        <Canvas>
            <Button Background="Blue" Foreground="Yellow" FontSize="9pt" FontFamily="Broadway" Canvas.Left="56" Canvas.Top="32" Width="40" Height="24" TabIndex="4" Content="1/x" Name="butrecip"/>
            <Button Background="Blue" Foreground="Yellow" FontSize="11.25pt" FontFamily="Broadway" Canvas.Left="56" Canvas.Top="8" Width="40" Height="24" TabIndex="3" Content="sqrt" Name="butroot"/>
            <Button Background="Blue" Foreground="Yellow" FontSize="11.25pt" FontFamily="Broadway" Canvas.Top="56" Width="56" Height="24" TabIndex="2" Content="tan" Name="buttan"/>
            <Button Background="Blue" Foreground="Yellow" FontSize="11.25pt" FontFamily="Broadway" Canvas.Top="32" Width="56" Height="24" TabIndex="1" Content="cos" Name="butcos"/>
            <Button Background="Blue" Foreground="Yellow" FontSize="11.25pt" FontFamily="Broadway" Canvas.Top="8" Width="56" Height="24" Content="sin" Name="butsin"/>
        </Canvas>
    </GroupBox>
            <Button Background="Silver" Foreground="{x:Static SystemColors.ActiveCaptionBrush}" FontSize="11.25pt" FontFamily="Broadway" Canvas.Left="256" Canvas.Top="152" Width="64" Height="24" TabIndex="19" Content="^" Name="opexp"/>
            <Button Background="Yellow" FontSize="11.25pt" FontFamily="Broadway" Canvas.Left="72" Canvas.Top="64" Width="40" Height="24" TabIndex="18" Content="C" Name="butclear"/>
            <Button Background="Yellow" FontSize="11.25pt" FontFamily="Broadway" Canvas.Left="32" Canvas.Top="64" Width="40" Height="24" TabIndex="17" Content="CE" Name="butce"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="136" Canvas.Top="152" Width="40" Height="24" TabIndex="16" Content="+/-" Name="butpm"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="96" Canvas.Top="152" Width="40" Height="24" TabIndex="15" Content="." Name="butdot"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="56" Canvas.Top="152" Width="40" Height="24" TabIndex="14" Content="9" Name="but9"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="16" Canvas.Top="152" Width="40" Height="24" TabIndex="13" Content="8" Name="but8"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="136" Canvas.Top="128" Width="40" Height="24" TabIndex="12" Content="7" Name="but7"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="96" Canvas.Top="128" Width="40" Height="24" TabIndex="11" Content="6" Name="but6"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="56" Canvas.Top="128" Width="40" Height="24" TabIndex="10" Content="5" Name="but5"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="16" Canvas.Top="128" Width="40" Height="24" TabIndex="9" Content="4" Name="but4"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="136" Canvas.Top="104" Width="40" Height="24" TabIndex="8" Content="3" Name="but3"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="96" Canvas.Top="104" Width="40" Height="24" TabIndex="7" Content="2" Name="but2"/>
            <Button Background="MediumOrchid" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="56" Canvas.Top="104" Width="40" Height="24" TabIndex="6" Content="1" Name="but1"/>
            <Button Background="MediumOrchid" Foreground="Black" FontSize="8.25pt" FontFamily="Microsoft Sans Serif" Canvas.Left="16" Canvas.Top="104" Width="40" Height="24" TabIndex="5" Content="0" Name="but0"/>
            <Button Background="Red" FontSize="11.25pt" FontFamily="Broadway" Canvas.Left="192" Canvas.Top="152" Width="64" Height="24" TabIndex="4" Content="=" Name="op5"/>
            <Button Background="Silver" Foreground="{x:Static SystemColors.ActiveCaptionBrush}" FontSize="11.25pt" FontFamily="Broadway" Canvas.Left="256" Canvas.Top="128" Width="64" Height="24" TabIndex="3" Content="/" Name="op4"/>
            <Button Background="Silver" Foreground="Blue" FontSize="11.25pt" FontFamily="Broadway" Canvas.Left="192" Canvas.Top="128" Width="64" Height="24" TabIndex="2" Content="*" Name="op3"/>
            <Button Background="Silver" Foreground="Blue" FontSize="11.25pt" FontFamily="Broadway" Canvas.Left="256" Canvas.Top="104" Width="64" Height="24" TabIndex="1" Content="-" Name="op2"/>
            <Button Background="Silver" Foreground="Blue" FontSize="11.25pt" FontFamily="Broadway" Canvas.Left="192" Canvas.Top="104" Width="64" Height="24" Content="+" Name="op1"/>
            <TextBox Background="{x:Static SystemColors.WindowBrush}" Foreground="{x:Static SystemColors.WindowTextBrush}" FontSize="12pt" FontFamily="Goudy Stout" Canvas.Left="16" Canvas.Top="16" Width="376" Height="29" Name="TextBox1"/>
        </Canvas>
    </Window>

    I’m sure that somebody with Expression Blend could make that look beautiful…

    Problems with Converter

    • This only converts a few properties.  Ideally you’d convert more.  Likely you’d loop through all the properties of the controls.
    • If the WPF control isn’t named the same thing, or if the WPF control doesn’t exist, this will fail.
    • Code written against winforms controls would need to be changed as well.
    • Ideally, you’d want to use less absolute positioning…and migrate to more Panels (like UniformGrid, Grid, WrapPanel, DockPanel, etc…)
    • Ideally, you’d want to have fewer Heights and Widths set…just size to content.
    • You’d want to take advantage of Styles and inheritable properties for repeated property sets.

    Readability comparison with InitializeComponent()

    Interesting to compare readability of the XAML created with the corresponding code that the WinForms designer had created:

     

    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents op1 As System.Windows.Forms.Button
    Friend WithEvents op2 As System.Windows.Forms.Button
    Friend WithEvents op3 As System.Windows.Forms.Button
    Friend WithEvents op4 As System.Windows.Forms.Button
    Friend WithEvents op5 As System.Windows.Forms.Button
    Friend WithEvents but0 As System.Windows.Forms.Button
    Friend WithEvents but1 As System.Windows.Forms.Button
    Friend WithEvents but2 As System.Windows.Forms.Button
    Friend WithEvents but3 As System.Windows.Forms.Button
    Friend WithEvents but4 As System.Windows.Forms.Button
    Friend WithEvents but5 As System.Windows.Forms.Button
    Friend WithEvents but6 As System.Windows.Forms.Button
    Friend WithEvents but7 As System.Windows.Forms.Button
    Friend WithEvents but8 As System.Windows.Forms.Button
    Friend WithEvents but9 As System.Windows.Forms.Button
    Friend WithEvents butdot As System.Windows.Forms.Button
    Friend WithEvents butpm As System.Windows.Forms.Button
    Friend WithEvents butce As System.Windows.Forms.Button
    Friend WithEvents butclear As System.Windows.Forms.Button
    Friend WithEvents opexp As System.Windows.Forms.Button
    Friend WithEvents grpone As System.Windows.Forms.GroupBox
    Friend WithEvents butsin As System.Windows.Forms.Button
    Friend WithEvents butcos As System.Windows.Forms.Button
    Friend WithEvents buttan As System.Windows.Forms.Button
    Friend WithEvents butroot As System.Windows.Forms.Button
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
    Friend WithEvents butrecip As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.TextBox1 = New System.Windows.Forms.TextBox
    Me.op1 = New System.Windows.Forms.Button
    Me.op2 = New System.Windows.Forms.Button
    Me.op3 = New System.Windows.Forms.Button
    Me.op4 = New System.Windows.Forms.Button
    Me.op5 = New System.Windows.Forms.Button
    Me.but0 = New System.Windows.Forms.Button
    Me.but1 = New System.Windows.Forms.Button
    Me.but2 = New System.Windows.Forms.Button
    Me.but3 = New System.Windows.Forms.Button
    Me.but4 = New System.Windows.Forms.Button
    Me.but5 = New System.Windows.Forms.Button
    Me.but6 = New System.Windows.Forms.Button
    Me.but7 = New System.Windows.Forms.Button
    Me.but8 = New System.Windows.Forms.Button
    Me.but9 = New System.Windows.Forms.Button
    Me.butdot = New System.Windows.Forms.Button
    Me.butpm = New System.Windows.Forms.Button
    Me.butce = New System.Windows.Forms.Button
    Me.butclear = New System.Windows.Forms.Button
    Me.opexp = New System.Windows.Forms.Button
    Me.grpone = New System.Windows.Forms.GroupBox
    Me.butrecip = New System.Windows.Forms.Button
    Me.butroot = New System.Windows.Forms.Button
    Me.buttan = New System.Windows.Forms.Button
    Me.butcos = New System.Windows.Forms.Button
    Me.butsin = New System.Windows.Forms.Button
    Me.MainMenu1 = New System.Windows.Forms.MainMenu
    Me.MenuItem1 = New System.Windows.Forms.MenuItem
    Me.MenuItem2 = New System.Windows.Forms.MenuItem
    Me.MenuItem3 = New System.Windows.Forms.MenuItem
    Me.grpone.SuspendLayout()
    Me.SuspendLayout()

    ‘TextBox1

    Me.TextBox1.Font = New System.Drawing.Font("Goudy Stout", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.TextBox1.Location = New System.Drawing.Point(16, 16)
    Me.TextBox1.Name = "TextBox1"
    Me.TextBox1.Size = New System.Drawing.Size(376, 29)
    Me.TextBox1.TabIndex = 0
    Me.TextBox1.Text = ""

    ‘op1

    Me.op1.BackColor = System.Drawing.Color.Silver
    Me.op1.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.op1.ForeColor = System.Drawing.Color.Blue
    Me.op1.Location = New System.Drawing.Point(192, 104)
    Me.op1.Name = "op1"
    Me.op1.Size = New System.Drawing.Size(64, 24)
    Me.op1.TabIndex = 0
    Me.op1.Text = "+"

    ‘op2

    Me.op2.BackColor = System.Drawing.Color.Silver
    Me.op2.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.op2.ForeColor = System.Drawing.Color.Blue
    Me.op2.Location = New System.Drawing.Point(256, 104)
    Me.op2.Name = "op2"
    Me.op2.Size = New System.Drawing.Size(64, 24)
    Me.op2.TabIndex = 1
    Me.op2.Text = "-"

    ‘op3

    Me.op3.BackColor = System.Drawing.Color.Silver
    Me.op3.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.op3.ForeColor = System.Drawing.Color.Blue
    Me.op3.Location = New System.Drawing.Point(192, 128)
    Me.op3.Name = "op3"
    Me.op3.Size = New System.Drawing.Size(64, 24)
    Me.op3.TabIndex = 2
    Me.op3.Text = "*"

    ‘op4

    Me.op4.BackColor = System.Drawing.Color.Silver
    Me.op4.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.op4.ForeColor = System.Drawing.SystemColors.ActiveCaption
    Me.op4.Location = New System.Drawing.Point(256, 128)
    Me.op4.Name = "op4"
    Me.op4.Size = New System.Drawing.Size(64, 24)
    Me.op4.TabIndex = 3
    Me.op4.Text = "/"

    ‘op5

    Me.op5.BackColor = System.Drawing.Color.Red
    Me.op5.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.op5.Location = New System.Drawing.Point(192, 152)
    Me.op5.Name = "op5"
    Me.op5.Size = New System.Drawing.Size(64, 24)
    Me.op5.TabIndex = 4
    Me.op5.Text = "="

    ‘but0

    Me.but0.BackColor = System.Drawing.Color.MediumOrchid
    Me.but0.ForeColor = System.Drawing.Color.Black
    Me.but0.Location = New System.Drawing.Point(16, 104)
    Me.but0.Name = "but0"
    Me.but0.Size = New System.Drawing.Size(40, 24)
    Me.but0.TabIndex = 5
    Me.but0.Text = "0"

    ‘but1

    Me.but1.BackColor = System.Drawing.Color.MediumOrchid
    Me.but1.Location = New System.Drawing.Point(56, 104)
    Me.but1.Name = "but1"
    Me.but1.Size = New System.Drawing.Size(40, 24)
    Me.but1.TabIndex = 6
    Me.but1.Text = "1"

    ‘but2

    Me.but2.BackColor = System.Drawing.Color.MediumOrchid
    Me.but2.Location = New System.Drawing.Point(96, 104)
    Me.but2.Name = "but2"
    Me.but2.Size = New System.Drawing.Size(40, 24)
    Me.but2.TabIndex = 7
    Me.but2.Text = "2"

    ‘but3

    Me.but3.BackColor = System.Drawing.Color.MediumOrchid
    Me.but3.Location = New System.Drawing.Point(136, 104)
    Me.but3.Name = "but3"
    Me.but3.Size = New System.Drawing.Size(40, 24)
    Me.but3.TabIndex = 8
    Me.but3.Text = "3"

    ‘but4

    Me.but4.BackColor = System.Drawing.Color.MediumOrchid
    Me.but4.Location = New System.Drawing.Point(16, 128)
    Me.but4.Name = "but4"
    Me.but4.Size = New System.Drawing.Size(40, 24)
    Me.but4.TabIndex = 9
    Me.but4.Text = "4"

    ‘but5

    Me.but5.BackColor = System.Drawing.Color.MediumOrchid
    Me.but5.Location = New System.Drawing.Point(56, 128)
    Me.but5.Name = "but5"
    Me.but5.Size = New System.Drawing.Size(40, 24)
    Me.but5.TabIndex = 10
    Me.but5.Text = "5"

    ‘but6

    Me.but6.BackColor = System.Drawing.Color.MediumOrchid
    Me.but6.Location = New System.Drawing.Point(96, 128)
    Me.but6.Name = "but6"
    Me.but6.Size = New System.Drawing.Size(40, 24)
    Me.but6.TabIndex = 11
    Me.but6.Text = "6"

    ‘but7

    Me.but7.BackColor = System.Drawing.Color.MediumOrchid
    Me.but7.Location = New System.Drawing.Point(136, 128)
    Me.but7.Name = "but7"
    Me.but7.Size = New System.Drawing.Size(40, 24)
    Me.but7.TabIndex = 12
    Me.but7.Text = "7"

    ‘but8

    Me.but8.BackColor = System.Drawing.Color.MediumOrchid
    Me.but8.Location = New System.Drawing.Point(16, 152)
    Me.but8.Name = "but8"
    Me.but8.Size = New System.Drawing.Size(40, 24)
    Me.but8.TabIndex = 13
    Me.but8.Text = "8"

    ‘but9

    Me.but9.BackColor = System.Drawing.Color.MediumOrchid
    Me.but9.Location = New System.Drawing.Point(56, 152)
    Me.but9.Name = "but9"
    Me.but9.Size = New System.Drawing.Size(40, 24)
    Me.but9.TabIndex = 14
    Me.but9.Text = "9"

    ‘butdot

    Me.butdot.BackColor = System.Drawing.Color.MediumOrchid
    Me.butdot.Location = New System.Drawing.Point(96, 152)
    Me.butdot.Name = "butdot"
    Me.butdot.Size = New System.Drawing.Size(40, 24)
    Me.butdot.TabIndex = 15
    Me.butdot.Text = "."

    ‘butpm

    Me.butpm.BackColor = System.Drawing.Color.MediumOrchid
    Me.butpm.Location = New System.Drawing.Point(136, 152)
    Me.butpm.Name = "butpm"
    Me.butpm.Size = New System.Drawing.Size(40, 24)
    Me.butpm.TabIndex = 16
    Me.butpm.Text = "+/-"

    ‘butce

    Me.butce.BackColor = System.Drawing.Color.Yellow
    Me.butce.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.butce.Location = New System.Drawing.Point(32, 64)
    Me.butce.Name = "butce"
    Me.butce.Size = New System.Drawing.Size(40, 24)
    Me.butce.TabIndex = 17
    Me.butce.Text = "CE"

    ‘butclear

    Me.butclear.BackColor = System.Drawing.Color.Yellow
    Me.butclear.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.butclear.Location = New System.Drawing.Point(72, 64)
    Me.butclear.Name = "butclear"
    Me.butclear.Size = New System.Drawing.Size(40, 24)
    Me.butclear.TabIndex = 18
    Me.butclear.Text = "C"

    ‘opexp

    Me.opexp.BackColor = System.Drawing.Color.Silver
    Me.opexp.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.opexp.ForeColor = System.Drawing.SystemColors.ActiveCaption
    Me.opexp.Location = New System.Drawing.Point(256, 152)
    Me.opexp.Name = "opexp"
    Me.opexp.Size = New System.Drawing.Size(64, 24)
    Me.opexp.TabIndex = 19
    Me.opexp.Text = "^"

    ‘grpone

    Me.grpone.Controls.Add(Me.butrecip)
    Me.grpone.Controls.Add(Me.butroot)
    Me.grpone.Controls.Add(Me.buttan)
    Me.grpone.Controls.Add(Me.butcos)
    Me.grpone.Controls.Add(Me.butsin)
    Me.grpone.Location = New System.Drawing.Point(16, 184)
    Me.grpone.Name = "grpone"
    Me.grpone.Size = New System.Drawing.Size(304, 80)
    Me.grpone.TabIndex = 20
    Me.grpone.TabStop = False

    ‘butrecip

    Me.butrecip.BackColor = System.Drawing.Color.Blue
    Me.butrecip.Font = New System.Drawing.Font("Broadway", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.butrecip.ForeColor = System.Drawing.Color.Yellow
    Me.butrecip.Location = New System.Drawing.Point(56, 32)
    Me.butrecip.Name = "butrecip"
    Me.butrecip.Size = New System.Drawing.Size(40, 24)
    Me.butrecip.TabIndex = 4
    Me.butrecip.Text = "1/x"

    ‘butroot

    Me.butroot.BackColor = System.Drawing.Color.Blue
    Me.butroot.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.butroot.ForeColor = System.Drawing.Color.Yellow
    Me.butroot.Location = New System.Drawing.Point(56, 8)
    Me.butroot.Name = "butroot"
    Me.butroot.Size = New System.Drawing.Size(40, 24)
    Me.butroot.TabIndex = 3
    Me.butroot.Text = "sqrt"

    ‘buttan

    Me.buttan.BackColor = System.Drawing.Color.Blue
    Me.buttan.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.buttan.ForeColor = System.Drawing.Color.Yellow
    Me.buttan.Location = New System.Drawing.Point(0, 56)
    Me.buttan.Name = "buttan"
    Me.buttan.Size = New System.Drawing.Size(56, 24)
    Me.buttan.TabIndex = 2
    Me.buttan.Text = "tan"

    ‘butcos

    Me.butcos.BackColor = System.Drawing.Color.Blue
    Me.butcos.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.butcos.ForeColor = System.Drawing.Color.Yellow
    Me.butcos.Location = New System.Drawing.Point(0, 32)
    Me.butcos.Name = "butcos"
    Me.butcos.Size = New System.Drawing.Size(56, 24)
    Me.butcos.TabIndex = 1
    Me.butcos.Text = "cos"

    ‘butsin

    Me.butsin.BackColor = System.Drawing.Color.Blue
    Me.butsin.Font = New System.Drawing.Font("Broadway", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.butsin.ForeColor = System.Drawing.Color.Yellow
    Me.butsin.Location = New System.Drawing.Point(0, 8)
    Me.butsin.Name = "butsin"
    Me.butsin.Size = New System.Drawing.Size(56, 24)
    Me.butsin.TabIndex = 0
    Me.butsin.Text = "sin"

    ‘MainMenu1

    Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})

    ‘MenuItem1

    Me.MenuItem1.Index = 0
    Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3})
    Me.MenuItem1.Text = "Calculator Type"

    ‘MenuItem2

    Me.MenuItem2.Index = 0
    Me.MenuItem2.Text = "Normal"

    ‘MenuItem3

    Me.MenuItem3.Index = 1
    Me.MenuItem3.Text = "Scientific"

    ‘Calculator

    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.BackColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(192, Byte), CType(192, Byte))
    Me.ClientSize = New System.Drawing.Size(424, 281)
    Me.Controls.Add(Me.grpone)
    Me.Controls.Add(Me.opexp)
    Me.Controls.Add(Me.butclear)
    Me.Controls.Add(Me.butce)
    Me.Controls.Add(Me.butpm)
    Me.Controls.Add(Me.butdot)
    Me.Controls.Add(Me.but9)
    Me.Controls.Add(Me.but8)
    Me.Controls.Add(Me.but7)
    Me.Controls.Add(Me.but6)
    Me.Controls.Add(Me.but5)
    Me.Controls.Add(Me.but4)
    Me.Controls.Add(Me.but3)
    Me.Controls.Add(Me.but2)
    Me.Controls.Add(Me.but1)
    Me.Controls.Add(Me.but0)
    Me.Controls.Add(Me.op5)
    Me.Controls.Add(Me.op4)
    Me.Controls.Add(Me.op3)
    Me.Controls.Add(Me.op2)
    Me.Controls.Add(Me.op1)
    Me.Controls.Add(Me.TextBox1)
    Me.Menu = Me.MainMenu1
    Me.MinimizeBox = False
    Me.Name = "Calculator"
    Me.Text = "calcualator"
    Me.grpone.ResumeLayout(False)
    Me.ResumeLayout(False)

    Update for better searchability:
    Winforms to WPF Converter
    Windows Forms to WPF Converter
    Winforms to Xaml Converter
    Windows Forms to Xaml Converter

     

  • From → WPF

    4 Comments
    1. David permalink

      Thanks!!!  Your xaml helped me out!!  I just copied it into a new WPF project and except for some class changes:  Like the textbox not having a text property but content being an object, I cab see that it is actually a richer textbox that allows object to be put in them sort of like the listbox.  I just used my same code, I was actually a little confused about event handling because some articles (http://www.devx.com/dotnet/Article/31040/0/page/2) about WPF explained that I needed to put the event delegate into the XAML click event, however after a little experimenting I saw the same tried and true handles keyword working just like net 1.1 and 2.0.  In addition, I see how you walked the controls.  In all, this is not going to be as traumatic as I though:  Learn some new classes, Pickup on the controls properties and start coding.  I still think that I need to find a textbook or class on XAML so that I can explain the correspondence between the designer and XAML.  I am not able to upload the project just yet (Remote Server Issues) but I will when I get to school on Tuesday!  Thank You:  For taking the time to help a confused teacher.  I was able to update a net 2.0 project to WPF within an hour (Just by using Intellisense and tried and true net programming).  I know someone as busy as you didn\’t have to do this!  Thank You, Thank You, Thank You!!!!!!

    2. Pooja permalink

      Is there a way to use grid here instead of canvas? The way the left and top properties of Canvas are used, is there something to convert Winform designer to a grid based layout?

      • It is beyond the scope of my little sample (to automatically detect rows/columns, etc…) Perhaps a simple move to grid with 1 row and 1 col and using margins would be helpful to you.
        It is an exercise left to you though, sorry.

    3. Pooja permalink

      Yeah i did that.. it was fairly easy.

    Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out / Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out / Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out / Change )

    Google+ photo

    You are commenting using your Google+ account. Log Out / Change )

    Connecting to %s

    %d bloggers like this: