how to loop through some ASP.NET labels to set their attributes? - c#

How do I do this in a loop.
protected void ddlTool_SelectedIndexChanged(object sender, EventArgs e)
{
lblTool1.Visible = false;
txtTool1.Visible = false;
lblTool2.Visible = false;
txtTool2.Visible = false;
lblTool3.Visible = false;
txtTool3.Visible = false;
lblTool4.Visible = false;
txtTool4.Visible = false;
lblTool5.Visible = false;
if (ddlTool.SelectedValue == "1")
{
lblTool1.Visible = true;
txtTool1.Visible = true;
}
if (ddlTool.SelectedValue == "2")
{
lblTool1.Visible = true;
txtTool1.Visible = true;
lblTool2.Visible = true;
txtTool2.Visible = true;
}
if (ddlTool.SelectedValue == "3")
{
lblTool1.Visible = true;
txtTool1.Visible = true;
lblTool2.Visible = true;
txtTool2.Visible = true;
lblTool3.Visible = true;
txtTool3.Visible = true;
}
if (ddlTool.SelectedValue == "4")
{
lblTool1.Visible = true;
txtTool1.Visible = true;
lblTool2.Visible = true;
txtTool2.Visible = true;
lblTool3.Visible = true;
txtTool3.Visible = true;
lblTool4.Visible = true;
txtTool4.Visible = true;
}

Instead of having a separate variable for each textbox and label, have a collection of them - whether that's a List<T> or an array or whatever.
Then you can do:
// Potentially use int.TryParse here instead
int visibleLabels = int.Parse(ddlTool.SelectedValue);
for (int i = 0; i < labels.Count; i++)
{
labels[i].Visible = (i < visibleLabels);
textBoxes[i].Visible = (i < visibleLabels);
}
(Alternatively use two loops, one to set some Visible properties to true, and one to set some to false.)

You can access a control by its name using
container.Controls["nameofcontrol"]
So technically, you could use this to lookup your control
(Untested code)
for(int index = 1; index <= Convert.ToInt32(ddlTool.SelectedValue); index++)
{
this.Controls["lblTool" + index.ToString()].Visible = true;
this.Controls["txtTool" + index.ToString()].Visible = true;
}

Use a UserControl for each set of connected controls and then enable/disable the UserControl instead of all the component controls. This is classic, basic modularization of your user interface.
Note that this will still require a little "redundant" code because you're working with an unusual UI paradigm by enabling up-to the ddlTool's selected value of your control. E.g., create your user control that contains a single Label and TextBox. Call it LabeledTextBox or something similar. Then you'd create a collection of your labeled text boxes and enable them up to int.Parse(ddlTool.SelectedValue) - 1.

foreach (Control ctrl in this.Controls)
{
int index = (int)ctrl.Name.Substring(ctrl.Name.Length - 1);
int maxIndex = (int)ddlTool.SelectedValue;
ctrl.Visible = (index <= maxIndex);
}

Here is an example with no error checking, but should match your code functionality.
protected void ddlTool_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedValue = int.Parse(ddlTool.SelectedValue.ToString());
for (int i = 1; i <= 4; ++i)
{
bool state = i <= selectedValue;
this.Controls["lblTool" + i.ToString()].Visible = state;
this.Controls["txtTool" + i.ToString()].Visible = state;
}
}

Related

ObjectListView - Rows are formatted only after MouseOver on Items

I am in need of some assistance or advice/experience of someone else.
Here is what I'm struggling with:
In my project an objectlistview olvDifference is used to visualize items (Type Conflict) of a list. So far I was able to add the Columns needed and format them properly. But the format provided with
private void ConflictFormatRow(object sender,
BrightIdeasSoftware.FormatRowEventArgs e)
{
Conflict conflict = (Conflict)e.Model;
if (conflict == null) return;
if (conflict.resolution == ConflictResolution.None) e.Item.BackColor = conflictColor;
else if (conflict.resolution == ConflictResolution.UseMine) e.Item.BackColor = mineColor;
else if (conflict.resolution == ConflictResolution.UseTheirs) e.Item.BackColor = theirsColor;
else e.Item.BackColor = System.Drawing.Color.White;
if(e.Model == olvConflictList.SelectedObject)
{
BrightIdeasSoftware.RowBorderDecoration a = new BrightIdeasSoftware.RowBorderDecoration();
a.BorderPen.Color = Color.Black;
a.BorderPen.Width = 3;
a.CornerRounding = 0;
a.FillBrush = Brushes.Transparent;
e.Item.Decoration = a;
}
else
{
BrightIdeasSoftware.RowBorderDecoration b = new BrightIdeasSoftware.RowBorderDecoration();
b.BorderPen.Color = Color.Transparent;
b.BorderPen.Width = 0;
b.CornerRounding = 0;
b.FillBrush = Brushes.Transparent;
e.Item.Decoration = b;
}
}
In the constructor of the form (WFA), the AspectGetter is assigned and the objects to display are set.
Designer-Generated code equals the block below:
//
// olvConflictList
//
this.olvConflictList.AllColumns.Add(this.olvcConflictList);
this.olvConflictList.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.olvConflictList.CellEditUseWholeCell = false;
this.olvConflictList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.olvcConflictList});
this.olvConflictList.Cursor = System.Windows.Forms.Cursors.Default;
this.olvConflictList.FullRowSelect = true;
this.olvConflictList.Location = new System.Drawing.Point(780, 90);
this.olvConflictList.Name = "olvConflictList";
this.olvConflictList.Size = new System.Drawing.Size(286, 489);
this.olvConflictList.TabIndex = 18;
this.olvConflictList.UseCellFormatEvents = true;
this.olvConflictList.UseCompatibleStateImageBehavior = false;
this.olvConflictList.View = System.Windows.Forms.View.Details;
this.olvConflictList.FormatRow += new System.EventHandler<BrightIdeasSoftware.FormatRowEventArgs>(this.ConflictFormatRow);
this.olvConflictList.SelectedIndexChanged += new System.EventHandler(this.olvDifferenceGroups_SelectedIndexChanged);
//
// olvcConflictList
//
this.olvcConflictList.AspectName = "";
this.olvcConflictList.AutoCompleteEditor = false;
this.olvcConflictList.AutoCompleteEditorMode = System.Windows.Forms.AutoCompleteMode.None;
this.olvcConflictList.FillsFreeSpace = true;
this.olvcConflictList.Groupable = false;
this.olvcConflictList.HeaderCheckBoxUpdatesRowCheckBoxes = false;
this.olvcConflictList.Hideable = false;
this.olvcConflictList.IsEditable = false;
this.olvcConflictList.MinimumWidth = 50;
this.olvcConflictList.Searchable = false;
this.olvcConflictList.Sortable = false;
this.olvcConflictList.Text = "Conflicts";
this.olvcConflictList.UseFiltering = false;
this.olvcConflictList.Width = 283;
The only item in the olv has no BackGroundColor (White, default):
After starting the process, it is shown as one can see in the pic above, but I'd expect it to be initialized as in the pic below (after I hovered over it with my mouse the first time).
The only item in the olv has BackGroundColor conflictColor as assigned in the ConflictFormatRow:
Where do I need to improve my code? Any advices?
Well, I could figure it out myself.
I took a look at the source of OLV and what i found out so far:
In my case, the first row, here "Conflicts", was set to "Groupable=False", but the OLV itself was set to "ShowGroups=True".
FormatRow is fired in PostProcessOneRow, which is called according to source (Version: 2.9.1.0) in following logic:
protected virtual void PostProcessRows() {
// If this method is called during a BeginUpdate/EndUpdate pair, changes to the
// Items collection are cached. Getting the Count flushes that cache.
#pragma warning disable 168
// ReSharper disable once UnusedVariable
int count = this.Items.Count;
#pragma warning restore 168
int i = 0;
if (this.ShowGroups) {
foreach (ListViewGroup group in this.Groups) {
foreach (OLVListItem olvi in group.Items) {
this.PostProcessOneRow(olvi.Index, i, olvi);
i++;
}
}
} else {
foreach (OLVListItem olvi in this.Items) {
this.PostProcessOneRow(olvi.Index, i, olvi);
i++;
}
}
}
Since grouping failed, due to no groupable rows being present in OLV, the PostProcessOneRow in the if-body was not hit even once (foreach operated in "this.Groups" whichs count was 0).
Set "ShowGroups=False", works like a charm now.

Turn textboxes and buttons visible when the number of tracks it's selected in a combobox

I'm trying to turn textboxes and buttons visible when the number of tracks it's selected in a combobox.
For example: when I select 3, just 3 textboxes and the 3 respective buttons to select the tracks are enabled. How can I change this code that I've made to a simple foreach or a for?
if (numero_faixas == 1) {
txtFaixa1.Visible = true;
btnFaixa1.Visible = true;
} else if (numero_faixas == 2) {
txtFaixa1.Visible = true;
btnFaixa1.Visible = true;
txtFaixa2.Visible = true;
btnFaixa2.Visible = true;
} else if (numero_faixas == 3) {
txtFaixa1.Visible = true;
btnFaixa1.Visible = true;
txtFaixa2.Visible = true;
btnFaixa2.Visible = true;
txtFaixa3.Visible = true;
btnFaixa3.Visible = true;
}
You can reduce the lines of code by changing your conditions, so you don't have to reference the same control so many times:
if (numero_faixas > 0)
{
txtFaixa1.Visible = true;
btnFaixa1.Visible = true;
}
if (numero_faixas > 1)
{
txtFaixa2.Visible = true;
btnFaixa2.Visible = true;
}
if (numero_faixas > 2)
{
txtFaixa3.Visible = true;
btnFaixa3.Visible = true;
}
To use a foreach loop, you could cast the Controls collection to an IEnumerable<Control> and then, using System.Linq;, you can filter on controls of type TextBox and Button, where the control name contains "Faxia". Then, in the loop body, we can use int.TryParse to try to convert the last character of the control name to an int, and if that succeeds, then set the control to Visible if the control number is less than numero_faixas + 1:
foreach (Control control in Controls.Cast<Control>()
.Where(c => (c is Button || c is TextBox) && c.Name.Contains("Faixa")))
{
// Get the number associated with this control and compare it to numero_faixas
int controlNumber;
if (int.TryParse(control.Name.Substring(control.Name.Length - 1), out controlNumber) &&
controlNumber < numero_faixas + 1)
{
control.Visible = true;
}
}
Here's a proof of concept that you could respin using your business rules.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ShowHideButtons_47439046
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitOurThings();
}
private void InitOurThings()
{
//lets create a combo box with options to select
ComboBox combo = new ComboBox();
combo.Location = new Point(5, 5);//place it somewhere
//add selectable items
for (int i = 0; i < 10; i++)
{
combo.Items.Add(i);
}
combo.SelectedValueChanged += Combo_SelectedValueChanged;//the event which will handle the showing/hidding
Controls.Add(combo);//add the combo box to the form
//lets create some buttons and textboxes
int btnx = 5;
int btny = combo.Height + combo.Location.Y + 5;
for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.Location = new Point(btnx, btny);
btn.Name = i.ToString();
btn.Text = i.ToString();
Controls.Add(btn);
btny += btn.Height + 5;
TextBox txtbx = new TextBox();
txtbx.Location = new Point(btn.Location.X + btn.Width + 5, btn.Location.Y);
txtbx.Name = i.ToString();
txtbx.Text = i.ToString();
Controls.Add(txtbx);
}
}
private void Combo_SelectedValueChanged(object sender, EventArgs e)
{
int selectedValue = int.Parse(((ComboBox)sender).SelectedItem.ToString());
foreach (Control item in Controls)
{
//show/hide the controls based on their Name being Equal Or Smaller than the selectedItem
if (item is TextBox)
{
int itemNumber = int.Parse(item.Name);
item.Visible = itemNumber <= selectedValue ? true : false;
}
if (item is Button)
{
int itemNumber = int.Parse(item.Name);
item.Visible = itemNumber <= selectedValue ? true : false;
}
}
}
}
}

C# - bunifu animation doesn't work

While debugging the project the animation of the panel doesn't show... It only lags but doesn't show the transition.
if (sidePanel.Width == 260)
{
PanelAnimator.ShowSync(sidePanel);
sidePanel.Width = 0;
panelProduct.Height = 0;
panelOrdering.Height = 0;
panel4.AutoScroll = false;
}
else
{
PanelAnimator.ShowSync(sidePanel);
sidePanel.Width = 260;
panel4.AutoScroll = true;
}
This PanelAnimator.ShowSync(sidePanel); should be PanelAnimator.HideSync(sidePanel); since the sidepanel is already shown.
PS: sidepanel Visible property should be false before ShowSync(panel) V.V
Something like
if (sidePanel.Width == 260)
{
sidePanel.Width = 0;
panelProduct.Height = 0;
panelOrdering.Height = 0;
panel4.AutoScroll = false;
PanelAnimator.HideSync(sidePanel);
}
else
{
sidePanel.Width = 260;
panel4.AutoScroll = true;
PanelAnimator.ShowSync(sidePanel);
}

I can't disable checkbox?

I can't disable checkbox...
public void seat_reser_Load(object sender, EventArgs e)
{
string asd = "";
DataTable dt = ob.dataview("Select * from seat_res where bus_id='"+ bus_select.Id+"'and date='"+bus_select.date +"'");
foreach (DataRow d in dt.Rows)
{
asd += d[2].ToString(); // d[2] is seat column
asd+=",";
}
box[0] = CheckBox1;
box[1] = CheckBox2;
box[2] = checkBox3;
box[3] = checkBox4;
box[4] = checkBox5;
box[5] = checkBox6;
box[6] = checkBox7;
box[7] = checkBox8;
box[8] = checkBox9;
box[9] = checkBox10;
box[10] = checkBox11;
box[11] = checkBox12;
box[12] = checkBox13;
box[13] = checkBox14;
box[14] = checkBox15;
box[15] = checkBox16;
box[16] = checkBox17;
box[17] = checkBox18;
box[18] = checkBox19;
box[19] = checkBox20;
box[20] = checkBox21;
box[21] = checkBox22;
box[22] = checkBox23;
box[23] = checkBox24;
box[24] = checkBox25;
for (int h = 0; h < box.Length; h++)
{
box[h].Enabled = true;
}
string[] n = asd.Split(',');
for (int i = 0; i < n.Length; i++)
{
for (int j = 0; j < box.Length; j++)
{
if (n[i] == box[j].Text)
{
box[j].Enabled = false;
j++;
}
else if (!(box[j].Enabled == false))
{
box[j].Enabled = true;
}
}
}
}
Your code seems fine, did you want to set the checkbox's visibility or checked state to false, instead of disabling it? Did you want to make it invisible or simply unchecked?
In that case you might want to change your usage of Enabled to Visible or Checked, for example, like this:
box[h].Visible = false;
box[h].Checked = false;
What's the difference between Enabled, Checked and Visible?
Enabled - this will enable/disable the checkbox, you cannot check or uncheck the checkbox if it is disabled, however, the checkbox will still be visible on screen
Checked - this will check/uncheck the checkbox, it's identical to user actually clicking the checkbox
Visible - this will make the checkbox visible/invisible, so that user cannot see the control, nor can he click on it or interact with it in any way

DataGridview search: show only searchresult and hide other rows?

What should I add to my code to only show the results of my search?
Right now when I search the searchresult becomes selected (highlighted) and the others remain the same.
Been trying to hide the other rows but without any success (and only show the searchresult, alone). Any suggestions?
Im using a datagridview.
My code:
private void button3_Click_1(object sender, EventArgs e)
{
string search = textBox1.Text;
for (int i = 0; i < dgTest.Rows.Count; i++)
{
if (dgTest.Rows[i].Cells[0].Value.ToString() == search)
{
dgTest.Rows[i].Selected = true;
break;
}
else
{
dgTest.Rows[i].Selected = false;
}
}
}
If your DataGridView is not bound to a data source, then setting the row's Visible property to false will hide it:
for (int i = 0; i < dgTest.Rows.Count; i++)
{
var row = dgTest.Rows[i];
if (row.Cells[0].Value.ToString() == search)
{
row.Selected = true;
row.Visible = true;
}
else
{
row.Selected = false;
row.Visible = false;
}
}
(I removed the 'break' command, as even after you have found the matching row, you would want to continue and hide the other rows.)
If you're using DataBinding, though, it's not so easy, as shown in this page.
you can try this:
for (int i = 0; i < dgTest.Rows.Count; i++)
{
if (dgTest.Rows[i].Cells[0].Value.ToString() == "search")
{
dgTest.Rows[i].Selected = true;
dgTest.Rows[i].Visible = true;
}
else
{
dgTest.Rows[i].Visible = false;
dgTest.Rows[i].Selected = false;
}
}

Categories

Resources