[Webinar] Streamline your web hosting managementRegister Today


pass string parameter from textbox in one window to another windiw in C#

Posted on 2010-03-25
Medium Priority
Last Modified: 2013-11-12
Hi experts,

Here is my question:
This is in a WPF application using C#

In Window1.xaml

I have a textbox that looks like this:

<TextBox Height="23" Name="textBox1" Width="88" />

My code takes the string that was input in the textbox and puts it in a SQL server query and when a user presses a submit button it queries the database.

So in my code file: Window1.xaml.cs this is the code that handled the textbox string

        private string GenerateQuery()
            string val = null;
            string recordNumber = textBox1.Text;

            if (!(string.IsNullOrEmpty(recordNumber)))
                val = "SELECT RECORD_NUMBER, PAGES, RECORD_DATE FROM TABLE1 WHERE RECORD_NUMBER = '" + recordNumber + "'";

            return val;

That works fine and dandy.

So on another page called Window2.xaml

In part of the C# code

I have a query that looks like this

query += "FROM TABLE1 WHERE RECORD_NUMBER = '" + recordNumber + "'";

Is there a way that I can access the string variable recordNumber that was input into textbox1 in Window1 from my code in Window2?

I thought maybe i can declare the variable again in window2.xaml.cs and do it like this:
Saying "I am declaring a variable called recordNumber which is found in Window1"

string recordNumber = Window1.textBox1.Text;

But it's not liking that.     Has anyone done this before? Can anyone help?

Question by:maqskywalker
LVL 23

Expert Comment

by:Roshan Davis
ID: 28605540
keep a static variable in either window or window2 and assign the value there instead of using the window controls directly.

Accepted Solution

Mike_Mozhaev earned 2000 total points
ID: 28610922
One option is to create an external component for communication between these windows. In the simplest case it will just hold RecordNumber value and have reference to this component in both windows. Or it can expose an event RecordNumberChanged so that 2nd window can subscribe to it and get notifications.

The reference to this communication helper object can be set upon creation of windows. Something like that:

public class Mediator
    public string RecordNumber {get;set;}

public class App
    public void Func()
        Mediator mediator = new Mediator();
        Window1 w1 = new Window1(mediator);
        Window2 w2 = new Window2(mediator);

Expert Comment

ID: 28621661
Roshmon - that is the cheap way out and not a good practice to use.  

Mike has a good idea except for this promotes coupling between your classes.  I would look closely at your design to see if it is ideal (passing value from one window to the other doesn't make sense to me in the context you put it in).  

I think your best bet would be to keep a property in the calling application (in the example above this would be in the App class.  Since the App class is the "parent" of each of the windows you can reference back to the parent class without coupling the two windows together.  

Expert Comment

ID: 28650486
Window1 and Window2 do not depend on each other. The only dependency is Mediator. Though to loosen coupling between classes more we can extract an interface of Mediator.

public interface IMediator
    string RecordNumber {get;set;}

This is still just a quick fix. Without knowing the context it's difficult to suggest an optimal solution. So only the direction to move in...

Author Closing Comment

ID: 31707303

Your suggestion helped me get unstuck.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question