Entering newline in a cedit control - visual-c++

A very straightforward question....
How do you enter a new line in a CEdit control box without it triggering OK and closing the dialog box altogether? What I mean is when you hit the enter key it automatically selects OK, even if your cursor is still in the CEdit control. Is what I am trying to do possible? Or do i have to use some other control
PS: It is a modal dialog box btw.

There are various solutions for this problem.
Basically what you need is the ES_WANTRETURN style on the edit control.
Another solution is to check the message and key in PreTranslateMessage (since it has been commented upon this is not the recommended way, I'm just mentioning it for possibilities):
BOOL CYouDialog::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN &&
GetFocus() == youcontrol)
{
return TRUE;
}
return FALSE;
}
The other solution is to handle WM_GETDLGCODE. You should subclass the edit control and do this:
UINT CYourEdit::OnGetDlgCode()
{
return CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS;
}
UPDATE: FYI, also have a look at Just because you're a control doesn't mean that you're necessarily inside a dialog box.

The default dialog processing, as you've discovered, is to close a dialog box when enter is pressed. MFC actually executes the OnOK processing, but you can override that. Here's and old explanation, but, it's still relevant.

Related

How can I set a CListBox to disallow the user changing the selected items? Without changing the selection mode from Extended? [duplicate]

I have a dialog based MFC application. I have a hidden Edit Control on it which has the default focus for reading USB connected scanner input. All other controls on the dialog are read only. But when user clicks on any of the controls, the hidden edit control loses the focus. How can I solve the problem, so that this hidden edit control has the focus even after user clicks on the dialog box anywhere.
Well, most mouse messages in an MFC application are posted and not sent. So, you could override PreTranslateMessage(MSG*) in your CDialog derived class and then eat those message by returning TRUE to prevent message dispatch or FALSE to allow normal processing.
BOOLCMyDlg::PreTranslateMessage(MSG* pMsg)
{
switch (pMsg->message)
{
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
///etc, etc
{
// might want other tests
return TRUE;
} break;
}
return CDialog::PreTranslateMessage(MSG* pMsg);
}

How to prevent a dialog from closing when hitting enter or escape?

I have a dialog that I would like to stay open except if the [x] or exit buttons are clicked. Dialogs seem to always close if you hit escape or if you hit enter when most objects are selected (e.g. string boxes), whether you use pose() or display(). Is there a way to prevent this?
Some public examples of when this occurs are DM Mitchell's "Example: Thread and Dialog Interaction" and "Example: Single Button Dialog" but it seems to apply to any dialog.
The exception seems to be a docked palette, but this functionality seems to be rather limited in GMS2.3/GMS3.
Example code to test:
class myUItest:UIframe
{
object Init( object self )
{
TagGroup dlgTGitems
TagGroup dlgTG = DLGCreateDialog("Modal",dlgTGitems)
TagGroup dlgField = DLGCreateIntegerField(12,20)
dlgTGitems.DLGAddElement(dlgField)
return self.super.init(dlgTG)
}
}
{
object DLG = ALLOC(myUItest).Init()
DLG.Pose()
DLG.display("Modeless")
}
There seems to be no way to prevent termination of modal dialogs (those presented via the Pose method of UIFrame) via the keystrokes you mention. However, would a modeless dialog work for your application? Such a dialog is created by invoking the Display method on your UIFrame object and it is not closed via esc or enter.

MonoTouch.Dialog: Dismissing keyboard by touching anywhere in DialogViewController

NOTE: There are two similar SO questions (1) (2), but neither of them provides an answer.
TL;DR: How can one dismiss the keyboard in a MonoTouch.Dialog by letting the user touch any empty space in the view?
I'm writing an app using MonoTouch.Dialog and a UITabBarController. One of my tabs is "Settings"...
When the user starts typing, the keyboard obstructs the tabbar...
Using MonoTouch.Dialog, the only way to dismiss the keyboard is to go to the last field and press the "return" key. Considering the fact that the user cannot press any tab until the keyboard is gone, I would like a better way to do it. Namely, to dismiss if the user taps anywhere else on the screen.
Without MonoTouch.Dialog, it's a snap: simply override TouchesBegan and call EndEditing. But this doesn't work with MT.D. I've tried subclassing DialogViewController and overriding TouchesBegan there, but it doesn't work. I'm currently at a loss.
Or, I wonder, would I be better off ditching the tabbar so I can use a UINavigationController with a "Back" button on top, which won't be hidden by the keyboard?
I suggest you use a tap gesture recognizer that will not cause interference with the TableView event handlers:
var tap = new UITapGestureRecognizer ();
tap.AddTarget (() => dvc.View.EndEditing (true));
dvc.View.AddGestureRecognizer (tap);
tap.CancelsTouchesInView = false;
You missed my question about it also: Can the keyboard be dismissed by touching outside of the cell in MonoTouch.Dialog?
:-)
This is my #1 feature request for MonoTouch.Dialog.
To answer your question: No. It is not possible. I have searched and asked around and have not found any answers.
I assume because it is just a sectioned (grouped) table and if it wasn't sectioned, there wouldn't be any spot to click. However, that is just my speculation.
I wish that miguel or someone that works on monotouch would answer this and say if it is even possible. Possibly a future enhancement?
I figured out a workaround that satisfies me well enough, so I'm answering my own question.
// I already had this code to set up the dialog view controller.
var bc = new BindingContext (this, settings, "Settings");
var dvc = new DialogViewController (bc.Root, false);
// **** ADD THIS ****
dvc.TableView.DraggingStarted += (sender, e) => {
dvc.View.EndEditing (true);
};
This will dismiss the keyboard whenever the user drags the view a little bit. There's no touch event I could find associated with the tableview, so this is the next best thing. I'd welcome any other ideas. Cheers!
One workaround to use the dragging gesture instead of the tap as proposed (that do not interfere with the table view gestures) is to override MonoTouch.Dialog.DialogViewController.SizingSource (or MonoTouch.Dialog.DialogViewController.Source if you don't want uneven rows) and give it to the DialogViewController. I don't know if it is very clean or safe.
public class CustomTableViewSource : MonoTouch.Dialog.DialogViewController.SizingSource
{
public CustomTableViewSource(MonoTouch.Dialog.DialogViewController dvc) : base(dvc)
{
}
public override void DraggingStarted(UIScrollView scrollView)
{
base.DraggingStarted(scrollView);
if (scrollView != null)
{
scrollView.EndEditing(true);
}
}
}

Change Edit Box content when Button is clicked in mfc

I have an Edit Box and a Button on a dialog. How can I change the content in the edit box runtime as the button is clicked? I have to read a new record from a file and post it in the Edit Box as the Button is clicked and I am using mfc.
Once you have trapped the button press, in most cases the easiest way to change text in an Edit Control is:
SetDlgItemText(IDC_EDIT_ID, "Desired Text String")
Where IDC_EDIT_ID is the ID of the Edit Control (set in the properties window)
You can set the text of an Edit control (wrapped by the CEdit class in MFC) by calling the SetWindowText method, which it inherits from the CWnd base class.
So then all you need to do is respond to a click event on your button control. You do this by listening for the BN_CLICKED notification from the appropriate button control within your parent window's OnCommand method.
Something like:
BOOL CMyDialog::OnCommand(WPARAM wParam, LPARAM lParam)
{
if (HIWORD(wParam) == BN_CLICKED && LOWORD(lParam) == IDC_MYBUTTON)
{
m_Edit.SetWindowText(TEXT("My string"));
}
return CWnd::OnCommand(wParam, lParam);
}
Obtaining and reading a book on MFC would be very helpful. This is fairly basic stuff, but it's a lot to cover in a single answer if you don't already understand the fundamental concepts.
Using the Class Wizard would make this even easier... Invoke it with the Ctrl+W keys and follow the on-screen instructions. You'll end up with something like:
void CMyDialog::OnMyButton()
{
m_Edit.SetWindowText(TEXT("My string"));
}

CEdit selects everything when getting focus

When I move to a CEdit control on my dialog using the tab key or the arrow keys all the text in the control is selected. This behaviour is causing me problems and I would prefer it if the control just put the cursor at the start (or end) of the text and didn't select anything.
Is there a simple way to do this (e.g. a property of the control that I can set)?
Another way of achieving your goal is to prevent the contents from being selected. When navigating over controls in a dialog the dialog manager queries the respective controls about certain properties pertaining to their behavior. By default an edit control responds with a DLGC_HASSETSEL flag (among others) to indicate to the dialog manager that its contents should be auto-selected.
To work around this you would have to subclass the edit control and handle the WM_GETDLGCODE message to alter the flags appropriately. First, derive a class from CEdit:
class CPersistentSelectionEdit : public CEdit {
public:
DECLARE_MESSAGE_MAP()
afx_msg UINT OnGetDlgCode() {
// Return default value, removing the DLGC_HASSETSEL flag
return ( CEdit::OnGetDlgCode() & ~DLGC_HASSETSEL );
}
};
BEGIN_MESSAGE_MAP( CPersistentSelectionEdit, CEdit )
ON_WM_GETDLGCODE()
END_MESSAGE_MAP()
Next subclass the actual control. There are a number of ways to do this. To keep things simple just declare a class member m_Edit1 of type CPersistentSelectionEdit in your dialog class and add an appropriate entry in DoDataExchange:
// Subclass the edit control
DDX_Control( pDX, IDC_EDIT1, m_Edit1 );
At this point you have an edit control that doesn't have its contents auto-selected when navigated to. You can control the selection whichever way you want.
I don't think that such a style exists.
But you can add OnSetfocus handler with the wizard:
void CMyDlg::OnSetfocusEdit1()
{
CEdit* e = (CEdit*)GetDlgItem(IDC_EDIT1);
e->SetSel(0); // <-- hide selection
}
Please note that there must be a code in your program to highlight the selection. Please find something like this:
CEdit* pEdit = ((CEdit*)GetDlgItem(IDC_EDIT1));
pEdit->SetFocus();
pEdit->SetSel(0, -1); // select everything
Simply comment the last two lines, instead of >SetSel(0). Your code is enabling and disabling which is meaningless to me.

Resources