473,396 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I add an item from a textBox in Form1 to a listBox in Form2?

Hi,

I'm trying to make a program with 2 Windows Forms. In Form1 there's a textBox and a button, and in Form2 there's a listBox. I want the program to add the items written in the textBox to the listBox after pushing the button. How can I do this? How do I link the 2 Forms?
Oct 23 '10 #1
1 3368
Bassem
344 100+
Hello Erika,

There are a lot of ways to connect between your two Forms, and the way you should go is up to your requirements.

To have a link between the 2 Forms, one of them should have a reference of the other, so you may declare and instantiate Form#2 from Form#1, for example on the Form#1 loaded event handler:

1. Go create Form1 and Form2.
2. Inside the code behind of Form1. Declare Form2 as a member.(just like any class or struct)
Expand|Select|Wrap|Line Numbers
  1. Form2 f2 = new Form2()
3. Double click on Form1, this will add an event handler to the code behind.
Expand|Select|Wrap|Line Numbers
  1. private void Form1_Loaded(object sender, RoutedEventArgs e)
  2.         {
  3. }
4. Show Form2.
Expand|Select|Wrap|Line Numbers
  1. Form2.Show();
5. Now there is a link between the two Forms, so go and create a public Method inside Form2.
Expand|Select|Wrap|Line Numbers
  1. public void AddToComboBox(string fromTextBox)
  2. {
  3. // here is the code to make whatever you want..
  4. }
6. On Form1 on the event handler of your button, access that Method by adding to it the value of the Text property of the TextBox.
Expand|Select|Wrap|Line Numbers
  1. f2.AddToComboBox(TextBox1.Text);
Hope that will help.
Oct 26 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hiroshi ochi | last post by:
Hello, Using MSIE 6.0 and above, with javascript is it possible to display an individual tooltip for each item in a listbox? I need this functionality to show the listitems that are longer...
2
by: Alpha | last post by:
How do I change the selected item in a listbox according to a Combox's selected item on the same form? The Combox is from a different table in the same dataset as the Listbox uses. Combox's...
3
by: amber | last post by:
Hello again : How do I specify which item in a listbox I want selected The listbox is already populated, and if the user types in a value into a textbox, I want that value to be highlighted in...
5
by: John | last post by:
Hi, I can't find a simple example for a simple(?) problem. I am working on an application with a variable in form1, that variable is needed in form2 for a calculation but i can't get that...
2
by: Svein Erik | last post by:
C# 2.0 How can i update my progress bar on Form1, from Form2? Form 2 contains a button that is supposed to update the progressbar on Form1 by 5 in value. I can't find out how to do this, please...
1
by: ssknov | last post by:
Hi all any bdy pls tell me how to access textbox control in Form2 from Form1 thanks in advance ssk
4
Mohan Krishna
by: Mohan Krishna | last post by:
Hi Everyone, I'm working with a project, in which i used a listbox and textbox as a combination. Hoping anyone would help me, i explianed my problem:- There is a list of companies (sorted or...
1
by: ashwinigopi | last post by:
I have two forms, form1 & form 2. Form1 has datagridview. I am not supposed use any sql command in form1. So i added form2 and wrote the sql commands using stored procedures to get the data from the...
0
by: Miha | last post by:
Hello im new here and to programing. At the moment i am trying to code a program that would load a webpage in Textbox, then parse number between var lv_screenshots =...
2
by: erbear | last post by:
Hi all, I am new to VB, and I am having some troubles. I have a listbox that when you click on the item it displays a picture in a picture box and a message in a label. It works fine when an item...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.