473,782 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I pass a string to a new form

Hi:

I have MS C# 2005 Express Edition and I'm trying to pass a string to a
new form in my project.

First I created the form, Customer, with a textbox, "textBox1", and
declared a string "name":

namespace myProject
{
public partial class Customer : Form
{
public string name;
public Customer()
{
InitializeCompo nent();
textBox1.Text = name;
}
}
}

Then, in my main program I have a buttom click event that activates
the form:

private void button1_Click(o bject sender, EventArgs e)
{
Customer dlg1 = new Customer();
dlg1.name = "test";
dlg1.ShowDialog ();
}

But, when I press button1 the string "test" does not show up in
textBox1.

Can anyone tell me what I need to do to get the string to pass to my
new form?

TIA,
Brad McMillan

Mar 18 '07 #1
3 1805
Brad McMillan <mc******@visel ect.comwrote:
I have MS C# 2005 Express Edition and I'm trying to pass a string to a
new form in my project.

First I created the form, Customer, with a textbox, "textBox1", and
declared a string "name":

namespace myProject
{
public partial class Customer : Form
{
public string name;
public Customer()
{
InitializeCompo nent();
textBox1.Text = name;
}
}
}

Then, in my main program I have a buttom click event that activates
the form:

private void button1_Click(o bject sender, EventArgs e)
{
Customer dlg1 = new Customer();
dlg1.name = "test";
dlg1.ShowDialog ();
}

But, when I press button1 the string "test" does not show up in
textBox1.

Can anyone tell me what I need to do to get the string to pass to my
new form?
Let's look at what you're doing:

1) You call the parameterless constructor. This sets textBox1.Text to
the current value of "name", which is null.

2) You set the new value of "name". This doesn't nothing other than
changing the value of the field.

That's why it doesn't work at the moment.

The easiest fix is to change "name" to be private (which is always a
good idea - public fields should be avoided), give the constructor a
parameter, and pass the name in as that parameter. That means that when
you've created the text box, you can set the text of it before even
leaving the constructor.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 18 '07 #2
Hi Jon:

It worked.

Thanks!

On Sun, 18 Mar 2007 19:56:41 -0000, Jon Skeet [C# MVP]
<sk***@pobox.co mwrote:
>Brad McMillan <mc******@visel ect.comwrote:
>I have MS C# 2005 Express Edition and I'm trying to pass a string to a
new form in my project.

First I created the form, Customer, with a textbox, "textBox1", and
declared a string "name":

namespace myProject
{
public partial class Customer : Form
{
public string name;
public Customer()
{
InitializeCompo nent();
textBox1.Text = name;
}
}
}

Then, in my main program I have a buttom click event that activates
the form:

private void button1_Click(o bject sender, EventArgs e)
{
Customer dlg1 = new Customer();
dlg1.name = "test";
dlg1.ShowDialog ();
}

But, when I press button1 the string "test" does not show up in
textBox1.

Can anyone tell me what I need to do to get the string to pass to my
new form?

Let's look at what you're doing:

1) You call the parameterless constructor. This sets textBox1.Text to
the current value of "name", which is null.

2) You set the new value of "name". This doesn't nothing other than
changing the value of the field.

That's why it doesn't work at the moment.

The easiest fix is to change "name" to be private (which is always a
good idea - public fields should be avoided), give the constructor a
parameter, and pass the name in as that parameter. That means that when
you've created the text box, you can set the text of it before even
leaving the constructor.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 18 '07 #3
Maybe you need not use constructor with paremeter,just using Perperty.
like this,

public string SomeName
{
set{textbox1.Te xt=value;}
set{return textBox1.Text;} //if needed
}

And,

Customer customer=new Customer();
customer.SomeNa me='...';

"Brad McMillan" <mc******@visel ect.comдÈëÏûÏ¢ ÐÂÎÅ:45******** *******@dslnews .netwiz.net...
Hi Jon:

It worked.

Thanks!

On Sun, 18 Mar 2007 19:56:41 -0000, Jon Skeet [C# MVP]
<sk***@pobox.co mwrote:
>>Brad McMillan <mc******@visel ect.comwrote:
>>I have MS C# 2005 Express Edition and I'm trying to pass a string to a
new form in my project.

First I created the form, Customer, with a textbox, "textBox1", and
declared a string "name":

namespace myProject
{
public partial class Customer : Form
{
public string name;
public Customer()
{
InitializeCom ponent();
textBox1.Te xt = name;
}
}
}

Then, in my main program I have a buttom click event that activates
the form:

private void button1_Click(o bject sender, EventArgs e)
{
Customer dlg1 = new Customer();
dlg1.name = "test";
dlg1.ShowDial og();
}

But, when I press button1 the string "test" does not show up in
textBox1.

Can anyone tell me what I need to do to get the string to pass to my
new form?

Let's look at what you're doing:

1) You call the parameterless constructor. This sets textBox1.Text to
the current value of "name", which is null.

2) You set the new value of "name". This doesn't nothing other than
changing the value of the field.

That's why it doesn't work at the moment.

The easiest fix is to change "name" to be private (which is always a
good idea - public fields should be avoided), give the constructor a
parameter, and pass the name in as that parameter. That means that when
you've created the text box, you can set the text of it before even
leaving the constructor.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Mar 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
23656
by: Matt | last post by:
In ASP, when we pass data between pages, we usually pass by query string. If we pass data by query string, that means we need to use submit button, not by regular button, and the form will pass to the server since it's GET or POST request. But if just form-to-form communication, why we need to send the form to the server? Please help clarify. Thanks!! <form action="process.asp" method="get"> //form controls <input type="submit">
7
21635
by: Zlatko Matiæ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the server ? Is it possible to use parameters in pass-through queries ? An additional question: Is it possible to connect to a database on MySQL or PostgreSQL using ADO ? Is it possible to execute pass-through queries with parameters, using ADO...
0
3322
by: Zlatko Matiæ | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems appear when someone is trying to use it as front-end for real server database systems such as PostgreSQL or MySQL. One of these problems is regarding pass-through queries and parameters. I wanted to have all the code on client, while executing it on...
6
2577
by: NewToCode | last post by:
I built an application aroung the FileSystemWatcher and i am having trouble passing a string from my mainForm to a second form toastForm. Can someone please give me some insight on how to do this. I am a noob to programming so go easy on me. I have googled searced for 2 days and have yet to find something that makes sense to me. Specifically i am wanting to pass string stringEvent to my toastForm What kind of code do i need to place in...
3
1422
by: Hei | last post by:
Hi All, i using .showdialog to show a child form for user input some data, and i wand to pass back these data to the parent form. how can i achieve this? thx. Hei.
6
5353
by: juky | last post by:
Hi all, I have 2 applications one in VB.net and the other in VC6. I need to pass data between them. How can I do it? what's the best way to implement such communication ? Any comment will be appreciate. Thank you. Juky
4
5218
by: rlntemp-gng | last post by:
I have one module where I would like to launch 2 different forms (that do exist), based on a form object and string that is passed into it. (prmTable is a string, not a table object simply because prmTable is only used in the form's caption) (I know I could put this code behind a form object, but my goal is to modularize this code somewhat and get this technique down so that I can import this code to other applications in the future where...
13
3736
by: magickarle | last post by:
Hi, I got a pass-through query (that takes about 15 mins to process) I would like to integrate variables to it. IE: something simple: Select EmplID from empl_Lst where empl_lst.timestamp between !! And !! Not sure how to do so (should it be a query in Access or a macro) The connection would be ODBC.
12
11111
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.