Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm a c# beginner . and I want to do this :


-------------------------------------------------------------
to understand what I want to do here is this example:I have 2 programs :
-----------------------------------------------------------------
Program1 contains : Textbox(1)
Program2 contains : Textbox(2) and Button(2)

So when I open program 1 and type text in textbox(1)
then go to program 2 and click the button(2) , the textbox(2) gain the text of textbox(1)
-----------------------------------------------------------------
can any one explain how to solve such hard problem, please?
I've read too much so I found things about API and unmanaged dll methods and spy++ and many other things but the information is not organized , I really got more confused.

So if any one could Write the code for that and explain the code in details , I'll really appreciate it.

Thank You very much.
Posted
Updated 2-Mar-12 7:21am
v5
Comments
ZurdoDev 2-Mar-12 12:15pm    
Are you saying you want to read text from a different program and put it into your textbox? Please clarify so we understand.
sympasetic 2-Mar-12 12:26pm    
-------------------------------------------------------------
to understand what I want here is this example:I have 2 programs :
-----------------------------------------------------------------
Program1 contains : Textbox(1)
Program2 contains : Textbox(2) and Button(2)

So when I open program 1 and type in textbox(1)
then go to program 2 and click the button(2) , the textbox(2) gain the text of textbox(1)

clerify your question.but when transfer object from one program to another use session object.
 
Share this answer
 
C#
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ConsoleApplication1
{
    class Program
    {
        // For Windows Mobile, replace user32.dll with coredll.dll
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);


        static void Main(string[] args)
        {
            Process myProcess = Process.Start(@"c:\windows\notepad.exe");
            SetForegroundWindow(myProcess.Handle);
            if (myProcess.Responding)
                SendKeys.SendWait("This text was entered using the System.Windows.Form=s.SendKeys method.");
            Console.ReadLine();
        }
    }
}
 
Share this answer
 
Comments
sympasetic 2-Mar-12 13:33pm    
no this is not what i meant . I want to read the text in notepad not to write to it. thanks anyway for your time.
I don't know how to code this, but I would investigate doing it in the following manner:
When Button(2) is pressed, App(2) should send a message to App(1). When App(1) receives this message, it should get the text from Textbox(1), then compose a message that includes the text as a parameter and then sends that message to App(2). App(2) then processes the message and parameter and displays the text in Textbox(2). :)
 
Share this answer
 
If you can have code in the application with the first textbox that can send information on a channel, then catch that broadcast in the second application, you can use wcf. Check out this article:

.NET Interprocess Communication Revisited[^]

It has the code to easily implement interprocess communication.
 
Share this answer
 
As an simple alternative use remoting. This allows you to call a method on an other process. Helmut Güldenagel his .NET Remoting Sample[^] on codeproject, is probably a good starting point.

Regards
Piet
 
Share this answer
 
Finally ... I tried to get the handle of utorrent control "Edit"(textbox)
And I succeded but now , How to use the handle to get text from the control "Edit" >>> This is the Problem for now.
----------------------------------------------------------------------------
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices ;
using System.Diagnostics;

namespace WindowsFormsApplication5

{
    public partial class Form1 : Form
    {
        //load FindWindow method
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        //load FindWindowEx method
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);




public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

IntPtr Program = new IntPtr(0);
IntPtr myControl = new IntPtr(0);

Program = FindWindow("µTorrent4823DF041B09", null);
if (Program.Equals(IntPtr.Zero))
label1.Text=("Notepad Not Running");
else
{


myControl = FindWindowEx(Program, IntPtr.Zero, "Edit", null);
if (myControl.Equals(IntPtr.Zero))
{
label1.Text = "µTorrent doesn't have an Edit component ?";
}
else
{
label1.Text = "Edit Control: " + myControl.ToString();

}
 
Share this answer
 
v2
Controlling one program by another is always a bad idea, especially if one program is not designed to be controlled (remoting, WPF, sockets, other IPC). The is abuse and going against OS paradigm designed to isolate processes. Right approach is the component approach; the components should be put together and integrated in one process, not different processes. You have enough flexibility in programming technologies. You are just started to move in wrong direction. Don't do it until it's too late.
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900