Results 1 to 10 of 935

Thread: Windows 10 and Office Excel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    10,457
    Rep Power
    10
    Code in Add_Click to function alternative
    In Chris’s script, the main meat of any Debloat tweak script, or install script, or any other thing done after clicking a button, appears in the code section within the .Add_Click , example:

    $winterminal.Add_Click({
    Write-Host "Installing New Windows Terminal"
    $ResultText.text = "`r`n" +"`r`n" + "Installing New Windows Terminal... Please Wait"
    winget install -e Microsoft.WindowsTerminal | Out-Host
    if($?) { Write-Host "Installed New Windows Terminal" }
    $ResultText.text = "`r`n" + "Finished Installing New Windows Terminal" + "`r`n" + "`r`n" + "Ready for Next Task"

    })


    That makes sense as the script within the .Add_Click is what is done when the button, ( in this case the button object variable $winterminal), is clicked by the user.

    But there can be advantages to call an external script from within the .Add_Click. For example, this would give better flexibility in use of the script: The script could then be called from other buttons or other script.
    This basic requirement, often done for convenience, is done by what is typically referred to in coding as a function or sub routine, which in simple terms is just a sub set of coding called from other coding

    Simple examples are perhaps the easiest way to explain this:
    This simple .Add_Click results in a text coming up when you click the button

    $okButton.Add_Click({
    Write-Host "Hello Shit World. Fuck off"
    })


    Using the function idea, that would be done with this

    function SayHelloToTheShitWorld {
    Write-Host "Hello Shit World. Fuck off"
    }

    $okButton.Add_Click({
    SayHelloToTheShitWorld
    })


    What’s going on.
    In simple terms:
    For the non function script, the script associated with the click, wrote a text. ( It was written into the PowerShell window belonging to the opened instance of the PowerShell/ ISE window in which the script is ( - in computer jargon: the script is hosted there in that window or PowerShell shell) )
    That is almost the same for the function case. The slight difference for the function way of doing it is that the script associated with the click sets off the script in the function SayHelloToTheShitWorld which is the same script as previously.

    Important positioning of the function in relation to other things:
    In PowerShell scripts things are not done as immediately as in some other languages. Things are generally done fir the first time, in sequence, as the script is run the first time. Not much is done before. Things tend to be stored the first time they are done or used or passed. They are then held in the memory of the open PowerShell until you close it.
    This has some consequences.
    _ ( Variables may have their type set the first time they are used, which can cause some unexpected results when developing or experimenting or debugging script during re runs. For this reason its often wise to include a command line towards the start of a script like this: Remove-Variable * -ErrorAction SilentlyContinue )
    _ The workings associated with a function are stored as the function is passed. So for our example to work, the function must go above the first time it is used, - so it would not work if placed below the .Add_Click
    _ ( Everything associated with the form is finally available after the form is shown. Hence often the last script line is some sort of .Show code line )
    _._____

    That concludes all we need to know. But in passing it my help get things more into a greater perspective appreciation if we explain the general function idea a little further. This may help avoid confusion later.
    On most computer work and languages, the most simple function version as we showed above would rarely be referred to as a function, rather a simple routine or sub routine. The word function would usually be used for a slightly more advanced form of the basic simple coding whereby something, typically a single variable or single object, would be returned, the specific details of which are a function of, ( or as a result of ) , other things given to the function
    This is a PowerShell script example that extends the previous example slightly:
    A single value is given, 0 or 1, ( in this particular case it could also alternatively be given as True or False ). Depending on what is given, a text may or may not tell you the time at which you used the function. ( As I present it here, I am passing a 0 , so the final texts that come out are
    ___Hello Shit World. Fuck off
    ___I used the function on ....I am not telling you

    Code:
    function SayHelloToTheShitWorld {param([Boolean]$WantDate)
    Write-Host "Hello Shit World. Fuck off"
    if ($WantDate -eq 1) {
    [string]$Hr=(Get-Date).Hour ; [string]$Mn = (Get-Date).Minute
    [string]$Rtn =$Hr + "Hour, " + $Mn + "minutes"
    return ($Rtn)}
    else {return ("....I am not telling you")}
    }
     
    $okButton.Add_Click({ #$vscode.location = New-Object System.Drawing.Point(4,797)
    $DateDone = SayHelloToTheShitWorld -WantDate 0 
    Write-Host "I used the function on $DateDone" 
    })
    A function of this sort may or may not do something itself other than returning something as a function of what it is given.

    The syntax may be a bit off topic for now, but just before we leave this introduction, lets do another function in PowerShell script that is more typical of a simple function example done generally in any introduction to function coding. This function returns a number, the value of which depends on the numbers given. Specifically it gives the sum of those numbers given.
    Code:
    function AddNumbers {param($Num1, $Num2, $Num3, $Num4)
    return ($Num1 + $Num2 + $Num3 + $Num4)
    }
     
    $Reslt = addnumbers -Num2 2 -Num4 3
    Write-Host "Result of summing numbers is $Reslt" # This is always done
     
    $Some.Add_Click({ #$vscode.location = New-Object System.Drawing.Point(4,797)
    $Reslt = addnumbers -Num2 2 -Num4 3
    Write-Host "Result of summing numbers is $Reslt" # This is done if you hit the Some button 
    })


    Share ‘functions.ps1’ https://app.box.com/s/7rmx2zsasymftrikjojjs3p3g1g8lng6



    An application of the function idea to my GUI version
    .
    Last edited by DocAElstein; 04-07-2022 at 09:44 PM.

Similar Threads

  1. Tests and Notes on Range Referrencing
    By DocAElstein in forum Test Area
    Replies: 70
    Last Post: 02-20-2024, 01:54 AM
  2. Tests and Notes for EMail Threads
    By DocAElstein in forum Test Area
    Replies: 29
    Last Post: 11-15-2022, 04:39 PM
  3. Notes tests. Excel VBA Folder File Search
    By DocAElstein in forum Test Area
    Replies: 39
    Last Post: 03-20-2018, 04:09 PM
  4. Replies: 2
    Last Post: 12-04-2012, 02:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •