将所选列表框项从ownform传递到主窗体 - C# [英] Passing selected listbox item from ownedform to main form - C#

查看:57
本文介绍了将所选列表框项从ownform传递到主窗体 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主窗体中有一个datagridview。通过单击datagridview的特定列,它将打开子表单或ownedform(动态创建)。拥有的表单有一个列表框和一个OK按钮。当用户从列表框中选择一个项目并单击确定时,所选文本需要出现在datagridview单元格上。但是当我点击OK时我无法得到结果。我也没有收到任何错误。这是代码



注意:如果使用文本框而不是列表框,我可以传递值到datagridview。我只是不知道如何处理列表框来传递价值。



动态创建所有形式的代码&添加控件



Hi, I have a datagridview in the main form. By clicking on a particular column of datagridview, it will open the sub form or ownedform(dynamically created). The owned form have a listbox and an OK button. When The user selects an item from listbox and click OK then selected text needs to be appear on the datagridview cell. But when i click OK i couldn't get the result. I am not receiving any error neither.Here's the code

NOTE: If use Textbox instead of listbox, i am able to pass the value to datagridview. I just don't know the way to handle listbox to pass value.

CODE FOR DYNAMICALLY CREATING OWNED FORM & ADDING CONTROLS

private List





BUTTON点击事件处理程序 - 不工作(列表框)





BUTTON CLICK EVENT HANDLER - NOT WORKING (LISTBOX)

private void dbtn_Click_SIGTB(object sender, EventArgs e)
        {
            int rowidx = dataGridView3.CurrentCell.RowIndex;
            int colidx = dataGridView3.CurrentCell.ColumnIndex;
            foreach(ListBox lb in inputlistboxes)
            {
                string selected = lb.GetItemText(lb.SelectedItem);
                dataGridView2.Rows[rowidx].Cells[colidx].Value = selected;
            }
            
            foreach (Form frm in ownform)
               frm.Close();
        }





BUTTON EVAND HANDLER - WORKING(TEXTBOX)





BUTTON EVENT HANDLER - WORKING (TEXTBOX)

private void dbtn_Click_VARTB(object sender, EventArgs e)
        {
            int rowidx = dataGridView2.CurrentCell.RowIndex;
            int colidx = dataGridView2.CurrentCell.ColumnIndex;
            foreach (TextBox txt in inputTextBoxes)
                dataGridView2.Rows[rowidx].Cells[colidx].Value = txt.Text;
            foreach (Form frm in ownform)
                frm.Close();
        }





DATAGRIDVIEW CELL CONTENT CLICK





DATAGRIDVIEW CELL CONTENT CLICK

private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
           if (dataGridView3.CurrentCell.ColumnIndex.Equals(3))
           {
               ShowMyOwnedForm_SIGTB();
           }
       }





我尝试了什么: < br $>


private void dbtn_Click_SIGTB(object sender,EventArgs e)

{

int rowidx = dataGridView3.CurrentCell.RowIndex ;

int colidx = dataGridView3.CurrentCell.ColumnIndex;

foreach(输入列表框中的ListBox lb)

{

string selected = lb.GetItemText(lb.SelectedItem);

dataGridView2.Rows [rowidx] .Cells [colidx] .Value = selected;

}



foreach(表格frm in ownform)

frm.Close();

}



What I have tried:

private void dbtn_Click_SIGTB(object sender, EventArgs e)
{
int rowidx = dataGridView3.CurrentCell.RowIndex;
int colidx = dataGridView3.CurrentCell.ColumnIndex;
foreach(ListBox lb in inputlistboxes)
{
string selected = lb.GetItemText(lb.SelectedItem);
dataGridView2.Rows[rowidx].Cells[colidx].Value = selected;
}

foreach (Form frm in ownform)
frm.Close();
}

推荐答案

只需更新您的 dbtn_Click_SIGTB 函数,如下所示:

hi just update your dbtn_Click_SIGTB function as follows,
private void dbtn_Click_SIGTB(object sender, EventArgs e)
{
    int rowidx = dataGridView3.CurrentCell.RowIndex;
    int colidx = dataGridView3.CurrentCell.ColumnIndex;
    foreach(ListBox lb in inputlistboxes)
    {
        string selected = lb.Items[lb.SelectedIndex].ToString(); //lb.GetItemText(lb.SelectedItem);
        dataGridView2.Rows[rowidx].Cells[colidx].Value = selected;
    }

    foreach (Form frm in ownform)
       frm.Close();
}


这篇关于将所选列表框项从ownform传递到主窗体 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆