samedi 8 décembre 2018

C#) How do you disable CheckBoxes in ListView?

enter image description here

If you type a word on the "txtProdBarcode" section, the lists will come at the bottom like the picture. The list sections name is "lvBase" which is aListView.

Then when you type a word on the "searchText" section, the checkboxes on the "lvBase" section shouldn't be clickable. I don't want to remove the checkboxes, but want to prevent them from being checked.

I've tried my best but can't get the answer.

private void txtProdBarcode_TextChanged(object sender, EventArgs e)
    {
        string searchText = txtProdBarcode.Text.ToUpper().Trim();

        if (searchText.Length > 0)
        {
            lvBase.CheckBoxes = false;
        }
        else
        {
            lvBase.CheckBoxes = true;
        }
    }

I heard that ItemCheck event will work, but don't know how to add it to my coding.

bool preventCheck = true;
private void listView1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if (preventCheck)  // for example: !string.IsNullOrEmpty(textBox1.Text)
        e.NewValue = e.CurrentValue;
}

Here is my full coding.

namespace COSMOS
{
public partial class ProdGc : Form
{
    public ProdGc()
    {
        InitializeComponent();
    }
// Base items view
    private void select_lvBase(string sCompanyId, string sGcNo, string sKeyNo, string sUserId
                                , out string sGcGbn, out bool bIsBaseData)
    {
        sGcGbn = "";
        bIsBaseData = false;

        lvBase.Items.Clear();

        Cursor.Current = Cursors.WaitCursor;
        try
        {
            CWS.Service rqService = new COSMOS.CWS.Service();
            rqService.Url = CommonVar.URL;

            DataSet ds = rqService.GET_GC_BASE( sCompanyId
                                              , sGcNo
                                              , sKeyNo
                                              , sUserId
                                              );
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        sGcGbn = dt.Rows[i]["GC_GBN"].ToString().Trim();
                    }
                    bIsBaseData = true;

                    // Insert data to grid
                    lvBase.Items.Add(new ListViewItem(new string[] {
                                                                     dt.Rows[i]["GC_NO"        ].ToString().Trim(), // 1
                                                                     dt.Rows[i]["CUSTOMER_NM"  ].ToString().Trim(), // 2
                                                                     dt.Rows[i]["GOODS_CD"     ].ToString().Trim(), // 3
                                                                     dt.Rows[i]["STEEL_KIND_CD"].ToString().Trim(), // 4
                                                                     dt.Rows[i]["SURFACE_CD"   ].ToString().Trim(), // 5
                                                                     dt.Rows[i]["TAPETUM_CD"   ].ToString().Trim(), // 6
                                                                     dt.Rows[i]["SIZE1"        ].ToString().Trim(), // 7
                                                                     dt.Rows[i]["SIZE2"        ].ToString().Trim(), // 8
                                                                     dt.Rows[i]["SIZE3"        ].ToString().Trim(), // 9
                                                                     dt.Rows[i]["SIZE4"        ].ToString().Trim(), // 10
                                                                     dt.Rows[i]["SIZE5"        ].ToString().Trim(), // 11
                                                                     dt.Rows[i]["SIZE6"        ].ToString().Trim(), // 12
                                                                     dt.Rows[i]["QTY"          ].ToString().Trim(), // 13
                                                                     dt.Rows[i]["WGT"          ].ToString().Trim(), // 14
                                                                     dt.Rows[i]["LEN"          ].ToString().Trim(), // 15
                                                                     dt.Rows[i]["SO_NO"        ].ToString().Trim(), // 16
                                                                     dt.Rows[i]["COMPANY_ID"   ].ToString().Trim(), // 17
                                                                     dt.Rows[i]["SITE_CD"      ].ToString().Trim(), // 18
                                                                     dt.Rows[i]["FACTORY_CD"   ].ToString().Trim(), // 19
                                                                     dt.Rows[i]["GC_GBN"       ].ToString().Trim()  // 20. 기타필드역할
                                                                   }));                       
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString(),
                            CommonVar.GetMessage("Error"),
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand,
                            MessageBoxDefaultButton.Button1);
        }
        finally
        {
            Cursor.Current = Cursors.Default;
            txtGcBarcode.Text = "";
            txtGcBarcode.Focus();
        }
    }


    // Clicking the scan button
    private void btnGcScan_Click(object sender, EventArgs e)
    {
        // Grid reset
        lvBase.Items.Clear();

        // Var reset
        bool bIsBaseData = false;

        string sGcNo = txtGcBarcode.Text.ToUpper().Trim(); // scanned number
        string sCompanyId = CommonVar.COMPANY_ID;
        string sGcGbn = "";
        string sUserId = CommonVar.USER_ID;

        // Confirm the entered number
        if (sGcNo == "")
        {
            MessageBox.Show(CommonVar.GetMessage("Data is invalid") + "\n - " + CommonVar.GetMessage("Prod No") + " : " + sGcNo,
                            CommonVar.GetMessage("Error"),
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Hand,
                            MessageBoxDefaultButton.Button1);

            txtGcBarcode.Focus();
            return;
        }

        // Base List View output         
        select_lvBase(  sCompanyId
                        , sGcNo
                        , "" // sKeyNo
                        , sUserId
                        , out sGcGbn
                        , out bIsBaseData
                        );
    }


    // How do you disable to click the CheckBoxes in ListView?
    private void txtProdBarcode_TextChanged(object sender, EventArgs e)
    {
        string searchText = txtProdBarcode.Text.ToUpper().Trim();

        if (searchText.Length > 0)
        {
            lvBase.CheckBoxes = false;
        }
        else
        {
            lvBase.CheckBoxes = true;
        }
    }
}
}

Can you guys help me to solve this problem?




Aucun commentaire:

Enregistrer un commentaire