Changing Char of a textbox to it's next char - c++

In Qt, I want to make something which will show the next char of the char input.
For example, I entered 'a' into a QTextEdit, it automatically turns to 'b' in another QTextEdit, and when I again enter 'b', it turns to 'c'. Which algorithm is perfect for this?

As mentioned in a comment, you want to do the following things (which are not code):
Detect a change in the first input box
Decide how to modify the second text box given the value of the first text box
Update the second text box
Step 2 might be (from your example) to ensure the text has more than one character and, assuming its a std::string, get text[text.length() -1] and Step 3 might be to get the text of the second text box, append that character and assign it.
This would work for a modifiable or non-modifiable second text box since it does not re-modify each character in the string -- just the added one.
But, you need to clearly define what you want to happen.

If you can modify the characters directly, you can set each character like tChar = iChar + 1. It'd get a little wonky if you put in something that isn't a letter (spaces or enter key). In that case you'd wrap it with if (iChar >= 'a' && iChar <= 'Z') { which uses the 'n' syntax to alias the actual number codes. Another method would be to subclass the Qtextedit control and overload the void Qtextedit::keyReleaseEvent(QKeyEvent* event) virtual function to intercept the key strokes and check that way. The comparison is similar, but you'd use Qt::Key_A instead of 'a' to check that it's a letter key, then modify the resulting char appropriately.
See also: http://doc.qt.nokia.com/4.7-snapshot/qtextedit.html

Related

Caret position in EditBox after change in text length

I have an EditBox in a MFC-dialog. The user is supposed to enter a number. I'm trying to automatically add separators to the number while the user is inputting it:
When the number is more than 3 digits long, a separator is added between the hundreds and the thousands digit; a second one between the hundredthousands and the millions when it gets longer than 6 digits and so forth (so that 1234567 becomes 1,234,567 for example).
This is done in a function executed by ON_EN_CHANGE and basically works fine already. But the problem is that the caret position is set to the beginning of the EditBox once my function changes the length of the string in it, preventing continous typing.
I tried simulating the press of the end-key to send the caret to the end of the EditBox, which works as long as the user only enters a number from left to right. But it won't work when the user is trying to add, remove or edit digits in the middle of the number. I need the caret position at the exact spot of the number where it was before the user pressed a key.
I tried calculating the new caret position from the previous one (gotten with CEdit::GetSel()) and the previous length of the number:
OnEnChange()
{
int prevCursPos {HIWORD(m_editCtrl.GetSel())};
int prevStrLen {m_editCtrl.GetWindowTextLengthW()};
UpdateData(TRUE);
// Adding/Removing of separators as needed
UpdateData(FALSE);
int difference {m_editCtrl.GetWindowTextLengthW() - prevStrLen))};
if(difference > 0) // a separator has been added to the string
m_editCtrl.SetSel(-1, prevCursPos + 1);
}
However, the SetSel() function doesn't seem to have any effect. I also tried to send an EM_SETSEL message instead, but I couldn't figure out how to make that work either, the caret always resets itself to the beginning of the EditBox.
Does anyone have another idea on how to accomplish what I'm trying to do?
It is unclear how you are handling "/ Adding/Removing of separators as needed".
SetSel with the first parameter set to -1 will position the caret at the beginning of the string if the string changes after calling UpdateData(false)
Create CString type of the variable (m_csEdit for example) for this edit control and
int iLen = m_csDDx.GetLength();
m_editCtrl.SetSel(iLen, -1);
or
m_editCtrl.SetSel(iLen, iLen);
You can calculate the length differently from what I suggested.
Consider using masked edit control (maybe).
Forgot to mention. Use GetNumberFormatEx to format number with thousand delimiter.

(C++) Can the color of text change as it is typed?

I have a list of instructions in my program and they are activated by entering a string. There are a large number of possible instructions. You could call them commands if you like.
I already have a program that can successfully execute the instructions I've added so far.
For example, adding a person to the database would require the user to enter add "John" "Doe".
This would output to the screen Added John Doe to the database, ID#1234. The IDs are random.
I know how to add colors; in this output text, "John Doe" would be colored green.
What I'm wondering is, can I make it so that color changes as one types? Because I learned how to use some kind of keyboard mode change so that when I type a password, all characters are displayed as * of any color I desire, or even nothing displayed at all, through "display" of \0, and I know how to do that. I was wondering if the color could change before the complete string (extracted with std::getline(std::cin, str);) is typed.
The first reason I want the colors to change is because when there are so many commands, and I already have more complex commands than that, I want to provide a way for the user to be able to correct syntax mistakes before they press enter. Something like Windows PowerShell, perhaps, which was written in C#. I know that C# is a very different language than C++, but if C# can achieve something like that, I want to see if C++ can as well. My hope is that it doesn't require thousands of lines of application-specific code, especially considering that PowerShell is an actual application and not a simple terminal-run executable. And while PowerShell appears to be open-source, I don't understand C#. See the bottom for the second reason.
I have no idea if this is possible, but because similar manipulation of entered text is possible (as I said, I know how to add colors and also mask text as some other single character like *), I want to know if this is also possible.
Simple examples:
Firstly, the user should know when they have not entered a valid command, and when they have. I want the text to be in red until the letters entered so far consist of an actual keyword, like add. So, the text would be red until the second d is added, when it reverts to white, and if another letter is entered, it becomes red again.
Example 1: add "John Elias" "Doe"
After the space, the text after "add" should be red no matter what, unless the character after the space is a quotation mark. In order to tell the user that they have not terminated the string, the text beyond the (orange?) quotation mark should be orange. When the final quotation mark is entered, the entire content of the quotation marks (including the quotation marks) should be some other color (probably green?) to tell the user that they have successfully entered an argument. The same applies to any instances of quotation-mark arguments. Note that a space is allowed in a quotation argument.
Example 2: list-info 1234
In this command, it gets more complex. list is a separate command, so the text should be red until t is entered, and it turns white. But then it turns red again after that, until o is entered, and it turns white again. The numerical argument following it should be red if the entered character isn't a digit. If it is, it's still red, because the only valid IDs are 3- or 4-digit numbers. It should turn green(?) once a third digit is entered, and still stay green when another digit is entered. But if a fifth digit is entered (or another character for that matter), the number turns red again. Although this would better be implemented as returning an error if the entered number is invalid, I would still like to know if this can be done as well.
Example 3: add "John" "Elias" "Doe" "fourth-string"
Since there is an overloaded function that enables an explicit first-middle-last name to be stored as well, it should be ok if there is a third string. But if there is a fourth string added, then it should be in red no matter what because add cannot take more than 3 arguments.
My question is, are any of these things possible? And yes, I am aware that it is almost certainly better to just implement an error system, but my intention is to expand my coding ability, and that is the second reason, and coding an error system will not do that because I have already done that for every command.
For reference, I'm operating in Linux Ubuntu 18.04, I compile with g++, my code conforms to C++17, I use ANSI escape sequences for color, bold, etc., and for masking characters with something like * I use a pointer to a char array (passed by address as char**) and a C-style FILE* to reference the input stream stdin (because I haven't bothered to conform it to a typical C++ implementation yet, learning ways to advance my current skills is my priority at this point in time).

C++ - Displaying and Storing Japanese Characters

So, I'm attempting to use C++ and Windows Forms to create an application that will help me study Japanese (for now, Hiragana and possibly Katakana only). The aim is to be able to create a program that has the user select the character sets they want to use (A through O, KA through KO, etc.), and either view the cards freely or have the program test them over the characters. For debugging purposes, I currently have the View button set to output 5 values to 5 different text boxes - the Roman pronunciation, the corresponding character, its position in an array in which all of the characters are stored, and a Boolean value.
My problem lies in the fact that the characters all show up as "?", and I get multiple warnings when I compile. An example of this warning:
1>c:\users\cameron\documents\visual studio 2010\projects\japanesecards\japanesecards\Form1.h(218): warning C4566: character represented by universal-character-name '\u3093' cannot be represented in the current code page (1252)
This shows up 46 times, 1 for each Japanese character in the array. The array's declaration line is,
std::string hiraList[5][11][2];
An example of inserting a Romanji-Hiragana pair is,
hiraCheck[0][0][0] = "A";
hiraCheck[0][0][1] = "あ";
Finally, the Hiragana is being inserted into a text box using the following code:
System::String^ displayText = gcnew String(hiraList[x][y][1].c_str());
textBox5 -> Text = displayText;
Basically, given all of this, my question is - How can I get my form to display Japanese characters properly in a text box?
Okay! I've done a bit of tweaking and experimenting with wchar_t, and I've found out a solution.
First, I reduced the hiraList array to a two-dimensional array, and moved the Hiragana characters into their own, array, defined like so:
wchar_t hiraChar[5][11];
And added values like so:
hiraChar[0][0] = L'あ';
Then, I went down to the code for the 'View' button and made a few changes:
Deleted the method for declaring and filling the displayText variable
Updated the line of code which assigns textBox5 its text value to read from hiraChar[x][y]
A line of the new code has been pasted below:
textBox5 -> Text = hiraChar[x][y].ToString();
In essence, the program now creates three variables for Hiragana - One to monitor check boxes, one to store the romanji values, and one to store Hiragana characters. When at least one check box is selected, and the View button pressed, five things are outputted to text boxes - the character, its position in the array (x and y are separate boxes), its romanji equivalent, and a 'True' value which was used earlier in development for debugging purposes.

Visual Studio C++ removing last character from string

I need a little help with my calculator program. I have created the code for the main buttons like the numbers 0-9 and the arithmetic operators to make it perform simple calculations.
What I'm having problems with right now is making the CE button work, after clicking the CE button I need the last entered character to be removed from the display label.
I have tried to adapt this code somehow, but it doesn't work:
lblResult->substr(0, lblResult->size()-1);
I know I'm doing somehting wrong here, can you please help me?
Thanks in advance
...Now that we know that lblResult is a System.Windows.Forms.Label, we can look at the documentation.
A Label has a Text Property, which is a String^ (i.e. a string reference).
For what you want to do, the Remove Method of String is appropriate. But note in the documentation it says that it "Returns a new string in which a specified number of characters from the current string are deleted." This means that it does not modify the string, but returns a modified copy.
So to change the label's text, we need to assign to its Text property what we want: the current string with all of the characters except the last:
lblResult->Text = lblResult->Text->Remove(lblResult->Text->Length - 1);
lblResult->resize(lblResult->size() - 1);
In this case you can use components Remove and Length methods.
Use the following code to access the components text:
component->Text
Than remove the last character of string by accessing Remove and component Length method
= component->Text->Remove(component->Text->Length - 1)
I hope you find this useful.
Just asking the obvious -- the whole statement is
*lblResult = lblResult->substr(0, lblResult->size()-1);
right?

get char on screen

I've looked through the NCurses function list, and I can't seem to find a function that returns the characters already printed on the screen. Is there an accessible value for the char stored in each character cell? If not, is there a similar function in the Windows terminal?
I want to use this to replace all the characters on the screen of a certain value (ex: all the a's) with a different character, or with new attributes.
The function inch() gets the character and returns it as a chtype. Use winch() to get a character from a window other than stdscr.