change an edit box text from another dialog

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • change an edit box text from another dialog

      Hi
      I have made two windows(dialogs) by emwin gui builder in my project, first window has a button and 2nd window has an edit box. i want to change text of edit box of 2nd window when i click on 1st window button directly as follows:

      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
      // USER START (Optionally insert code for reacting on notification message)
      // USER END
      sprintf(tstr,"fwin:%ld %ld \n\r",dialogTest,WM_GetDialogItem(dialogTest,(GUI_ID_USER + 0x01)));
      _DBG_(tstr);
      //WM_ShowWindow(dialogTest);
      //WM_Paint(dialogTest);

      EDIT_SetText((EDIT_Handle)WM_GetDialogItem(dialogTest,(GUI_ID_USER + 0x01)),"thats ok");

      break;


      as you can guess (GUI_ID_USER + 0x01) is id of edit box. but calling Edit_SetText() couses a HardFault.

      By the way i know i can change the editbox text in wm_paint message of 2nd window, but i want to know if i can change it directly from another window or not.
      thanks. :)

      The post was edited 1 time, last by mtajian ().

    • Hi,

      You can use the IDs of a widget to get its handle and then use the handle to change the widgets properties.

      Please don't use functions like WM_Paint(), better let the Window Manager decide when to redraw a window/widget.
      Every time a window/widget gets invalid (either by changing its properties or by calling WM_InvalidateWindow()) it will get redrawn. The redrawing gets performed the next time GUI_Exec() or GUI_Delay() are getting called. Typically one of these functions are getting called within a 'stay alice loop' in the main function (e.g. in MainTask).

      Here is a short example on how to change the text of an EDIT widget by pressing a BUTTOn in another dialog. Here I get the dialog handle by calling WM_GetNextSibling() with current dialog handle. You could also remember the dialog hanlde in a file-wide static variable.

      C Source Code

      1. #include "DIALOG.h"
      2. /*********************************************************************
      3. *
      4. * Externals
      5. *
      6. **********************************************************************
      7. */
      8. /*********************************************************************
      9. *
      10. * Defines
      11. *
      12. **********************************************************************
      13. */
      14. #define ID_WINDOW_00 (GUI_ID_USER + 0x00)
      15. #define ID_BUTTON_00 (GUI_ID_USER + 0x01)
      16. #define ID_BUTTON_01 (GUI_ID_USER + 0x02)
      17. #define ID_WINDOW_10 (GUI_ID_USER + 0x10)
      18. #define ID_EDIT_10 (GUI_ID_USER + 0x11)
      19. /*********************************************************************
      20. *
      21. * Static data
      22. *
      23. **********************************************************************
      24. */
      25. /*********************************************************************
      26. *
      27. * _aDialogCreate0
      28. */
      29. static const GUI_WIDGET_CREATE_INFO _aDialogCreate0[] = {
      30. { WINDOW_CreateIndirect, "Window", ID_WINDOW_00, 0, 0, 100, 100, 0, 0x0, 0 },
      31. { BUTTON_CreateIndirect, "Text A", ID_BUTTON_00, 10, 15, 80, 20, 0, 0x0, 0 },
      32. { BUTTON_CreateIndirect, "Text B", ID_BUTTON_01, 10, 65, 80, 20, 0, 0x0, 0 },
      33. };
      34. /*********************************************************************
      35. *
      36. * _aDialogCreate1
      37. */
      38. static const GUI_WIDGET_CREATE_INFO _aDialogCreate1[] = {
      39. { WINDOW_CreateIndirect, "Window", ID_WINDOW_10, 110, 0, 100, 100, 0, 0x0, 0 },
      40. { EDIT_CreateIndirect, "Edit", ID_EDIT_10, 10, 40, 80, 20, 0, 0x64, 0 },
      41. };
      42. /*********************************************************************
      43. *
      44. * Static code
      45. *
      46. **********************************************************************
      47. */
      48. /*********************************************************************
      49. *
      50. * _cbDialog0
      51. */
      52. static void _cbDialog0(WM_MESSAGE * pMsg) {
      53. WM_HWIN hItem;
      54. int NCode;
      55. int Id;
      56. switch (pMsg->MsgId) {
      57. case WM_INIT_DIALOG:
      58. //
      59. // Initialization of 'Window'
      60. //
      61. hItem = pMsg->hWin;
      62. WINDOW_SetBkColor(hItem, GUI_MAKE_COLOR(0x00400040));
      63. break;
      64. case WM_NOTIFY_PARENT:
      65. Id = WM_GetId(pMsg->hWinSrc);
      66. NCode = pMsg->Data.v;
      67. switch(Id) {
      68. case ID_BUTTON_00:
      69. switch(NCode) {
      70. case WM_NOTIFICATION_RELEASED:
      71. hItem = WM_GetNextSibling(pMsg->hWin); // Get sibling of this dialog which is the other dialog
      72. hItem = WM_GetDialogItem(hItem, ID_EDIT_10); // Get the handle of the EDIT widget
      73. EDIT_SetText(hItem, "Text A");
      74. break;
      75. }
      76. break;
      77. case ID_BUTTON_01:
      78. switch(NCode) {
      79. case WM_NOTIFICATION_RELEASED:
      80. hItem = WM_GetNextSibling(pMsg->hWin); // Get sibling of this dialog which is the other dialog
      81. hItem = WM_GetDialogItem(hItem, ID_EDIT_10); // Get the handle of the EDIT widget
      82. EDIT_SetText(hItem, "Text B");
      83. break;
      84. }
      85. break;
      86. }
      87. break;
      88. default:
      89. WM_DefaultProc(pMsg);
      90. break;
      91. }
      92. }
      93. /*********************************************************************
      94. *
      95. * _cbDialog1
      96. */
      97. static void _cbDialog1(WM_MESSAGE * pMsg) {
      98. WM_HWIN hItem;
      99. switch (pMsg->MsgId) {
      100. case WM_INIT_DIALOG:
      101. //
      102. // Initialization of 'Window'
      103. //
      104. hItem = pMsg->hWin;
      105. WINDOW_SetBkColor(hItem, GUI_MAKE_COLOR(0x00808040));
      106. //
      107. // Initialization of 'Edit'
      108. //
      109. hItem = WM_GetDialogItem(pMsg->hWin, ID_EDIT_10);
      110. EDIT_SetText(hItem, "123");
      111. EDIT_SetTextAlign(hItem, GUI_TA_HCENTER | GUI_TA_VCENTER);
      112. break;
      113. default:
      114. WM_DefaultProc(pMsg);
      115. break;
      116. }
      117. }
      118. /*********************************************************************
      119. *
      120. * _cbBk
      121. */
      122. static void _cbBk(WM_MESSAGE * pMsg) {
      123. switch (pMsg->MsgId) {
      124. case WM_PAINT:
      125. GUI_SetBkColor(GUI_BLACK);
      126. GUI_Clear();
      127. break;
      128. default:
      129. WM_DefaultProc(pMsg);
      130. break;
      131. }
      132. }
      133. /*********************************************************************
      134. *
      135. * Public code
      136. *
      137. **********************************************************************
      138. */
      139. /*********************************************************************
      140. *
      141. * MainTask
      142. */
      143. void MainTask(void) {
      144. GUI_Init();
      145. WM_SetCallback(WM_HBKWIN, _cbBk);
      146. GUI_CreateDialogBox(_aDialogCreate0, GUI_COUNTOF(_aDialogCreate0), _cbDialog0, WM_HBKWIN, 0, 0);
      147. GUI_CreateDialogBox(_aDialogCreate1, GUI_COUNTOF(_aDialogCreate1), _cbDialog1, WM_HBKWIN, 0, 0);
      148. while (1) {
      149. GUI_Delay(100);
      150. }
      151. }
      152. /*************************** End of file ****************************/
      Display All

      Regards,
      Sven
      Please read the forum rules before posting.

      Keep in mind, this is *not* a support forum.
      Our engineers will try to answer your questions between their projects if possible but this can be delayed by longer periods of time.
      Should you be entitled to support you can contact us via our support system: segger.com/ticket/

      Or you can contact us via e-mail.