Loop
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Loop

  1. #1
    Join Date
    Sep 2012
    Location
    England
    Posts
    2

    Loop

    Hi, could someone put this in a loop, where you can define how many labels and combo boxes are to be made not visible.

    cb_recipient1.Visible = true;
    lbl_Recipient1.Visible = true;
    cb_recipient2.Visible = false;
    lbl_Recipient2.Visible = false;
    cb_recipient3.Visible = false;
    lbl_Recipient3.Visible = false;
    cb_recipient4.Visible = false;
    lbl_Recipient4.Visible = false;
    cb_recipient5.Visible = false;
    lbl_Recipient5.Visible = false;
    cb_recipient6.Visible = false;
    lbl_Recipient6.Visible = false;
    cb_recipient7.Visible = false;
    lbl_Recipient7.Visible = false;
    cb_recipient8.Visible = false;
    lbl_Recipient8.Visible = false;

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,202

    Re: Loop

    To do this, all you have to do is to dynamically create the checkbox and label controls and add them to the form.

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,005

    Re: Loop

    Alternatively, you can throw them into an array:
    Code:
    Label[] lblRecipArray = { lbl_Recipient1, lblRecipient2,  /*etc*/ };
    ComboBox[] cbRecipArray = { cb_recipient1, cb_recipient2, /*etc*/ };
    public static void setOneTrue(int indexToBeTrue)
    {
        for(int i = 0; i < lblRecipArray.Length; i++)
        {
            lblRecipArray[i].Visible = (i == indexToBeTrue);
            cbRecipArray[i].Visible = (i == indexToBeTrue);
        }
    }
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Sep 2012
    Location
    England
    Posts
    2

    Re: Loop

    Quote Originally Posted by BioPhysEngr View Post
    Alternatively, you can throw them into an array:
    Code:
    Label[] lblRecipArray = { lbl_Recipient1, lblRecipient2,  /*etc*/ };
    ComboBox[] cbRecipArray = { cb_recipient1, cb_recipient2, /*etc*/ };
    public static void setOneTrue(int indexToBeTrue)
    {
        for(int i = 0; i < lblRecipArray.Length; i++)
        {
            lblRecipArray[i].Visible = (i == indexToBeTrue);
            cbRecipArray[i].Visible = (i == indexToBeTrue);
        }
    }
    Thank you, this worked great. I was able to use a similar array later on in my code, manipulated from yours:

    Code:
                    int numberCB = 0;
    
                    if (cb_recipient8.Visible) { numberCB = 7; } // if 8 is visible and 8 is null then 7 else 8.
                    else if (cb_recipient7.Visible) { numberCB = 6; }
                    else if (cb_recipient6.Visible) { numberCB = 5; }
                    else if (cb_recipient5.Visible) { numberCB = 4; }
                    else if (cb_recipient4.Visible) { numberCB = 3; }
                    else if (cb_recipient3.Visible) { numberCB = 2; }
                    else if (cb_recipient2.Visible) { numberCB = 1; } //cb 2 is 1, becuase when 
                    else if (cb_recipient1.Visible) { numberCB = 0; }
    
                    ComboBox[] cbRecipArray = { cb_recipient1, cb_recipient2, cb_recipient3, cb_recipient4, cb_recipient5, cb_recipient6, cb_recipient7, cb_recipient8 };
    
                    for (int i = 0; i < numberCB; i++)
                    {
                        string strTransNo = transmittalNo;
                        string strCCID = cbRecipArray[i].SelectedValue.ToString();
                        string strSendType = "m";
    
                        try
                        {
                            con.Open();
                            SqlCommand cmd4 = con.CreateCommand();
    
    
                            cmd4.CommandText = "INSERT INTO tblTransContact (transmittleNo, clientContactID, sendType) values (@transNo, @clientContactID, @sendType)";
    
                            cmd4.Parameters.AddWithValue("transNo", strTransNo);
                            cmd4.Parameters.AddWithValue("clientContactID", strCCID);
                            cmd4.Parameters.AddWithValue("sendType", strSendType);
    
                            cmd4.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("An Error Occured - " + ex.Message, "Transmittals - Error 240", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
    
                        finally
                        {
                            if (con != null)
                            {
                                con.Close();
                            }
                        }
                    }
    Does this look ok? Also, would it be possible to use an array to shorten the following code?

    Code:
    string strClientID = cb_client.SelectedValue.ToString();
                        SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM tblClientContact WHERE clientID= @clientID", con);
    
                        da.SelectCommand.Parameters.AddWithValue("@clientID", strClientID);
                        DataTable dt = new DataTable();
                        dt.Rows.Clear();
                        da.Fill(dt);
    
                        string strCb1 = cb_recipient1.SelectedValue.ToString();
                        string strCb2 = cb_recipient2.SelectedValue.ToString();
                        string strCb3 = cb_recipient3.SelectedValue.ToString();
                        string strCb4 = cb_recipient4.SelectedValue.ToString();
    
                        DataRow[] drr = dt.Select("clientContactID=' " + strCb1 + " ' ");
                        for (int i = 0; i < drr.Length; i++)
                            drr[i].Delete();
                        dt.AcceptChanges();
    
                        DataRow[] drr2 = dt.Select("clientContactID=' " + strCb2 + " ' ");
                        for (int i = 0; i < drr2.Length; i++)
                            drr2[i].Delete();
                        dt.AcceptChanges();
    
                        DataRow[] drr3 = dt.Select("clientContactID=' " + strCb3 + " ' ");
                        for (int i = 0; i < drr3.Length; i++)
                            drr3[i].Delete();
                        dt.AcceptChanges();
    
                        DataRow[] drr4 = dt.Select("clientContactID=' " + strCb4 + " ' ");
                        for (int i = 0; i < drr4.Length; i++)
                            drr4[i].Delete();
                        dt.AcceptChanges();
    Thank you very much for your time and help, it is much appreciated.

  5. #5
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,005

    Re: Loop

    Looks OK to me (though I didn't really read through the SQL part in much detail). Yes, you should definitely be able to shorten the second one. Mostly like (you can check for bugs, just doing it off the top of my head, based on your code):

    Code:
    ComboBox[] cb_recipients = { cb_recipient1, /* etc /* };
    for(int k = 0; k < cb_recipients.Length; k++)
    {
        strCb = cb_recipients[k].SelectedValue.ToString();
        DataRow[] drr = dt.Select("clientContactID=' " + strCb + " ' ");
        for (int i = 0; i < drr.Length; i++)
            drr[i].Delete();
        dt.AcceptChanges();
    }
    You can also get rid of:

    Code:
    int numberCB = 0;
    if (cb_recipient8.Visible) { numberCB = 7; } // if 8 is visible and 8 is null then 7 else 8.
    else if (cb_recipient7.Visible) { numberCB = 6; }
    else if (cb_recipient6.Visible) { numberCB = 5; }
    else if (cb_recipient5.Visible) { numberCB = 4; }
    else if (cb_recipient4.Visible) { numberCB = 3; }
    else if (cb_recipient3.Visible) { numberCB = 2; }
    else if (cb_recipient2.Visible) { numberCB = 1; } //cb 2 is 1, becuase when 
    else if (cb_recipient1.Visible) { numberCB = 0; }
    by looping. Namely:

    Code:
    numberCB = 0;
    while( cb_recipients[numberCB].Visible )
    {
    	numberCB++;
    }
    numberCB--;	//This is for compatibility with what you wrote; do you have an off-by-one-error?
                            //Or do you really want that?
    Though, I think it would be better to not infer the value of numberCB based on visibility of the ComboBoxes. It'd be better (from a style perspective) to keep track of how many are visible as you set/unset them. In general, I try to avoid inferring anything from the GUI I don't absolutely have to (for example, reading user input is OK). It will wok just fine, as written, but GUIs have a tendency to change and it makes your code a little more brittle.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center