0

I have two forms - one is the main form which has a listbox containing data which is loaded in from a text file. The other is a delivery form. When the user selects an item in the list box and clicks the edit button the delivery form should appear with the selected data displayed in the text box of the delivery form. At the moment I have something like this:

private Visit theVisit = new Visit();
private List<Delivery> deliveries = new List<Delivery>();
private FrmDelivery deliveryForm = new FrmDelivery();

private void updateDelivery()
{
    lstDeliveries.Items.Clear();            
    List<String> listOfD = theVisit.listDeliveries();
    lstDeliveries.Items.AddRange(listOfD.ToArray());            
}

private void btnEditDelivery_Click(object sender, EventArgs e)
{
    deliveryForm.ShowDialog();
    updateDelivery();
}
2
  • Can we see your Xaml? Is this WinForms? Commented Nov 27, 2012 at 21:50
  • Would you post the code from both of your forms please? Commented Nov 28, 2012 at 3:27

2 Answers 2

1

A Form is a class like any other: you can add properties and you can setup accessors.

Use a property on the delivery form which fill a textbox when changed.

All you have to do now is set this value from the main form and show the delivery form.

Delivery Form:

class FrmDelivery: Form
{
    TextBox text1; // Initialize this as usual
    public string DisplayText
    {
       get { return text1.Text; }
       set { text1.Text = Value; }
    }
} 

Main Form:

private void btnEditDelivery_Click(object sender, EventArgs e)
{
    FrmDelivery frm = new FrmDelivery();
    frm.DisplayText = "Whatever Value you want";
    frm.ShowDialog();
}

You could also declare text1 as public, but I don't like given more control than needed. Always pick the most restrictive way.

Sign up to request clarification or add additional context in comments.

2 Comments

i want text to be displayed in textboxes
i have tried this but it doesnt work: private void btnEditDelivery_Click(object sender, EventArgs e) { Delivery deliveryForm = new Delivery(); deliveryForm.customerName = (lstDeliveries.SelectedIndex.ToString()); deliveryForm.ShowDialog(); updateDelivery(); }
0

There are some ways to do this,one is you can use static field to pass the value of selecteditem of listbox to Delivery form

like this :

form1(in selectedindexchanged event of listbox):

public static string listboxselecteditem=listbox1.selecteditem;//here you add selected item of listbox

and then in Delivery form you do :

textbox1.Text=form1.listboxselecteditem;//add value of selected item in listbox to textbox in Delivery form

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.