Solved

I would like to use a script to provide calculations from kg to lbs and viceversa

Posted on 2014-10-29
12
161 Views
Last Modified: 2014-10-29
I would like a script that a user can simply double click.
It should prompt them if they want to convert from lbs to kg or kg to lbs.
Depending on what is selected they enter an amount, and then the calculated amount will then be provided for them.
0
Comment
Question by:100questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 250 total points
ID: 40410859
Hi Try the following

A user friendly form :-)

#region Script Settings
#<ScriptSettings xmlns="http://tempuri.org/ScriptSettings.xsd">
#  <ScriptPackager>
#    <process>powershell.exe</process>
#    <arguments />
#    <extractdir>%TEMP%</extractdir>
#    <files />
#    <usedefaulticon>true</usedefaulticon>
#    <showinsystray>false</showinsystray>
#    <altcreds>false</altcreds>
#    <efs>true</efs>
#    <ntfs>true</ntfs>
#    <local>false</local>
#    <abortonfail>true</abortonfail>
#    <product />
#    <version>1.0.0.1</version>
#    <versionstring />
#    <comments />
#    <company />
#    <includeinterpreter>false</includeinterpreter>
#    <forcecomregistration>false</forcecomregistration>
#    <consolemode>false</consolemode>
#    <EnableChangelog>false</EnableChangelog>
#    <AutoBackup>false</AutoBackup>
#    <snapinforce>false</snapinforce>
#    <snapinshowprogress>false</snapinshowprogress>
#    <snapinautoadd>2</snapinautoadd>
#    <snapinpermanentpath />
#    <cpumode>1</cpumode>
#    <hidepsconsole>false</hidepsconsole>
#  </ScriptPackager>
#</ScriptSettings>
#endregion

#region ScriptForm Designer

#region Constructor

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

#endregion

#region Post-Constructor Custom Code

#endregion

#region Form Creation
#Warning: It is recommended that changes inside this region be handled using the ScriptForm Designer.
#When working with the ScriptForm designer this region and any changes within may be overwritten.
#~~< Form1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = New-Object System.Drawing.Size(287, 172)
$Form1.Text = "Weight Convert V1.0"
$Form1.BackColor = [System.Drawing.SystemColors]::InactiveCaption
#~~< RadioButtonLb >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$RadioButtonLb = New-Object System.Windows.Forms.RadioButton
$RadioButtonLb.Location = New-Object System.Drawing.Point(177, 63)
$RadioButtonLb.Size = New-Object System.Drawing.Size(104, 24)
$RadioButtonLb.TabIndex = 7
$RadioButtonLb.TabStop = $true
$RadioButtonLb.Text = "Pounds to KG"
$RadioButtonLb.UseVisualStyleBackColor = $true
$RadioButtonLb.add_Click({RadioButtonLbClick($RadioButtonLb)})
#~~< RadioButtonkg >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$RadioButtonkg = New-Object System.Windows.Forms.RadioButton
$RadioButtonkg.Location = New-Object System.Drawing.Point(177, 41)
$RadioButtonkg.Size = New-Object System.Drawing.Size(95, 24)
$RadioButtonkg.TabIndex = 6
$RadioButtonkg.TabStop = $true
$RadioButtonkg.Text = "kg to Pounds"
$RadioButtonkg.UseVisualStyleBackColor = $true
$RadioButtonkg.add_Click({RadioButtonkgClick($RadioButtonkg)})
#~~< TextBox2 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$TextBox2 = New-Object System.Windows.Forms.TextBox
$TextBox2.Location = New-Object System.Drawing.Point(12, 109)
$TextBox2.ReadOnly = $true
$TextBox2.Size = New-Object System.Drawing.Size(131, 20)
$TextBox2.TabIndex = 4
$TextBox2.Text = ""
#~~< TextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$TextBox1 = New-Object System.Windows.Forms.TextBox
$TextBox1.Location = New-Object System.Drawing.Point(7, 67)
$TextBox1.Size = New-Object System.Drawing.Size(136, 20)
$TextBox1.TabIndex = 3
$TextBox1.Text = ""
$TextBox1.add_TextChanged({TextBox1TextChanged($TextBox1)})
#~~< Label1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Point(33, 8)
$Label1.Size = New-Object System.Drawing.Size(150, 30)
$Label1.TabIndex = 2
$Label1.Text = "Powershell Converter"
#~~< btn_convert >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$btn_convert = New-Object System.Windows.Forms.Button
$btn_convert.Location = New-Object System.Drawing.Point(177, 93)
$btn_convert.Size = New-Object System.Drawing.Size(104, 36)
$btn_convert.TabIndex = 1
$btn_convert.Text = "Convert"
$btn_convert.UseVisualStyleBackColor = $true
$btn_convert.add_Click({Btn_convertClick($btn_convert)})
#~~< btn_quit >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$btn_quit = New-Object System.Windows.Forms.Button
$btn_quit.Location = New-Object System.Drawing.Point(177, 145)
$btn_quit.Size = New-Object System.Drawing.Size(75, 23)
$btn_quit.TabIndex = 0
$btn_quit.Text = "Quit"
$btn_quit.UseVisualStyleBackColor = $true
$btn_quit.add_Click({Btn_quitClick($btn_quit)})
$Form1.Controls.Add($RadioButtonLb)
$Form1.Controls.Add($RadioButtonkg)
$Form1.Controls.Add($TextBox2)
$Form1.Controls.Add($TextBox1)
$Form1.Controls.Add($Label1)
$Form1.Controls.Add($btn_convert)
$Form1.Controls.Add($btn_quit)

#endregion

#region Custom Code

#endregion

#region Event Loop

function Main{
	[System.Windows.Forms.Application]::EnableVisualStyles()
	[System.Windows.Forms.Application]::Run($Form1)
}

#endregion

#endregion

#region Event Handlers

function RadioButtonLbClick( $object ){
	$RadioButtonkg.checked=$false
	$RadioButtonLb.checked=$true
}

function RadioButtonkgClick( $object ){
	$RadioButtonkg.checked = $true
	$RadioButtonLb.checked = $false
}

function Btn_convertClick($object)
{
	if ($RadioButtonkg.checked )
	{ $TextBox2.Text = [single]($TextBox1.Text) * 2.2046226218 }
		
	else
	{ $TextBox2.Text = [single]($TextBox1.Text) / 2.2046226218  }
	

}

function Btn_quitClick( $object ){
$Form1.close()
}

function TextBox1TextChanged( $object ){

}

Main # This call must remain below all other event functions

#endregion

Open in new window

0
 
LVL 27

Expert Comment

by:MacroShadow
ID: 40410882
Here is a vbs:
Sub Demo()
    Dim dblAmount
    Dim intMeasurment
    Dim dblResult
    intMeasurment = InputBox("Which measurment would you like to convert from?" & vbCrLf & vbCrLf & "For ''lbs to kg'' type 1" & vbCrLf & "For ''kg to lbs'' type 2", "Measurment Conversion")
    dblAmount = InputBox("Please enter the weight to convert.", "Measurment Conversion")
    dblResult = (IIf(intMeasurment = 1, dblAmount / 0.4535922921969, dblAmount * 2.204623))
    MsgBox dblResult
End Sub

Open in new window

0
 

Author Comment

by:100questions
ID: 40410936
@joeklimis - Is your script a batch script, Powershell or VBScript?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 10

Expert Comment

by:JoeKlimis
ID: 40410945
Hi
this is a Powershell script , wich uses .net forms

just save it into a file and run it in the normal way  

Joe
0
 

Author Comment

by:100questions
ID: 40410951
@MacroShadow - when trying to run the script nothing happens.
0
 
LVL 10

Expert Comment

by:JoeKlimis
ID: 40410955
try adding   Demo()  as the last line to invoke the subroutine, you may not need the ()
0
 

Author Comment

by:100questions
ID: 40411000
Joe are you referring to the script from MacroShadow?
0
 

Author Comment

by:100questions
ID: 40411014
@MacroShadow - I added the Demo(), however after I enter the weight to convert, it does not calculate anything..
0
 

Author Comment

by:100questions
ID: 40411021
@MacroShadow - if I try to run the script by doubleclicking the vbs file, I get an error:
Type mismatch: 'IIf'
Code:  800A000D
0
 
LVL 10

Expert Comment

by:JoeKlimis
ID: 40411027
@100questions  - Yes I was , I am guessing this needed to be VB, its a  sham as the Powershell rocked  :-)

did you try the powershell ?
0
 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 250 total points
ID: 40411121
Sorry, this works.
Dim dblAmount
Dim intMeasurment
Dim dblResult
intMeasurment = InputBox("Which measurment would you like to convert from?" & vbCrLf & vbCrLf & "For ''lbs to kg'' type 1" & vbCrLf & "For ''kg to lbs'' type 2", "Measurment Conversion")
dblAmount = InputBox("Please enter the weight to convert.", "Measurment Conversion")
If intMeasurment = 1 Then
    dblResult = dblAmount / 0.4535922921969
Else
    dblResult = dblAmount * 2.204623
End If
MsgBox dblResult

Open in new window

0
 

Author Closing Comment

by:100questions
ID: 40411235
Thanks everyone, both work well.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Synchronize a new Active Directory domain with an existing Office 365 tenant
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question