WinRT/C++ TextBox doesn't accept any character input in ContentDialog - c++

I have a desktop program with a WinRT/C++ library (and Microsoft.UI.Xaml.Controls 2.8 prerelease), which in the application will bring up a ContentDialog with a TextBox as its content. However, unfortunately the TextBox does not accept any character input from the keyboard, even though the TextBox is in a focused state. For the experiment so far, the TextBox can only accept input from the Copy and Paste methods. So, is there any workaround or manipulation that can be done to solve this problem?
Code:
TextBox tbox{};
ContentDialog dialog{};
dialog.Title(box_value(L"Title"));
dialog.Content(tbox);
dialog.PrimaryButtonText(L"Create");
dialog.CloseButtonText(L"Cancel");
dialog.XamlRoot(XamlRoot());
auto result = co_await dialog.ShowAsync();
if (result == ContentDialogResult::Primary)
{
// Create new data from tbox.Text()
}

Related

Performantly appending (rich) text into QTextEdit or QTextBrowser in Qt

QTextEdit can be appended text to simply using append(). However, if the document is rich text, every time you append to the document, it is apparently reparsed. This seems like a bit of a trap in Qt.
If you're using the edit box as a log window and appending text in fast successions as a result of external signals, the appending can easily hang your app with no intermediate appends shown until each of the appends have completed.
How do I append rich text to a QTextEdit without it slowing down the entire UI?
If you want each append to actually show quickly & separately (instead of waiting until they've all been appended before they are shown), you need to access the internal QTextDocument:
void fastAppend(QString message,QTextEdit *editWidget)
{
const bool atBottom = editWidget->verticalScrollBar()->value() == editWidget->verticalScrollBar()->maximum();
QTextDocument* doc = editWidget->document();
QTextCursor cursor(doc);
cursor.movePosition(QTextCursor::End);
cursor.beginEditBlock();
cursor.insertBlock();
cursor.insertHtml(message);
cursor.endEditBlock();
//scroll scrollarea to bottom if it was at bottom when we started
//(we don't want to force scrolling to bottom if user is looking at a
//higher position)
if (atBottom) {
scrollLogToBottom(editWidget);
}
}
void scrollLogToBottom(QTextEdit *editWidget)
{
QScrollBar* bar = editWidget->verticalScrollBar();
bar->setValue(bar->maximum());
}
The scrolling to bottom is optional, but in logging use it's a reasonable default for UI behaviour.
Also, if your app is doing lots of other processing at the same time, appending this at the end of fastAppend, will prioritize actually getting the message displayed asap:
//show the message in output right away by triggering event loop
QCoreApplication::processEvents();
This actually seems a kind of trap in Qt. I would know why there isn't a fastAppend method directly in QTextEdit? Or are there caveats to this solution?
(My company actually paid KDAB for this advice, but this seems so silly that I thought this should be more common knowledge.)

MFC C++ VS 2010 : Edit Box to accept only alphabets, backspace and spaces

As mentioned in the title, I am currently using VS 2010 C++ , MFC application for my project. Currently new to programming.
I am currently asked to create an edit box to accept names, full names, e.g "Lee Roy Long". I have looked through many other websites but I am confused with which method should I use to do it.
Is there any examples or a guide to how to go about this?
EDIT: I have another question aside from this solved one [ Cannot Post new questions due to the "restrictions"], I am currently using the same edit box to add new names as strings into the SQLite database. I am currently having some trouble converting CString to string
vector<int> userSerialNumber;
vector<string> userName;
vector<int> userID;
vector<int> userTrainingImagesNo;
Program starts here:
CString str,text;
CString Lone = _T("MEEP"); // This one converts it succesffuly...
string ss((CStringA(Lone)));/Only works for declared CStrings?
CEdit* editBox = (CEdit*)GetDlgItem(IDC_EDIT1);
editBox->GetWindowText(str);
Adding the user's input from above into the program below.
userSerialNumber.push_back(newserialnumber);
userID.push_back(newserialnumber);
userName.push_back(ss);
userTrainingImagesNo.push_back(Img);
I have referred to many websites on how to convert CStrings to strings, but none of them worked, including this one.
As I debug the program, the conversion between CString and string did not work as I get "" for string, which causes the database to update a blank "".
CString str = "name";//Name CString gotten from EditBox
std::string newname = ""; //After typing many conversion methods, results ""
Is there something that I did not notice regarding this ?
You can filter the keystrokes going into the edit control by deriving a class from CEdit and handling the WM_CHAR message in your derived class. To accept a key pass it along to CEdit::OnChar, to reject a key simply return without calling the CEdit function.
To connect the edit control to your code you use a standard MFC subclassing technique. Right-click on the control and create a control member variable (a CEdit) in the parent window. Then edit to change the variable from a CEdit to a CYourDerivedCEdit.
There is a tutorial about this and a sample project at http://www.flounder.com/validating_edit_control.htm
As an alternative to trapping each character, you can handle the CWnd::OnKillFocus event for the edit box and interrogate the value once. Validating can be done by using CString::SpanExcluding with numbers and any other character that should not be in the resulting string. For example,
CString stringEnteredByUser = _T("Lee Roy Long");
CString validatedString = stringEnteredByUser.SpanExcluding("0123456789");
if (stringEnteredByUser != validatedString)
AfxMessageBox(_T("Invalid string"), MB_OK);
The 'stringEnteredByUser' variable should contain the string entered by the user. In this example, using SpanExcluding will tell you if they've entered a number. The returned string from the call (validatedString) will not match the string the user typed (stringEnteredByUser) if they've entered a character that is invalid (ie. the character is within the list provided to the SpanExluding call).
If the validatoin fails, simply force the focus back to the edit box.
I'm assuming you know some basic event coding.
Use the Textbox.textchanged event.
Also research ASCII and its conversion (asc function.)
If you need any more help, comment below.
Good luck!

AvalonEdit: Copy takes forever for large highlighted text files

Actually this was asked on http://community.sharpdevelop.net/forums/p/21949/56153.aspx#56153 but with no answer yet - so I try it here.
I'm using Avalon Edit (ICSharpCode.AvalonEdit.dll 4.4.2) in a WPF 4.0 application.
I have loaded a text file (~7 MBytes) into the editor. When I apply syntax highlighting and then coping (Control-A and Control-C) the whole text it takes forever (without highlighting it's done in a second)
When I break into debugger I get the following callstack (shortened):
System.Text.RegularExpressions.RegexInterpreter.Go()
System.Text.RegularExpressions.RegexRunner.Scan(regex, text, textbeg, textend, textstart, prevlen, quick, timeout)
System.Text.RegularExpressions.Regex.Run(quick, prevlen, input, beginning, length, startat)
System.Text.RegularExpressions.Regex.Match(input, beginning, length)
ICSharpCode.AvalonEdit.Highlighting.DocumentHighlighter.HighlightNonSpans(until)
ICSharpCode.AvalonEdit.Highlighting.DocumentHighlighter.HighlightLineInternal(line)
ICSharpCode.AvalonEdit.Highlighting.DocumentHighlighter.HighlightLineAndUpdateTreeList(line, lineNumber)
ICSharpCode.AvalonEdit.Highlighting.DocumentHighlighter.HighlightLine(lineNumber)
ICSharpCode.AvalonEdit.Highlighting.HtmlClipboard.CreateHtmlFragment(document, highlighter, segment, options)
ICSharpCode.AvalonEdit.Editing.Selection.CreateHtmlFragment(options)
ICSharpCode.AvalonEdit.Editing.Selection.CreateDataObject(textArea)
ICSharpCode.AvalonEdit.Editing.EditingCommandHandler.CopySelectedText(textArea)
ICSharpCode.AvalonEdit.Editing.EditingCommandHandler.OnCopy(target, args)
It seems the editor creates html-based content for the clipboard and uses RegularExpressions which takes forever (~30 seconds).
Question: Does anyone know a possibility to disable the syntax highlighting for the copy action so that only pure text is copied to the clipboard.
I got the answer from DanielGrunwald on SharpDevelop that I want to share:
In avalonedit 4.X it's not possible to disable html copy to clipboard. But in 5.X you can do that.
With:
AvalonEdit.TextEditor TextView
write the following to register the callback for the before-copy event:
DataObject.AddSettingDataHandler(TextView, onTextViewSettingDataHandler);
to register a user handler that is called before cliboard copying is processed. In that handler cancel the html format (e.g. depending on the document size). Example:
static public void onTextViewSettingDataHandler(object sender, DataObjectSettingDataEventArgs e)
{
var textView = sender as TextEditor;
if (textView != null && e.Format == DataFormats.Html && textView.Text.Count() > MaxDocByteSizeForHtmlCopy)
{
e.CancelCommand();
}
}
With that code you can prevent that hanger, but of course formatting is not preserved when the content is pasted (e.g. into Word).

How to disable Edit control's focus on Dialog first launch?

Hello everybody reading this. Thanks in advance for your time.
One thing before question: I DO NOT use neither MFC nor Windows Forms, just WinApi in C++.
Well, I am making a polynomial calculator in Visual C++. I added a Dialog to it, which was created in resources (.rc file) using drag'n'drop method. I suppose there would be no such a problem if i created my Dialog with CreateWindowEx (but I don't want to).
My Dialog has a few of Edit Controls. Everything is fine except that when the Dialog is launched, one of Edit controls takes focus to be ready to take keyboard input.
I have included management of EN_KILLFOCUS (Edit sends it to parent when loses focus due to selecting another control).
Here I read from control to wstring (string of wide characters - _UNICODE is defined), use some kind of parser to verify this wstring and remove bad characters, and then put correct string into the same edit control. It works fine, but here is the source of my problem:
When there was no input, parser returns string "0" (not the NULL, string is just set to "0"), as if control had focus and then lost it even before I clicked anything in Dialog.
Due to that, and something else (this is what I have to figure out), at the Dialog launch parser puts this string "0" to edit.
I want to make my edit not be able to take input from keyboard until i click one of the Edits (including this one).
If it is not possible, I want to clear the whole text at the beginning of dialog (being able to take input is not a problem, I just want to prevent parser from entering string "0" at the beginning)
My code:
In DlgProc I have:
//up here is switch to manage all controls
case MyEditID: // here is ID of one of my edits from resources
switch (HIWORD(wParam))
{
case EN_KILLFOCUS: // edit lost focus - another control selected
if (LOWORD(wParam)==MyEditID) //necessary to determine if
// one of allowed Edits sent this message
// because I have also other Edits
{
GetDlgItemText(hPanel, LOWORD(wParam), MyTempWcharArray, 100);
MyTempString.assign(MyTempWcharArray);
w1 = polynomial(MyTempWcharArray); // parser takes the string
// and removes bad chars in constructor
// polynomial is my class - you don't have to care of it
// w1 is declared before as object of polynomial class
MyTempString = w1.ConversionToString();
SetDlgItemText(hDialog, LOWORD(wParam), sw1);
}
break;
}
break;
does it matter what integer number is set to Edit's ID?
I know SetFocus(), and WM_SETFOCUS message. In this case I just can't get this working.
If i haven't included something important to make you see my point please let me know. I'm sorry I'm just a newbie in WinAPI world.
EDIT:
For those with a similar problem: Do not do this:
I made an workaround with global variable ProcessKillFocus set to false indicating that instructions in message management should not be processed, except that at the end (just before break;) I am changing it to true, so next time and later it will be processed:
case EN_KILLFOCUS:
if (ProcessKillFocus && LOWORD(wParam)==MyEditID)
{
// first time global ProcessKillFocus is false so all this is skipped
// 2nd time and later do all the stuff
}
ProcessKillFocus = true;
break;
Huge thanx to Sheyros Adikari for making my question easy to understand!!!
Huge thanx to patriiice for simple answer on a huge messing question!!!
ANSWER:
BTW: patriiice, I tried this:
case WM_INITDIALOG:
SetFocus(GetDlgItem(hDialog, Desired_Control_ID));
return (INT_PTR)FALSE;
break;
IT JUST WORKS!!!
You have to return FALSE to WM_INITDIALOG message and set the correct focus by yourself.

Preventing newline when pressing enter in WxWidgets text control

I am writing an application with WxWidgets, and have run into an issue with a multiline text control (wxTextControl). It is the input field in a chat window, and it needs to be multi line in case the user types a longer message that needs to wrap. I want the send event, e.g. the action that is taken when the Send button is pressed, to be executed when the user presses enter in the control. I have this working using the wxEVT_COMMAND_TEXT_ENTER event, with the wxTE_PROCESS_ENTER style enabled. However, the problem is that while the send command does get executed, a new line character \n is also appended to the text (this happens after the send command and after I have cleared the text, resulting in an empty field except for a new line). I tried to avoid this by trapping both char and key down events, but for some reason they are not firing.
I simply want to avoid the new line being shown at all. Does anyone have any tips?
I am developing on Windows, but the application is meant to run on all platforms supported by WxWidgets.
You might track wxEVT_COMMAND_TEXT_UPDATED and clear the editor when a new-line is entered (it assumes you are also clearing the editor when Enter is pressed).
BEGIN_EVENT_TABLE(Test,wxFrame)
EVT_TEXT_ENTER(EditorID, Test::OnEnter)
EVT_TEXT(EditorId, Test::OnText)
END_EVENT_TABLE()
void Test::OnEnter(wxCommandEvent&)
{
Send(editor->GetValue());
editor->Clear();
}
void Test::OnText(wxCommandEvent& event)
{
if (event.GetString() == wxT("\n")) { //seems to work, not much info in documentation?
editor->Clear();
}
}