how to clear textbox text in C++/XAML - c++

im new to C++/XAML, using VS2012, working on my first Windows 8 app.
I have created a textbox1 that take a number, another textbox2 that display the results, another button that once it is clicked, it does the calculation. everything works, my question is when user want to do the calculation again, he will need to click on textbox1, press the backspace to erased the last entered number, how can i make it when textbox1 is clicked and tapped, it will auto clear the previously entered text? or how do i use/make a "CLEAR" button to handle the text clearing for textbox1 and textbox2? Thank You!

With button:
<Button Content="Clear" Name="button1" Click="button1_Click" />
Code behind:
void YourClass::button1_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ args)
{
textBox1->Text = "";
textBox2->Text = "";
}
If you want to use some logic when you tap or something else you should take a look at Tapped event. I'd do it another way: Whenever it got focus(GotFocus event) then select all the text in text box(textBox1->SelectAll(); in GotEvent handler).

You can use
SetWindowText is a function to set the contents of a Edit Control
m_myEditCtrl.SetWindowText(_T("")); // if using MFC
SetWindowText(hWndMyEditCtrl,_T("")); // When using plain Win32 API
myEditCtrl.Text = ""; // When using C++CLI
You can use it from the "On Click" handler also if needed!

I think that could be useful, if you just add in the end of the program that apply to your textboxes a new button with:
textBox2->Text = String::Empty;
textBox1->Text = String::Empty;
OR
…you could add these code lines before start the procedure of the program, just in the beginning of the program. Each time it would start over with blank.
Cheers.

Related

QLineEdit: how to put cursor at the end of typed text?

There is a QLineEdit in which you need to enter a phone number. Gave him a mask
ui->lineEdit_4->setInputMask("+7\\(999\\)999\\-99\\-99;_");
Further, when saving the data, the entered string is checked by the validator:
QRegularExpression numberRegex ("^\\+\\d{1,1}\\(\\d{3,3}\\)\\d{3,3}\\-\\d{2,2}\\-\\d{2,2}$");
QRegularExpressionValidator *numberValidator = new QRegularExpressionValidator (numberRegex);
QString a = ui->lineEdit_4->text();
int b = ui->lineEdit_4->cursorPosition();
if(numberValidator->validate(a, b) == QValidator::Acceptable){
....
}
Above I described everything that is, and now the very essence of the problem. Typing is extremely inconvenient, since the cursor is not attached to anything. Wherever you click, the cursor will appear there. How to make the cursor appear where you need to type text? So far, in ideas there is only a check for the cursor position, and every time it changes, put it on the first "_" in the line. But something sounds crazy
Connect this signal: https://doc.qt.io/qt-5/qlineedit.html#cursorPositionChanged
In the target method, move cursor to the end of the field with this: https://doc.qt.io/qt-5/qlineedit.html#end
Something like this:
QObject::connect(
ui->lineEdit_4,
&QLineEdit::cursorPositionChanged,
this,
[this](){
ui->lineEdit_4->end(false);
});

Validating an Edit Control's text while the user types MFC

Question/Problem: I have an edit control (text box) that the user enters a username into. I am attempting to compare the username entered to the ones listed in my List Control. If the username given matches, my button's text should change from Create User to Update User.
My problem is in finding the correct event/time to compare the strings, without creating an infinite loop.
What I have tried: I have tried using the edit control events EN_CHANGE and EN_UPDATE. Both of these events cause stack-overflow exception or an infinite loop. I thought that one of these events would be called every time something is typed or the backspace was used within my edit control.
In my EN_CHANGE / EN_UPDATE event, I compare the username strings and set the button's text. With either event, it is called infinite times:
void Users::OnEnUpdateLoginName() //EN_UPDATE Event
{
bool match = false;
//Compare the edit control text with each List Control text.
for(int i = 0; i<m_UserList.GetItemCount(); i++)
{
if(strcmp(m_UserList.GetItemText(i,0),m_loginName)==0)
match = true;
}
//If the usernames match, change the button's text to "Update User"
if(match)
{
CWnd *currentSelection = GetDlgItem(TXTC_LOGIN_NAME);
currentSelection->SetWindowTextA("Update User");
}
else
{
CWnd *currentSelection = GetDlgItem(TXTC_LOGIN_NAME);
currentSelection->SetWindowTextA("Create User");
}
}
.
If the text in red matches, change the text of the button highlighted in blue.
Should I be using a different event to validate the string in real-time as the user types?
My code had two issues. I needed to use UpdateData, so that data for all my dialog controls would be current. I also was updating the wrong variables. Thanks #rrirower

Continually check process and show Button/TextBox

I am attempting to create a form using VB.Net that checks if the IExplorer process is running and then display a RichTextBox (which advises the user to close IE) is the process = 1 and a Button to proceed to the next form if the process = 0.
That's the easy part, the hard part is that if the process was = 0 when the form was loaded then the user opens IE, I want to remove the button and show the RichTextBox (which advises the user to close IE) and once again if they close IE the Button reappears.
I have the button and RichTextBox in the form_load with an If statement that shows depending on IE being open or not, but i cannot get them to swap over if IE is closed or opened, any help will be greatly appreciated.
Here is the code I have in the Form_load for the RTB and Button
aProc = Process.GetProcessesByName("iexplore")
If aProc.Length = 0 Then
Dim b1 As New Button
b1.Location = New System.Drawing.Point(274, 244)
b1.Name = "btnOK"
b1.Size = New System.Drawing.Size(75, 29)
b1.TabIndex = 5
b1.Text = "OK"
b1.UseVisualStyleBackColor = False
Me.Controls.Add(b1)
AddHandler b1.Click, AddressOf btn_OK
Else
Dim t1 As New RichTextBox
t1.Location = New System.Drawing.Point(170, 233)
t1.Name = "rtbMessage2"
t1.ReadOnly = True
t1.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
t1.Size = New System.Drawing.Size(293, 40)
t1.TabIndex = 5
t1.Text = ("Internet Explorer is Running - Please Close Internet Explorer to Continue")
Me.Controls.Add(t1)
AddHandler t1.Click, AddressOf btn_OK
End If
Two changes I would make:
Add a timer that continually calls the logic (show/hide the Button/RichTextBox)
Make the Button and RichTextBox always there, but initially invisible.
To do this, I would (on load) create the Button and RichTextBox with .Visible = false. Then create a timer that runs every 500 milliseconds (+/-). That timer would call a function that contains the logic you have above. However, instead of creating the controls (with that logic) just reference them and set their visibility.
In essence, create the controls once, run the logic multiple times.

CRichEditCtrl how to ignore a keypress?

i have a rich edit box in one of my dialog of MFC dialog based app.
it can only have numeric values.
Now what i am trying to do is dont allow user to enter a value greater then 4567899.
if user is entering numbers in the the rich edit box and by pressing the number key will make the value already enterd in text box greater then 4567899 then just make the app behave like no key is pressed or just ignore that key press.
I did some research and found that this can be done by EN_MSGFILTER event but i m not sure about that.
so this is the function
void CMyDialog::OnMsgfilterObjectid(NMHDR* pNMHDR, LRESULT* pResult)
{
char tempID[10];
MSGFILTER *pMsgFilter = reinterpret_cast<MSGFILTER *>(pNMHDR);
// TODO: The control will not send this notification unless you override the
// CDialog::OnInitDialog() function to send the EM_SETEVENTMASK message
// to the control with either the ENM_KEYEVENTS or ENM_MOUSEEVENTS flag
// ORed into the lParam mask.
// TODO: Add your control notification handler code here
*pResult = 0;
if((pMsgFilter->wParam >= 48) || pMsgFilter->wParam<=57) // check if 0-9 is pressed
{
m_objectIDInstance.GetLine(NULL,tempID); //m_objectIDInstance is a CRichEditCtrl
tempID[m_objectIDInstance.LineLength()] = '\0';
if ((atol(tempID) + (pMsgFilter->wParam-48)) > 4567899)
{
*pResult=1;
}
}
}
in OnInitDialog() i added following line:
m_objectIDInstance.SendMessage(EM_SETEVENTMASK, 0, ENM_KEYEVENTS);
Buts its not working so please if somebody can help or can suggest a different way to do what i am trying to implement.
Thanks
You seem to be sending the message to the dialog. It has to be sent to the edit control.
m_objectIDInstance.SendMessage(...)

How to format the selected text in a QTextEdit by pressing a button

I want to format a selected text in a QTextEdit by clicking a button. For axample I want to make it bold if it is not-bold, or not-bold if it is bold. Please help me with an example.
EDIT:
Actually I have found already a code - qt demo for text editor which does what I need:
void
MyTextEdit::boldText(bool isBold) //this is the SLOT for the button trigger(bool)
{
QTextCharFormat fmt;
fmt.setFontWeight(isBold ? QFont::Bold : QFont::Normal);
mergeFormatOnWordOrSelection(fmt);
}
void
MyTextEdit::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
QTextCursor cursor = m_textEdit->textCursor();
if (!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor);
cursor.mergeCharFormat(format);
m_textEdit->mergeCurrentCharFormat(format);
}
But I can't understand what returnes the textCursor() method, and how the merging of properties is being done? Just some formats are being changed, some of them stay constant. How mergeCharFormat function understands what to change and what to leave as is. Please explain me just these 2 things.
Thanks.
The textCursor() returns a textCursor that contains the position of the cursor you use in the textEdit, see QTextCursor in Qt classes. So by selecting the text that is contained by the cursor start and end position, you have the text that is currently highlited.
As for the mergeCharFormat, I guess that it is used to apply a new state (bold, italic, underlined) and to keep the existing ones. Say your text is already underlined and you apply bold, you would want to keep both.
Hope this helps.