question

KwebenaAcquah-9104 avatar image
0 Votes"
KwebenaAcquah-9104 asked KwebenaAcquah-9104 edited

How to Access a WPF page control from a windows form control

Hello: i am having a windows form FINGER PRINT VERIFIER and a wpf page i would like to equate the textBox1 value (from windows FINGER PRINT VERIFIER) to the value of Textbox1 in wpf page each time a finger is detected please can some one help me out on this;


Here is what i have tried;

 if (dt.Rows.Count > 0)
                     {
                         for (int i = 0; i < dt.Rows.Count; i++)
                         {
                             lstledgerIds.Add(dt.Rows[i]["LedgerId"].ToString());
                             Fmd val = Fmd.DeserializeXml(dt.Rows[i]["StudentFinger"].ToString());
                             CompareResult compare = Comparison.Compare(firstFinger, 0, val, 0);
                             if (compare.ResultCode != Constants.ResultCode.DP_SUCCESS)
                             {
                                 Reset = true;
                                 throw new Exception(compare.ResultCode.ToString());
                             }
                             if (Convert.ToDouble(compare.Score.ToString()) == 0)
                             {
                                 GetDataOnBarcodeBasis = true;
                                 LedgerId = lstledgerIds[i].ToString();
                                 this.Invoke(new MethodInvoker(delegate()
                                 {
                                     textBox1.Clear(); //windows form textbox1
                                     textBox1.Text += lstledgerIds[i].ToString();
                                      
                                     wpfPage1 page1 = new wpfPage1(); //wpf textbox1
                                     page1.Textbox1.Text += lstledgerIds[i].ToString();
    
                                      //or
                                      //wpfPage1 page1 = new wpfPage1(); //wpf textbox1
                                     //page1.Textbox1.Text = textBox1
                                 }));
                                 GetDataOnBarcodeBasis = false;
                                 //txtledgerid.editvalue = lstledgerids[i].tostring();
                                 count++;
                                 break;
                             }
                         }
                         if (count == 0)
                         {
                             SendMessage(Action.SendMessage, "FingerPrint not Registered.");
                         }
                     }


BUT nothing seems to be working;


dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@KwebenaAcquah-9104, Could you tell me that the wpf page is contained in the winform app or not?

0 Votes 0 ·

No sir it's not contained in the windows form it's a separate window and the finger print windows form is also a separate window on its own;

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JackJJun-MSFT edited

@ KwebenaAcquah-9104, we can try to use SendMessage function (winuser.h) api to send the text from winform textbox to wpf page textbox.

In winform:


  public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
         [DllImport("user32.dll")]
         public static extern IntPtr FindWindow(string lpClassName, String lpWindowName);
    
         [DllImport("User32.dll")]
         private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam);
    
    
         public struct COPYDATASTRUCT
         {
             public IntPtr dwData;
             public int cbData;
             [MarshalAs(UnmanagedType.LPStr)]
             public string lpData;
         }
         public const int WM_COPYDATA = 0x004A;
         private void button1_Click(object sender, EventArgs e)
         {
             IntPtr WINDOW_HANDLER = FindWindow(null, "MainWindow");
             if (WINDOW_HANDLER != IntPtr.Zero)
             {
                 string text = textBox1.Text;
                 byte[] sarr = System.Text.Encoding.Default.GetBytes(text);
                 int len = sarr.Length;
                 COPYDATASTRUCT cds;
                 cds.dwData = (IntPtr)100;
                 cds.lpData = text;
                 cds.cbData = len + 1;
                 SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);
             }
         }
     }

In wpf:



 public partial class MainWindow : Window
 {
     public MainWindow()
     {
         InitializeComponent();
         Page1 p = new Page1();
         this.Content = p;
         this.Loaded += MainWindow_Loaded;
     }
     #region 
     public const int WM_GETTEXT = 0x0D;
     public const int WM_SETTEXT = 0x0C;
     public const int WM_SIZEING = 0x0214;
     public const int WM_COPYDATA = 0x004A;
     public const int WM_LBUTTONDBLCLK = 0x0203;
     #endregion
    
     #region 
     public struct COPYDATASTRUCT
     {
         public IntPtr dwData;
         public int cbData;
         [MarshalAs(UnmanagedType.LPStr)]
         public string lpData;
     }
     #endregion
     private void MainWindow_Loaded(object sender, RoutedEventArgs e)
     {
    
         HwndSource hWndSource;
         WindowInteropHelper wih = new WindowInteropHelper(this);
         hWndSource = HwndSource.FromHwnd(wih.Handle);
    
         hWndSource.AddHook(MainWindowProc);
     }
        
     private IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
     {
         switch (msg)
         {
    
             case WM_COPYDATA:
                 {
    
                     COPYDATASTRUCT mystr = new COPYDATASTRUCT();
                     Type mytype = mystr.GetType();
    
                     COPYDATASTRUCT MyKeyboardHookStruct = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));
                     Console.WriteLine(MyKeyboardHookStruct.lpData);
                     Page1 p = (Page1)this.Content;
    
                     p.txtName.Text = MyKeyboardHookStruct.lpData;
    
    
                     break;
    
                 }
             default:
                 {
                     break;
                 }
    
    
         }
         return IntPtr.Zero;
     }
 }

Page1:

  public partial class Page1 : Page
     {
         public Page1()
         {
             InitializeComponent();
    
         }
    
           
         public string GetText
         {
             get
             {
                 return txtName.Text;
             }
             set
             {
                 txtName.Text = value;
             }
         }
               
    
    
    
    
            
    
     }



If the response is helpful, please click "Accept Answer" and upvote it.


Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.









333.gif (158.7 KiB)
2222.gif (145.9 KiB)
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

please sir do have to import any namespace to prevent err arising if yes can i get links for this;




WINDOWS TEXTBOX to WPF TEXTBOX

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT KwebenaAcquah-9104 ·

@KwebenaAcquah-9104 , I have added the using code. And what links do you want to get? Could you explain it in more detail?

0 Votes 0 ·

please sir do have to import any namespace to prevent err arising if yes can i get links for this;


is there no easy way of doing this; can't i just do this

        set the value of textbox1 in windows form;

                                   textBox1.Clear();              //windows form textbox1
                                  textBox1.Text += lstledgerIds[i].ToString();

  make textbox found in wpf page or window receive the same text in textbox1 from windows form

                                   wpfPage1 page1 = new wpfPage1();         //wpf textbox1
                                  page1.Textbox1.Text = textBox1


please can't i do it this way of some thing similar at least; (that above solution didn't work actually)(name spaces required) please some should help

WINDOWS TEXTBOX to WPF TEXTBOX

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT KwebenaAcquah-9104 ·

@KwebenaAcquah-9104 , Because your wpf app and winfrom app both are single apps, it is hard to do it by using easy way like you said. Also, I add all the code in my answer. Please check it again.

0 Votes 0 ·

actually sir i tried out your code; it was
ERROR FREE;
BUT it did not solve the problem actually;

please sir i am Sending the textBox1 value in WindowsForm to the textbox1 in wpf form or perhaps Page

the windows form contains a textBox1 which show up the id of each individual whose thumb print is detected by my application;

now my problem is to show up each "id" in a different textbox found in a wpf window or may be page; as individuals are detected and the id's are shown in textBox1 in windows form;

             textBox1.Text found in windows form should  = textbox1.Text found in wpf windows or(maybe page)

this is my exact goal but i find it difficult to accomplish; please do help me; and help me make reference to other prodigies like you to try solving my issue please Sir (thanks)


0 Votes 0 ·
Show more comments