Changing seelctionmode for a listbox
I have a form that is called during the processing of a report. However, several different reports use this form, and on certain of the reports I want the listbox to only allow a single item selected (selectionmode = One) and when other reports use this form i want to have the ability to choose multiple items from this same listbox (selectionmode = multiSimple). So obviously I could create a duplicate form and just change the selectionmode, but i wanted tot ry and update the selection mode for the listbox based on the report using the form. However, I'm not sure where to insert the code (in the class or the designer) and really want to be careful if i have to change the designer as I know messing around in there can wreak havoc. I've attached the Designer code and the class Code below. The listbox in question is called "lstMode". I've read about the syntax of setting it via something like "this.lstMode.SelectionMode = SelectionMode.MultiSimple;";" but when i try to insert this into the class I get an error saying "selectionMode doesn't exist in the current context" in the Designer (which it doesn't and I'm worried about screwing that the Designer). How/where in my existing code can i change the property of selectionMode properly? I'll deal with the logic of when I want to set it to MultiSimple or MultieExtended afterwards, as that will be the easy part. But I can't figure out what to put in the class (and where) and what to put in the designer (if anything) and I'm annoyed, because I'm sure it's simple. Also, I'm pretty new to C# and this code was written by someone else and i'm not ready to make major changes, so if this code is impossible to work through, le tme know. Making a copy of the form and just changing the mode is always a rather simple (and annoying) soltuion. Class Code (Designer Code is after) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; //using System.Data.Odbc; using System.Data.SqlClient; namespace AmortClient { public partial class frmDlgRollforward : Form { public frmDlgRollforward() { InitializeComponent(); // Data objects are unmanaged code. // Declare them above the try{} block and always dispose of them in finally{}. SqlCommand cmd = null; SqlDataReader dr = null; try { this.clbCountries.DisplayMember = "Label"; this.clbCountries.ValueMember = "Value"; this.clbExCountries.DisplayMember = "Label"; this.clbExCountries.ValueMember = "Value"; this.clbNodes.DisplayMember = "Label"; this.clbNodes.ValueMember = "Value"; this.lstMode.DisplayMember = "Label"; this.lstMode.ValueMember = "Value"; this.cboFromDate.DisplayMember = "Label"; this.cboFromDate.ValueMember = "Value"; this.cboToDate.DisplayMember = "Label"; this.cboToDate.ValueMember = "Value"; this.lstMode.SelectionMode = SelectionMode.MultiSimple; // Country parameters cmd = util.SqlConn.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "proc_parms_countries"; dr = cmd.ExecuteReader(); while (dr.Read()) if (dr["country key"].ToString() != "0" && dr["country key"].ToString() != "1") { this.clbCountries.Items.Add(new ListItem(dr["country name"].ToString(), dr["country key"].ToString()), false); this.clbExCountries.Items.Add(new ListItem(dr["country name"].ToString(), dr["country key"].ToString()), false); } dr.Close(); // Node parameter cmd.CommandText = "proc_parms_nodes"; dr = cmd.ExecuteReader(); while (dr.Read()) if (dr["node_key"].ToString() == "1" || dr["node_key"].ToString() == "2") this.clbNodes.Items.Add(new ListItem(dr["node_name"].ToString(), dr["node_key"].ToString()), true); else this.clbNodes.Items.Add(new ListItem(dr["node_name"].ToString(), dr["node_key"].ToString()), false); dr.Close(); // To/From date parameters - must be the last day of the month cmd.CommandText = "proc_parms_eom"; dr = cmd.ExecuteReader(); while (dr.Read()) { this.cboFromDate.Items.Add(new ListItem(dr["label"].ToString(), dr["val"].ToString())); this.cboToDate.Items.Add(new ListItem(dr["label"].ToString(), dr["val"].ToString())); // set the defaults if (dr["is_from_default"].ToString() == "1") this.cboFromDate.SelectedIndex = this.cboFromDate.Items.Count - 1; if (dr["is_to_default"].ToString() == "1") this.cboToDate.SelectedIndex = this.cboToDate.Items.Count - 1; } dr.Close(); // Amortization Mode cmd.CommandText = "proc_parms_amort_modes"; dr = cmd.ExecuteReader(); while (dr.Read()) { this.lstMode.Items.Add(new ListItem(dr["amort_mode_description"].ToString(), dr["amort_mode_key"].ToString())); // set the default if (dr["amort_mode_key"].ToString() == "1") this.lstMode.SelectedIndex = this.lstMode.Items.Count - 1; } dr.Close(); } catch (Exception ex) { util.LogError(ex); MessageBox.Show(ex.Message); } finally { if (dr != null) dr.Dispose(); if (cmd != null) cmd.Dispose(); } } private void btnOk_Click(object sender, EventArgs e) { if (SelectionsAreValid()) this.wb1.Navigate(ReportUrl(), true); } public string ReportName { set { this.txtReportName.Text = value; } get { return this.txtReportName.Text; } } private bool SelectionsAreValid() { try { if (this.chkCountriesAll.Checked == false && this.clbCountries.CheckedItems.Count == 0) { MessageBox.Show("You must choose at least one country to include.", "Invalid criteria", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } else if (this.clbNodes.CheckedItems.Count == 0) { MessageBox.Show("You must choose at least one node.", "Invalid criteria", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } else if (int.Parse(((ListItem)this.cboFromDate.SelectedItem).Value) >= int.Parse(((ListItem)this.cboToDate.SelectedItem).Value)) { MessageBox.Show("'To' date must be later than 'From' date.", "Invalid criteria", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return false; } else { return true; } } catch (Exception ex) { util.LogError(ex); } return false; } private string ReportUrl() { string retval = util.ReportServerUrl + util.ReportFolder + this.ReportName; try { retval += "&parmAmortModeKey=" + ((ListItem)this.lstMode.SelectedItem).Value; if (this.chkCountriesAll.Checked) retval += "&countryKeys=0"; else foreach (ListItem itm in this.clbCountries.CheckedItems) retval += "&countryKeys=" + itm.Value; if (this.chkExCountriesNone.Checked) retval += "&exCountryKeys=0"; else foreach (ListItem itm in this.clbExCountries.CheckedItems) retval += "&exCountryKeys=" + itm.Value; foreach (ListItem itm in this.clbNodes.CheckedItems) retval += "&nodeKeys=" + itm.Value.Replace("&", "%26"); retval += "&parmBeginDateKey=" + ((ListItem)cboFromDate.SelectedItem).Value; retval += "&parmEndDateKey=" + ((ListItem)cboToDate.SelectedItem).Value; retval += "&guid=" + Guid.NewGuid().ToString(); retval += "&rs:Command=Render&rs:Format=PDF"; } catch (Exception ex) { util.LogError(ex); } return retval; } private void CheckAll(CheckedListBox clb, bool check) { for (int ii = 0; ii < clb.Items.Count; ii++) clb.SetItemChecked(ii, check); } private void chkCountriesAll_CheckedChanged(object sender, EventArgs e) { CheckAll(this.clbCountries, ((CheckBox)sender).Checked); this.clbCountries.Enabled = !(((CheckBox)sender).CheckState == CheckState.Checked); } private void chkNodesAll_CheckedChanged(object sender, EventArgs e) { CheckAll(this.clbNodes, ((CheckBox)sender).Checked); } private void chkExCountriesNone_CheckedChanged(object sender, EventArgs e) { if (((CheckBox)sender).CheckState == CheckState.Checked) CheckAll(this.clbExCountries, false); this.clbExCountries.Enabled = !(((CheckBox)sender).CheckState == CheckState.Checked); } private void btnCancel_Click(object sender, EventArgs e) { this.wb1.Dispose(); this.Close(); } } } Designer Code namespace AmortClient { partial class frmDlgRollforward { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmDlgRollforward)); this.clbCountries = new System.Windows.Forms.CheckedListBox(); this.btnOk = new System.Windows.Forms.Button(); this.wb1 = new System.Windows.Forms.WebBrowser(); this.label1 = new System.Windows.Forms.Label(); this.chkCountriesAll = new System.Windows.Forms.CheckBox(); this.label2 = new System.Windows.Forms.Label(); this.clbExCountries = new System.Windows.Forms.CheckedListBox(); this.chkExCountriesNone = new System.Windows.Forms.CheckBox(); this.label3 = new System.Windows.Forms.Label(); this.chkNodesAll = new System.Windows.Forms.CheckBox(); this.clbNodes = new System.Windows.Forms.CheckedListBox(); this.lstMode = new System.Windows.Forms.ListBox(); this.label4 = new System.Windows.Forms.Label(); this.btnCancel = new System.Windows.Forms.Button(); this.label5 = new System.Windows.Forms.Label(); this.cboFromDate = new System.Windows.Forms.ComboBox(); this.cboToDate = new System.Windows.Forms.ComboBox(); this.label6 = new System.Windows.Forms.Label(); this.txtReportName = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // clbCountries // this.clbCountries.CheckOnClick = true; this.clbCountries.FormattingEnabled = true; this.clbCountries.Location = new System.Drawing.Point(4, 23); this.clbCountries.Name = "clbCountries"; this.clbCountries.Size = new System.Drawing.Size(126, 274); this.clbCountries.TabIndex = 1; // // btnOk // this.btnOk.Location = new System.Drawing.Point(343, 270); this.btnOk.Name = "btnOk"; this.btnOk.Size = new System.Drawing.Size(78, 27); this.btnOk.TabIndex = 10; this.btnOk.Text = "&OK"; this.btnOk.UseVisualStyleBackColor = true; this.btnOk.Click += new System.EventHandler(this.btnOk_Click); // // wb1 // this.wb1.Location = new System.Drawing.Point(287, 202); this.wb1.MinimumSize = new System.Drawing.Size(20, 20); this.wb1.Name = "wb1"; this.wb1.ScriptErrorsSuppressed = true; this.wb1.ScrollBarsEnabled = false; this.wb1.Size = new System.Drawing.Size(40, 20); this.wb1.TabIndex = 2; this.wb1.TabStop = false; this.wb1.Visible = false; // // label1 // this.label1.AutoSize = true; this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label1.Location = new System.Drawing.Point(4, 7); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(60, 13); this.label1.TabIndex = 4; this.label1.Text = "Countries"; // // chkCountriesAll // this.chkCountriesAll.AutoSize = true; this.chkCountriesAll.BackColor = System.Drawing.SystemColors.Control; this.chkCountriesAll.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.chkCountriesAll.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.chkCountriesAll.Location = new System.Drawing.Point(64, 5); this.chkCountriesAll.Name = "chkCountriesAll"; this.chkCountriesAll.Size = new System.Drawing.Size(37, 17); this.chkCountriesAll.TabIndex = 0; this.chkCountriesAll.Text = "&All"; this.chkCountriesAll.UseVisualStyleBackColor = false; this.chkCountriesAll.CheckedChanged += new System.EventHandler(this.chkCountriesAll_CheckedChanged); // // label2 // this.label2.AutoSize = true; this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label2.Location = new System.Drawing.Point(135, 7); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(52, 13); this.label2.TabIndex = 7; this.label2.Text = "Exclude"; // // clbExCountries // this.clbExCountries.CheckOnClick = true; this.clbExCountries.Enabled = false; this.clbExCountries.FormattingEnabled = true; this.clbExCountries.Location = new System.Drawing.Point(135, 23); this.clbExCountries.Name = "clbExCountries"; this.clbExCountries.Size = new System.Drawing.Size(126, 274); this.clbExCountries.TabIndex = 4; // // chkExCountriesNone // this.chkExCountriesNone.AutoSize = true; this.chkExCountriesNone.BackColor = System.Drawing.SystemColors.Control; this.chkExCountriesNone.Checked = true; this.chkExCountriesNone.CheckState = System.Windows.Forms.CheckState.Checked; this.chkExCountriesNone.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.chkExCountriesNone.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.chkExCountriesNone.Location = new System.Drawing.Point(187, 5); this.chkExCountriesNone.Name = "chkExCountriesNone"; this.chkExCountriesNone.Size = new System.Drawing.Size(53, 17); this.chkExCountriesNone.TabIndex = 2; this.chkExCountriesNone.Text = "&None"; this.chkExCountriesNone.UseVisualStyleBackColor = false; this.chkExCountriesNone.CheckedChanged += new System.EventHandler(this.chkExCountriesNone_CheckedChanged); // // label3 // this.label3.AutoSize = true; this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label3.Location = new System.Drawing.Point(266, 7); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(43, 13); this.label3.TabIndex = 10; this.label3.Text = "Nodes"; // // chkNodesAll // this.chkNodesAll.AutoSize = true; this.chkNodesAll.BackColor = System.Drawing.SystemColors.Control; this.chkNodesAll.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.chkNodesAll.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.chkNodesAll.Location = new System.Drawing.Point(309, 5); this.chkNodesAll.Name = "chkNodesAll"; this.chkNodesAll.Size = new System.Drawing.Size(37, 17); this.chkNodesAll.TabIndex = 5; this.chkNodesAll.Text = "A&ll"; this.chkNodesAll.UseVisualStyleBackColor = false; this.chkNodesAll.CheckedChanged += new System.EventHandler(this.chkNodesAll_CheckedChanged); // // clbNodes // this.clbNodes.CheckOnClick = true; this.clbNodes.FormattingEnabled = true; this.clbNodes.Location = new System.Drawing.Point(266, 23); this.clbNodes.Name = "clbNodes"; this.clbNodes.Size = new System.Drawing.Size(85, 94); this.clbNodes.TabIndex = 6; // // lstMode // this.lstMode.FormattingEnabled = true; this.lstMode.Location = new System.Drawing.Point(356, 23); this.lstMode.Name = "lstMode"; this.lstMode.Size = new System.Drawing.Size(149, 95); this.lstMode.TabIndex = 7; // // label4 // this.label4.AutoSize = true; this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label4.Location = new System.Drawing.Point(357, 7); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(38, 13); this.label4.TabIndex = 17; this.label4.Text = "Mode"; // // btnCancel // this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; this.btnCancel.Location = new System.Drawing.Point(427, 270); this.btnCancel.Name = "btnCancel"; this.btnCancel.Size = new System.Drawing.Size(78, 27); this.btnCancel.TabIndex = 11; this.btnCancel.Text = "&Cancel"; this.btnCancel.UseVisualStyleBackColor = true; this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); // // label5 // this.label5.AutoSize = true; this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label5.Location = new System.Drawing.Point(300, 131); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(34, 13); this.label5.TabIndex = 19; this.label5.Text = "From"; // // cboFromDate // this.cboFromDate.FormattingEnabled = true; this.cboFromDate.Location = new System.Drawing.Point(300, 147); this.cboFromDate.Name = "cboFromDate"; this.cboFromDate.Size = new System.Drawing.Size(85, 21); this.cboFromDate.TabIndex = 8; // // cboToDate // this.cboToDate.FormattingEnabled = true; this.cboToDate.Location = new System.Drawing.Point(390, 147); this.cboToDate.Name = "cboToDate"; this.cboToDate.Size = new System.Drawing.Size(85, 21); this.cboToDate.TabIndex = 9; // // label6 // this.label6.AutoSize = true; this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.label6.Location = new System.Drawing.Point(391, 131); this.label6.Name = "label6"; this.label6.Size = new System.Drawing.Size(22, 13); this.label6.TabIndex = 22; this.label6.Text = "To"; // // txtReportName // this.txtReportName.BackColor = System.Drawing.SystemColors.GradientActiveCaption; this.txtReportName.Location = new System.Drawing.Point(319, 228); this.txtReportName.Name = "txtReportName"; this.txtReportName.Size = new System.Drawing.Size(135, 20); this.txtReportName.TabIndex = 23; this.txtReportName.Text = "txtReportName"; this.txtReportName.Visible = false; // // frmDlgRollforward // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.CancelButton = this.btnCancel; this.ClientSize = new System.Drawing.Size(509, 302); this.ControlBox = false; this.Controls.Add(this.txtReportName); this.Controls.Add(this.label6); this.Controls.Add(this.cboToDate); this.Controls.Add(this.cboFromDate); this.Controls.Add(this.label5); this.Controls.Add(this.btnCancel); this.Controls.Add(this.label4); this.Controls.Add(this.lstMode); this.Controls.Add(this.clbNodes); this.Controls.Add(this.label3); this.Controls.Add(this.chkNodesAll); this.Controls.Add(this.label2); this.Controls.Add(this.clbExCountries); this.Controls.Add(this.chkExCountriesNone); this.Controls.Add(this.label1); this.Controls.Add(this.wb1); this.Controls.Add(this.btnOk); this.Controls.Add(this.clbCountries); this.Controls.Add(this.chkCountriesAll); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "frmDlgRollforward"; this.ShowIcon = false; this.StartPosition = System.Windows.Forms.FormStartPosition.Manual; this.Text = "DSC Rollforward Selection Criteria"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.CheckedListBox clbCountries; private System.Windows.Forms.Button btnOk; private System.Windows.Forms.WebBrowser wb1; private System.Windows.Forms.Label label1; private System.Windows.Forms.CheckBox chkCountriesAll; private System.Windows.Forms.Label label2; private System.Windows.Forms.CheckedListBox clbExCountries; private System.Windows.Forms.CheckBox chkExCountriesNone; private System.Windows.Forms.Label label3; private System.Windows.Forms.CheckBox chkNodesAll; private System.Windows.Forms.CheckedListBox clbNodes; private System.Windows.Forms.ListBox lstMode; private System.Windows.Forms.Label label4; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.Label label5; private System.Windows.Forms.ComboBox cboFromDate; private System.Windows.Forms.ComboBox cboToDate; private System.Windows.Forms.Label label6; private System.Windows.Forms.TextBox txtReportName; } }
June 27th, 2012 3:52pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics