簡體   English   中英

我需要知道如何將comboBox的選定項顯示在Windows窗體應用程序上嗎?

[英]I need to know how to take the selected item of a comboBox and make it appear on a windows form application?

我有一個帶有ComboBox的Windows窗體應用程序,並且在框中有一些字符串。 我需要知道如何選擇一個字符串並按下創建按鈕,如何在創建的面板中的另一個Windows窗體應用程序上顯示該名稱。 這是添加客戶的代碼

public partial class AddOrderForm : Form
{
    private SalesForm parent;

    public AddOrderForm(SalesForm s)
    {
        InitializeComponent();
        parent = s;

        Customer[] allCusts = parent.data.getAllCustomers();

        for (int i = 0; i < allCusts.Length; i++)
        {
            Text = allCusts[i].getName();
            newCustomerDropDown.Items.Add(Text);
            newCustomerDropDown.Text = Text;
            newCustomerDropDown.SelectedIndex = 0;
         }

現在,當我單擊“創建訂單”按鈕時,我希望以上信息在其他Windows窗體應用程序上標記。

        private void newOrderButton_Click(object sender, EventArgs e)
        {

            //get the info from the text boxes


            int Index = newCustomerDropDown.SelectedIndex;
            Customer newCustomer = parent.data.getCustomerAtIndex(Index);              


            //make a new order that holds that info
            Order brandSpankingNewOrder = new Order(newCustomer);
            //add the order to the data manager
            parent.data.addOrder(brandSpankingNewOrder);
            //tell daddy to reload his orders
            parent.loadOrders();
            //close myself
            this.Dispose();
        }

對於我來說,上下文不是很清楚,但是如果我做對了,您可以從SalesForm的實例中打開AddOrderForm的實例,然后單擊newOrderButton時,要使用AddOrderForm的數據更新SalesForm上的內容。

如果是這種情況,有很多方法可以獲取它,但是也許需要對代碼進行較少更改的一種方法就是這種方法(即使我不太喜歡它)。

將需要在SalesForm中修改的控件public或至少在internal (請查看控件屬性的“設計”部分中的“修飾符”屬性)。 這將允許您編寫如下內容(假設customerTxt是SalesForm中的TextBox):

parent.customerTxt.Text = newCustomerDropDown.SelectedItem.Text;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM