How to make an apply button in powershell - powershell

I'm making a GUI and I need a button, that doesn't close the window on click.
Just copying from some of my other code for button creation, which works fine, but closes the window, on click. I need something that doesn't close the window, when clicked.
$apply = New-Object System.Windows.Forms.Button
$apply.Location = New-Object System.Drawing.Point(150, 120)
$apply.Size = New-Object System.Drawing.Size(50, 22)
$apply.Text = "Apply"
# $apply.DialogResult = [System.Windows.Forms.DialogResult]::
I'm making a GUI and I need a button, that doesn't close the window on click, like an apply button, does.

A button in a GUI with text Apply usually means some setting is performed without closing the window.
You just need to add an Click event handler to the button like:
$apply.Add_Click(
{
# here you do whatever the Apply is meant for
}
)
Another button called OK is usually also present which also applies whatever is was, but does close the form.

Related

Powershell (studio) - AutoComplete dropdown value clicks a button?

I'm building a GUI for a powershell script. It's a form built in Powershell studio.
I have this code which works well:
(Get-Content ".\historique.txt") | ? { $_.trim() -ne "" } | set-content ".\historique.txt"
$postes_historique = Get-Content ".\historique.txt"
$textboxPoste.AutoCompleteCustomSource.AddRange($postes_historique)
It takes what is in the "historique.txt" text file and suggests autocomplete values for the textbox like this:
On that texbox, i have a KEYDOWN event set up so when a user presses ENTER it clicks the button below the textbox:
$textboxPoste_KeyDown = [System.Windows.Forms.KeyEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
if ($_.KeyCode -eq 'Enter')
{
$buttonConnexion.PerformClick()
}
}
The strange part and my question is :
-If I click a value in the dropdown, it clicks the button. (UNWANTED BEHAVIOR)
-If I REMOVE that KeyDown enter event, it DOESN'T click the button. (Wanted behavior)
That seems very strange to me, it's as if when you click a dropdown value, the code understands it like "YOU PRESSED ENTER". What kind of weird correlation is that? I want to have both, which is being able to press down enter to click the button AND being able to choose an autocomplete value without it triggering a button click...Doesn't seem like too much to ask, no?
Thank you for your time.
Not sure if there is another solution but the work around in this case is :
REMOVE the keydown event that clicks the button when ENTER is pressed
and
REPLACE it by setting the form's AcceptButton to your button. This way when user presses ENTER, that button is triggered and the autocomplete dropdown acts as expected.

How can I disable the Close button of a waitbar in MATLAB?

I want to create a waitbar that should be processed without any interruption. How can I achieve this?
I have tried setting
h=waitbar(0,'please wait','CreateCancelBtn','setappdata(gcbf,''Cancel'',0)');
This disables the Close button on the waitbar, but it also shows me a Cancel button too. I don't want that button.
When you use the CreateCancelBtn option of waitbar, it creates a Cancel button, takes the string you supply, and then sets that string to be both the Callback of the Cancel button (i.e. the thing that happens when you press the button) and the CloseRequestFcn of the figure window (i.e. the thing that happens when you click the Close button on the window frame).
You can avoid this by just directly setting the CloseRequestFcn of the figure window yourself:
h = waitbar(0,'Please Wait...');
h.CloseRequestFcn = '';
The Close button is now disabled.
Bear in mind that the CloseRequestFcn is also what gets executed when you call close(h), so you now won't be able to close it with close(h). You can either call delete(h) instead, or you can make sure that before you call close(h) you reset the CloseRequestFcn back to the default, which is the buit-in function closereq (type edit closereq to see what this does, it basically just calls delete anyway).
Hope that helps!

openoffice calc button mouse over event cursor change

I have created an OpenOffice Calc spreadsheet and I have inserted a button into it. I can successfully call a macro with the button. I want to have a cursor hand when I mouse-over the button.
First, I switched to 'Design' mode, then I right-clicked on my button, and using the pop-up menu which appears, I selected the 'Events' tab. Then I clicked on the 'Mouse inside' dot dot dot button. In the 'Assign action' window that popped up, I clicked on the 'Macro..' button. I then got the 'Macro Selector' pop-up window, where I choose the 'OpenOffice Macros' folder in the left side pane. I expanded 'Tools'>'ModuleControls' from that 'Library' pane and then selected 'SwitchMousePointer' from the right side 'Macro name' pane, then clicked the 'Ok' 'Ok' buttons.
But now when I hover the mouse cursor over my button I get an OpenOffice Error window popping up "A Scripting Framework error occurred while running the Basic script Tools.ModuleControls.SwitchMousePointer."
I've very little OO experience but I have not found what I want here or on the OO Forum site. I would be very grateful to get some help, thanks, Nige.
The SwitchMousePointer needs attributes (oWindowPeer as Object, bDoEnable as Boolean). So you can't simply assign it to a event. And it will not do what you think.
You should better write you own Sub to change the Pointer. Also there is no need to do this on every mouse event. It should be done once on sheet activate for example.
For a introduce in OpenOffice.org BASIC Programming see https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide. And you will need a debugging tool, if you want to program your own BASIC macros. Therefore see especially: https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/UNO_Tools
Assuming on the sheet is a button named "Push Button 1" then:
Sub ChangeMousePointer
oController = ThisComponent.CurrentController
oButtonModel = oController.ActiveSheet.DrawPage.Forms(0).getByName("Push Button 1")
oButtonControl = oController.getControl(oButtonModel)
oWindowPointer = CreateUnoService("com.sun.star.awt.Pointer")
oWindowPointer.SetType(com.sun.star.awt.SystemPointer.HAND)
oButtonControl.Peer.setPointer(oWindowPointer)
End Sub
And assign this to the sheet event Activate Document.
Right click the sheet tab and select Sheet Events from the context menu. Then:
If, and only if, the button is on Sheets(0) then the sheet event Activate Document not occurs while opening the document and this sheet is in front. In this case we have to call the Sub ChangeMousePointer also on document event Open Document.
Sub Document_Open
oController = ThisComponent.CurrentController
oSheet = oController.ActiveSheet
if oSheet.Name = ThisComponent.Sheets(0).Name then
if oController.ActiveSheet.DrawPage.Forms.Count > 0 then
if oController.ActiveSheet.DrawPage.Forms(0).hasByName("Push Button 1") then
call ChangeMousePointer
end if
end if
end if
End Sub
To assign the macro to the document event Open Document select Tools - Customize from the menu and:

showForm, Command Action, and going-back

Going "back" from a form doesn't take me to the previous form but back to the main form. What am I doing wrong? Here's more detail:
I have a Command that takes you from the main form of my app to a "human setup" form. From that form if you click on a certain multibutton I go to another form (MaintenanceLevel) like this:
protected void onHumanSetup_MultiButtonMaintenanceAction(Component c, ActionEvent event) {
// Invoke the next level of form
showForm("MaintenanceLevel", null);
}
In the "MaintenanceLevel" form I have an Action set to Back, but when I click the menu bar to go back I end up back on the main-form and not the "human setup" where I want to be.
Edit:
Things have got a little worse!! Now when I go back in to view the Commands for my MaintenanceLevel form it's empty: it's forgetting the command I've entered :(
Here is my command setting for MaintenanceLevel form:
You don't need to define a back command. Remove it. Codename One automatically creates back commands for you.
The usage for this option is when you create a Button and place it within the form, then you want it to behave as a back button effectively (e.g. cancel button).

How to create a "New xxx" popup?

I have a Grid object and added a [ (+) New Client ] button which I'd like to open a popup form to create the new client with a couple fields.
I've looked at the code examples in the website but haven't found how to do it (sorry if I've missed something).
This is the current page code:
function page_clients_listing($p){
$g = $p->add('Grid');
$g->addColumn('text','first_name');
$g->addColumn('text','last_name');
$g->addColumn('inline','telephone');
$g->addColumn('expander','comments');
$g->setSource('client');
$g->addButton('With Icon')->set('Add New Client')->setIcon('Plus');
}
Thanks in advance!
You can either create a popup or a dialog. Dialog is based on jQuery UI dialog implementation. Popups are likely to be blocked and are harder to control.
This is actually working for any object (you can apply to view, button, image, icon, etc), but I'll use button).
$b=$g->addButton('Add New Client')->setIcon('Plus');
$b->js('click')->univ()->frameURL($title,$url);
// OR
$b->js('click')->univ()->dialogURL($title,$url);
$url would most likely be returned by api->getDestinationURL(). The other page would be loaded and scripts on that page will be evaluated. Let's say you are on other page and now need to close the window.
$result = $this->addButton('Close')->js('click')->univ()->closeDialog();
closeDialog() returns a jQuery chain object pointing to a view which originally opened the frame. As a result if you do $result->hide(); then after dialog is closed, the original button ('add new client') will also be hidden.
Here is example to show some additional things you can do with frames, reloading and custom event handlers:
http://agiletoolkit.org/example/refresh1