Solved

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

Posted on 2014-10-29
12
159 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 26

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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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 26

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now