Make a character appear only once on VB6 - vb6

I'm creating a simple calculator on VB6.
Here's my code I'm working on:
textScreen.Text = textScreen.Text & "+"
Here's the result when I press some number buttons, followed by clicking on
the plus sign button several times:
75+++++++
I would like the plus sign to appear only once, even if I click on
its button many times:
92+
...and when I click on some number buttons again, followed by clicking
on the plus sign button, I would like the plus sign to show up again:
58+4+
This is somehow similar to the default Calculator on Windows 7.

Well, there are different approaches for this. But in general, I wouldn't just concatenate some string. This way you'll have to parse the string later on, instead of just solving the requested term. Instead try to create some stack with your operations/numbers on it; just look on the web for calculator examples.
Anyway, for this, you'll have to somehow store the last operation (e.g. did I input a digit or an operator?)
If you'd like to limit the calculator to simple operations without brackets etc. you can use a boolean value for this:
Dim lastOp As Boolean
Then, before adding the + (or any other operator):
If Not lastOp Then
textScreen.Text = textScreen.Text & "+"
lastOp = true
End If
When adding any digit (e.g.):
lastOp = false
textScreen.Text = textScreen.Text & "0"
(Don't count on 100% error free code, I think I haven't touched VB6 for like 8 years.)

You mighty just check if the last character in the text was "+" :
If Mid(textScreen.Text, Len(textScreen.Text), 1) <> "+" Then
textScreen.Text = textScreen.Text & "+"
End If

Related

How to use AppleScript with Calculator.app to Automatically select the number of Decimals

Every time I enter a number in Calculator.app I need to go the top menu and select:
View >> Decimal Places >> [0 , 15] And choose the number of decimals.
Is there a way to make an AppleScript that will automatically do so, based on the number I entered?
My input ways are:
by pasting the number
by typing the number
For the pasting part, if you PASTE a number ending in 0, Calculator.app won't show the 0, so it will show one decimal less than the reality. I don't know if this could be also overcome.
Example, if you paste: 12.30 it will show 12.3
if you type: 12.30 it will show 12.30
Building on the addendum to my answer to your other question How can I copy result from Calculator.app to the clipboard using AppleScript, here is a way the trap ⌘V in Calculator and set Calculator > View > Decimal Places to the number of decimal places of the number on the clipboard.
What the example AppleScript code does:
When pressing ⌘V in Calculator the contents of the clipboard is set to a variable and if it contains a decimal point it sets the Calculator > View > Decimal Places menu to the number of decimal places in the number, then keystrokes the number into Calculator. Note that keystroking the number would probably not require you to set the number of decimal places, however I'll leave that up to you to decide.
Example AppleScript code:
set theNumberToType to the clipboard as text
if theNumberToType contains "." then
set theNumberOfDecimalPlaces to count characters ((offset of "." in theNumberToType) + 1) thru -1 of theNumberToType
if theNumberOfDecimalPlaces is greater than 15 then set theNumberOfDecimalPlaces to 15
else
set theNumberOfDecimalPlaces to -1
end if
tell application "Calculator" to activate
tell application "System Events"
if theNumberOfDecimalPlaces is greater than -1 then
click (first menu item of menu 1 of menu item "Decimal Places" of menu 1 of menu bar item "View" of menu bar 1 of process "Calculator" whose name is theNumberOfDecimalPlaces)
end if
delay 0.1
keystroke theNumberToType
end tell
The example AppleScript code above is saved as CalculatorSetDecimalPlaces.applescript in: ~/.hammerspoon/Scripts/, like in my other answer to your other question.
Example Lua code:
-- Create a hotkey used to trap the command v shortcut and disable it.
-- It will then be enabled/disabled as Calculator is focused/unfocused.
-- When enabled and command v is pressed it runs the AppleScript script.
local applicationCalculatorCommandVHotkey = hs.hotkey.bind({"cmd"}, "V", function()
local asFile = "/.hammerspoon/Scripts/CalculatorSetDecimalPlaces.applescript"
local ok, status = hs.osascript.applescriptFromFile(os.getenv("HOME") .. asFile)
if not ok then
msg = "An error occurred running the CalculatorResultToClipboard script."
hs.notify.new({title="Hammerspoon", informativeText=msg}):send()
end
end)
applicationCalculatorCommandVHotkey:disable()
-- Add the following two line respectively before or after the lines
-- applicationCalculatorEnterHotkey:enable()
-- applicationCalculatorEnterHotkey:disable()
-- in either of the two methods presented in my other answer.
applicationCalculatorCommandVHotkey:enable()
applicationCalculatorCommandVHotkey:disable()
Notes:
With the example Lua code, as coded, the behavior of the ⌘V keyboard shortcut is only trapped and modified to trigger the example AppleScript code while Calculator has focus. The ⌘V keyboard shortcut should work normally in all other applications.
The example Lua code above is added to the ~/.hammerspoon/init.lua file from my other answer to your other question.
The example Lua code and API's of Hammerspoon and AppleScript code, shown above, were tested respectively with Hammerspoon and Script Editor under macOS Mojave and macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Update using only Lua without AppleScript
The following example Lua code eliminates the use of AppleScript from both this answer and the code in the other answer to your other related question.
Overwriting the existing code, copy and paste this into your ~/.hammerspoon/init.lua file, save it and run Reload Config from the Hammerspoon menu on the menu bar.
I have performed all the various actions and calculations discussed in the comments and had no issues occur with this new code. Hopefully this will eliminate any issues you were having.
This should also run faster now that it does have to process AppleScript code as well.
Note that as coded it is not automatically reloading the configuration file as that just should not be necessary.
-- Create a hotkey used to trap the enter key and disable it.
-- It will then be enabled/disabled as Calculator is focused/unfocused
-- When enabled and the enter key is pressed it presses = then command C.
local applicationCalculatorEnterHotkey = hs.hotkey.bind({}, "return", function()
-- Press the '=' key to finish the calculation.
hs.eventtap.keyStroke({}, "=")
-- Copy the result to the clipboard.
hs.eventtap.keyStroke({"cmd"}, "C")
end)
applicationCalculatorEnterHotkey:disable()
-- Create a hotkey used to trap the command v shortcut and disable it.
-- It will then be enabled/disabled as Calculator is focused/unfocused.
-- When enabled and command v is pressed it runs the Lua code within.
local applicationCalculatorCommandVHotkey = hs.hotkey.bind({"cmd"}, "V", function()
-- Get the number copied to the clipboard.
local theNumberToType = hs.pasteboard.readString()
-- See if there is a decimal in the number
-- and if so how many decimal places it has.
local l = string.len(theNumberToType)
local s, e = string.find(theNumberToType, "%.")
if s == nil then
theNumberOfDecimalPlaces = -1
elseif l - s > 15 then
theNumberOfDecimalPlaces = 15
else
theNumberOfDecimalPlaces = l - s
end
-- Set the number of decimal places to show.
if theNumberOfDecimalPlaces > -1 then
local calculator = hs.appfinder.appFromName("Calculator")
local vdpn = {"View", "Decimal Places", theNumberOfDecimalPlaces}
calculator:selectMenuItem(vdpn)
end
-- Type the number into Calculator.
hs.eventtap.keyStrokes(theNumberToType)
end)
applicationCalculatorCommandVHotkey:disable()
-- One of two methods of watching Calculator.
--
-- The other is below this one and commented out.
-- Initialize a Calculator window filter.
local CalculatorWindowFilter = hs.window.filter.new("Calculator")
-- Subscribe to when the Calculator window is focused/unfocused.
CalculatorWindowFilter:subscribe(hs.window.filter.windowFocused, function()
-- Enable hotkeys when Calculator is focused.
applicationCalculatorCommandVHotkey:enable()
applicationCalculatorEnterHotkey:enable()
end)
CalculatorWindowFilter:subscribe(hs.window.filter.windowUnfocused, function()
-- Disable hotkeys when Calculator is unfocused.
applicationCalculatorCommandVHotkey:disable()
applicationCalculatorEnterHotkey:disable()
end)
-- Alternate method to wait for Calculator and enable/disable the hotkey.
--
-- Uncomment below method and comment the above method to test between them.
-- The opening '--[[' and closing '--]]' and removed from below added to above.
--[[
function applicationCalculatorWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
if (appName == "Calculator") then
-- Enable hotkeys when Calculator is activated.
applicationCalculatorCommandVHotkey:enable()
applicationCalculatorEnterHotkey:enable()
end
end
if (eventType == hs.application.watcher.deactivated) then
if (appName == "Calculator") then
-- Disable hotkeys when Calculator is deactivated.
applicationCalculatorCommandVHotkey:disable()
applicationCalculatorEnterHotkey:disable()
end
end
end
appCalculatorWatcher = hs.application.watcher.new(applicationCalculatorWatcher)
appCalculatorWatcher:start()
-- appCalculatorwWatcher:stop()
--]]

Multiline hints in Firemonkey

I am trying to create multi line hint in my application made in delphi 10 seattle (FMX). seems like line break is not working while setting the hints.
Button1.Hint := 'Line 1' + #13#10 + 'Line2';
Any idea about how this can be done. this is working fine in VCL though.
please check if your button has ShowHint property checked.
Button1.Hint := 'line 1' + sLineBreak + 'line 2';
I can offer a hint that I just worked through the same type of problem in C++ Builder Rio. I don't have Delphi, just C++ Builder, but the two products are so inter-related, I use hints (or code) from Delphi all the time to solve my problems.
In C/C++, you can generally use "\r" or its equivalent "\n\l" to display a carriage return (which I was trying to display in a TMemo). The TMemo looked like it was just stripping out the codes (except it thought the "\l", for line-feed, was an invalid escape code, so it would display just the "l") and was displaying everything on one line. I did notice the shortcut for tab ("\t") was working.
Again, in C/C++, there are other options for how to create characters. The equivalent of what you are doing, "char(13)+char(10)" just displays the characters "23" with everything on the same line (as you are describing). That is how one add characters when you are using decimal (base 10). If I wanted to use hexadecimal, I would write "\0xd\0xa" (which just gets stripped out of the text and displayed on one line, like the stuff in the second paragraph above).
The solution that I found that worked in C++ Builder was to use an octal notation for my character encoding ("\015\012"). Personally, in about 50 years of programming, I have never previously seen a situation where hexadecimal failed, but octal worked, but I was desperate enough to try it.
For all this testing and debugging, I created a new project, added a TMemo and a button (and set ShowHint=true for the button) to the form and put the following in for the code for the button:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString CR = "\015\012";
Memo1->Text = "a" + CR + "b";
Button1->Hint = Memo1->Text + " (hint)";
}
So, my solution to your problem is figure out how you can put octal codes in for characters and display the corresponding text in Delphi, then use that encoding for the octal characters "015" and "012".

distance between all typing of character in vb6.0

draw a form1 in vb6 and draw textbox1 in form1 and press f5 run this program. when i type " abc1234 " in textbox1 typing start this " abc1234 " but i show type " a b c 1 2 3 4 " in textbox1.
Its mean only some distance between all typing character.
for example
input of html
using css
letter-spacing: 1px;
when i type in input some distance between all typing
If I understand you correctly, you want margin to be added between the characters in the Textbox.
This is not possible in standard control, unless you manually append and manage spaces to the text within the Textbox. Which will be a pain to manage (when using backspace/copy-paste/different culture/languages/etc).
Its better to look for a third-party control, or better alter your requirements.
In worst case - Highly not-recommended - develop your own VB6 ActiveX Control (some people call it UserControl, because of the way designer shows it).
Mind you, TextBox/Edit control is way more complex than custom designing other controls, since it contains text, which is extremely tricky to handle due to multiple locals/languages/windows themes/and what not.
Well if you must add spacing to a Textbox this is how you would do it manually:
Dim oldString as String
Dim newString as String
oldString = "babel"
for i = 1 to len(oldString)
newString = newString & Space(2) & Mid(oldString,i,1)
Next
msgbox newString
Like subdeveloper said this is not recommended but in case you must add spacing to your control this is how you would do it.

Single Space Indent

Sometimes when i'm working in visual studio, I end up with a block of code that I need to indent a single space in order to make it align completely.
A normal sized indent would be done by selecting the text and pressing the tab key, however if I just want to move it along one space, selecting the text and pressing the space bar overwrites the code.
I know I could do this this by changing the tab spacing option so the indent size is 1, indenting the text and then changing it back but this seems a bit longwinded...
I've had no luck searching, so I've written a macro to do the above, but I thought i'd ask on here before i resigned to using it just in case the function/shortcut already existed...
Edit: Macro moved to answers
Here is said macro for anyone interested:
Sub SingleSpaceIndent()
Dim textEditor As Properties
textEditor = DTE.Properties("TextEditor", "AllLanguages")
textEditor.Item("IndentSize").Value = 1
DTE.ActiveDocument.Selection.Indent()
textEditor.Item("IndentSize").Value = 4
End Sub

Does anyone know a visual studio keyboard short cut to swap around two sides of a statement?

Just wondering if anyone knows the keyboard shortcut to swap around two sides of a statement. For example:
I want to swap
firstNameTextbox.Text = myData.FirstName;
to
myData.FirstName = firstNameTextbox.Text;
Does anyone know the shortcut, if there is one? Obviously I would type them out, but there is a lot of statements I need to swap, and I think a shortcut like that would be useful!
Feel free to throw in any shortcuts you think are cool!
My contribution would be CTRL + E, D - this will format your code to Visual Studio standards! Pretty well known I'm guessing but I use it all the time! :)
UPDATE
Just to let everyone know, using a bit of snooping of the article that was posted, I managed to construct a regular expression, so here it is:
Find:
{.+\.Text = myData\..+};
And replace with:
\2 = \1;
Hopefully people can apply this to their own expressions they want to swap!
I think the following thread is a good place to begin with
Invert assignment direction in Visual Studio
Here's how I would go about doing that without a specific keyboard shortcut:
First, select the text you want to modify and replace
" = " with " = "
(the key here is to add a lot of spaces).
If you hold down Alt and use the mouse, you can select a "block" of code. Use this to select only the text on the right side of the equation (it's helpful to add extra white space here in your selection)
Use the same Alt + Left-Click combination to select the beginning of the left side (just select a blank area). You should be able to paste text into here.
If you added extra white space to the text you just added, just should be able to easily insert an = using the Alt + Click technique. Use the same trick to remove the equal sign that's dangling on the right side of your code block.
While this might not do exactly what you're looking for, I've found these tricks quite useful.
If you're using ReSharper, you can do this by pressing CtrlAltShift + ← or →
The feature is in Resharper. Select the code segment and click the content wizard, which is a pencil icon in the left corner reading View Actions List, then choose Reverse Assignment.
It is done.
swap-word is a VSCode extension which sounds like it would do what you want.
Quickly swap places two words or selections...
But I'm not sure if it is compatible with VS.
Since I was not happy with the answers where I need to enter complicated strings into the Visual Studio search/replace dialog, I wrote myself a little AutoHotkey script, that performs the swaps with only the need to press a keyboard shortcut. And this, no matter if you are in VS or in another IDE.
This hotkey (start it once simply from a textfile as script or compiled to exe) runs whenever Win+Ctrl-S is pressed
#^s Up::
clipboard := "" ; Empty the clipboard
Sendinput {Ctrl down}c{ctrl up}
Clipwait
Loop, Parse, clipboard, `n, `r ; iterates over seperates lines
{
array := StrSplit(RegExReplace(A_LoopField,";",""),"=") ; remove semicolon and split by '='
SendInput, % Trim(array[2]) . " = " . Trim(array[1]) . ";{Enter}"
}
return
Many more details are possible, e.g. also supporting code where lines end with a comma
...and I can put many more hotkeys and hotstrings into the same script, e.g. for my most mistyped words:
::esle::else ; this 1 line rewrites all my 'else' typos
I recommend using the find-replace option in Visual Studio. IMHO the REGEX string is not that complicated, and moreover, you don't need to understand the expression in order to use it.
The following regex string works for most programming languages:
([\w\.]+)\s*=\s*([\w\.]+)
For Visual Studio's you want to use $ argument in the replace text.
$2 = $1
Make sure to enable regex.
To do this in one shot, you can select a section of the document, and click the replace-all option.
Before:
comboBoxAddOriginalSrcTextToComment.SelectedIndex = Settings.Default.comboBoxAddOriginalSrcTextToComment;
comboBoxDefaultLanguageSet.SelectedIndex = Settings.Default.comboBoxDefaultLanguageSet;
comboBoxItemsPerTransaltionRequest.SelectedIndex = Settings.Default.comboBoxItemsPerTransaltionRequest;
comboBoxLogFileVerbosityLevel.SelectedIndex = Settings.Default.comboBoxLogFileVerbosityLevel;
comboBoxScreenVerbosityLevel.SelectedIndex = Settings.Default.comboBoxScreenVerbosityLevel;
After:
Settings.Default.comboBoxAddOriginalSrcTextToComment = comboBoxAddOriginalSrcTextToComment.SelectedIndex;
Settings.Default.comboBoxDefaultLanguageSet = comboBoxDefaultLanguageSet.SelectedIndex;
Settings.Default.comboBoxItemsPerTransaltionRequest = comboBoxItemsPerTransaltionRequest.SelectedIndex;
Settings.Default.comboBoxLogFileVerbosityLevel = comboBoxLogFileVerbosityLevel.SelectedIndex;
Settings.Default.comboBoxScreenVerbosityLevel = comboBoxScreenVerbosityLevel.SelectedIndex;
IMHO: It's better for a developer to learn to use the IDE (Integrated Development Environment), then to create new tools to do the same thing the IDE can do.

Resources