Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

form1 has listbox1 and form2 has textbox1 for adding item to form1 listbox

for eg form2 textbox1.text = sometext added to form1 listbox1.

if i click the edit button listbox selected item its should be open form2 with textbox.text = sometext content for editing

help me the code.

rgds
Cad

[EDIT - OP code from comment]
form1
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}

public string ListBoxValue
{
get { return listBox1.SelectedItem.ToString(); }
}
}

form2

C#
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.ListBoxValue;
}
}


i use above code but when i select and click edit button ,form2 open with text but after change text and click ok then its not updated ,means added new item.
where is my stuck problem.solve this.
Posted
Updated 11-Oct-14 4:14am
v2
Comments
[no name] 8-Oct-14 9:00am    
How about you write the code yourself? There are examples of doing this all over the place. Make some sort of an effort.
cadsolution 8-Oct-14 9:26am    
form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnOpenForm2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.ShowDialog();
}

public string ListBoxValue
{
get { return listBox1.SelectedItem.ToString(); }
}
}

form2

public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}

private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = this.f1.ListBoxValue;
}
}

i use above code but when i select and click edit button ,form2 open with text but after change text and click ok then its not updated ,means added new item.
where is my stuck problem.solve this.

rgds

1 solution

You've got the function to pass the text from form1 to form2 but you also need something to pass the amended information from form2 back to form1 e.g. in form1
C#
public void SetNewListBoxValue(string newValue)
{
    listBox1.Items.Add(newValue);
}

and in form2
C#
private void okButton_Click(object sender, EventArgs e)
{
    f1.SetNewListBoxValue(textBox1.Text);
}
 
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