How to check if/else statement for multiple textboxes and variables? - c#

I have a Windows form with 5 textboxes on it. In these textboxes the user can fill in 3 values (500,555 or 610). Every value needs to be multiplied with a constant. For every value there is another constant. Below is my code for one textbox (txtSectie1).
In stead of copying this code 5 times (for every textbox) I think there is a way to use a loop so I don't have to copy the code 4 times, but I can't figure it out. For each of the 5 text-boxes I want to store a value in a variable (dblGewichtPaneel1 to dblGewichtPaneel5). All the textboxes and variables have the same name, only the last character changes from 1 to 5.

Solution 1:
Put the entire code in to a method which accepts TextBox and Panel as input parameters.
Put all your textboxes and panels in a list and then run a loop on the list, repeatedly calling the method created in point 1.
Solution 2:
Run a loop 1 to 5, wrapping the above code.
Find the controls using
Textbox control = (TextBox) this.Controls["txtSectie" + i]; //this being Form or container control
if(control!=null)
{
//do work
}

Related

C#: How can I programmatically loop through all controls on a Windows Form

I'm in the process of creating a front-end Windows Forms application, which will have multiple uses. One of those uses will be to allow the end-user to manage the contents of a database table.
At the moment, my form contains a DataGridView object which uses a DataTable as its source. That datatable is populated from a stored procedure which selects every row from the table based on criteria specified by the end user. I want to put code into the DataGridView which will look at the row that's been selected by the user, and will populate a series of text boxes with the details from that row.
I want to avoid hard-coding a text box for each column in the DataGridView, since it's highly possible that in the future I might add columns to the underlying stored procedure. Therefore, my plan is that the first time the user selects a row, the application would dynamically create a text box for each column, size that text box appropriately (both height and width) and add the appropriate labels.
So far, the code I've written creates a brand-new DataTable, and adds columns to store the following information about each column in the DataGridView:
The actual name of the DataGridView column;
The name that I wish to give to the associated label;
The text that I wish to assign to the associated label;
The top and left positions of the associated label;
The name that I wish to give to the associated text box;
The value that I wish to display in the associated text box;
So far when the user initially clicks on a row, everything is working well: the text boxes get created in a vertical column, with the associated label to the left of each text box and the appropriate value displayed in each one. Each text box has its width dynamically set based on the contents of that text box so that everything can easily be seen.
The difficulty comes when the user changes from one row of the DataGridView to another. The code that runs to create each control looks like this:
if (pnlManageJobManagerOutcomes.Controls.Contains(txtCurrent))
{
pnlManageJobManagerOutcomes.Controls.Remove(txtCurrent);
}
if (!pnlManageJobManagerOutcomes.Controls.Contains(txtCurrent))
{
pnlManageJobManagerOutcomes.Controls.Add(txtCurrent);
}
When the code first runs, all seems to be fine. However, if it gets fired a second time (at which point it should remove and re-create the control) it doesn't seem to detect the prior existence of the control and simply creates a second copy of it in the form.
For information, the form I'm building has multiple uses which are dictated by a series of buttons. Therefore, I'm creating the controls for this particular use inside a Panel (pnlManageJobManagerOutcomes). I'm completely stumped as to why the code doesn't spot the existence of those controls during subsequent executions.
TIA
I've figured out a way to do it, which may seem to be clunky but which seems to work.
Since it doesn't appear that I can check to see whether the Panel I'm working with Contains the field name, I've written a method which will loop through the controls in that Panel and return a count of the number of instances of that control in the Panel's Controls array. If the method returns a 0, this means the Control doesn't exist in that Panel and I can go ahead and create it.

Cannot append the list in dropdown selectedindexchange to later use in buttonclick event

I am building a webform in c#. I want to generate a list where i append every field(string variable) value into the list within a foreach loop in dropdownevent. But, I want to use the list later it in buttonclick event. Foreach loop is correctly configured and Field has correct value assigned because I get all the field values when i print label.Text=field .
However, when i try to append my List and use the list in buttonclick event, it is not working. I get an error 'Index was out of range. Must be non-negative and less than the size of the collection.
public List Paramlist = new List();
On button click event:
testlabel.Text = Paramlist[0]
On dropdown selectedindexchange: (Within a foreach loop)
Paramlist.Add(field)
label.Text = field (This works and prints field to label)
The value of a variable does not persist between different postbacks, and each event (dropdown changed and button clicked) is a separate postback. If you want to keep track of the values in the list as they change across different postbacks, it would be best to store the list somewhere like a file or database when you're done adding to it in the dropdown event. Then in the button click, you can pull that data back out from wherever it's stored and use it.
If it were a single value, I would suggest writing it to a hidden field on the page, which would then maintain its value on the next postback, but for a list that's probably not the ideal approach.

ASP.NET C# How to re-establish EmptyMessage in Telerik RadComboBox in nested User Control?

I have the following scenario:
the main aspx page
a user control inside that aspx.page, let's call this parent_user_control
two RadComboBox, with only numbers on them.
The first one, RadComboBox1, for example, when the user chooses 2, it will dynamically generate 2 user controls, let's call them child_user_control_1
The second one, RadComboBox2, for example when the user chooses 1, it will dynamically generate 1 user control, let's call it child_user_control_2.
child_user_control_2, has another RadComboBox, called RadComboBox_2_1, again with numbers only, that dynamically generates another user control, called grandchild_user_control.
grandchild_user_control has a RadComboBox called RadComboBox_grandchild, with an EmptyMessage="Please select", and only numbers, but the numbers must match RadComboBox1, as in, when 2 was chosen, only options 1 through 2 should appear in this RadComboBox_grandchild. If the value from RadComboBox1 changes to 5, then the range of values in RadComboBox_grandchild should change to 1 through 5. Same if it decreases from 2 to 1, only the value 1 should be available.
I properly handled the recreation of dynamic user controls, so all values persist, and the values in RadComboBox_grandchild get properly updated.
Here is the problem:
When I change the value to a smaller number, say from 3 to 1, and I have, for example, the value 2 already chosen in RadComboBox_grandchild, the list gets updated, and in the code_behind the values change correctly (as in selectedIndex is -1, selectedItem is null, etc.) BUT, I do not see "Please select", I still see the value "2", even though it is no longer on the list.
How can I avoid this? How can I make it show the EmptyMessage "Please select" again? I tried the ClearSelection method, but it didn't do anything because it already is without a selection. It seems to me that the mechanism used to persist the values from the previous postback is forcefully persisting a value that no longer exists.
It seems to me that the mechanism used to persist the values from the previous postback is forcefully persisting a value that no longer exists.
It is; you can turn EnableViewState property for control on or off.
I suppose you need to clear properties Text and/or SelectedValue of your Telerik.Web.UI.RadComboBox instance that persists outdated value.

Reference a button as a variable for use in a loop

Hi Sorry if the answer has been given already, but I have searched high and low for this and have drawn a blank.
I have a grid of buttons that disappear when clicked. This is good. The buttons are named tile1, tile2, tile3 etc... their numbers are contiguous.
Their click_handlers currently each manually 'disappear' the sending buttons like this...:
tile1.visible = false
I am looking for a simple way to reference the tile names and numbers so I can
disappear the buttons in groups by referencing their names and numbers (ie disappear the 1st 3 in one command (or loop) followed by the next 2 together followed by the next 6 together... you get the picture).
Create a reset loop that resets the buttons' visibility property from within the code without having to manually type every button name.
So in an ideal world it might look something like...:
for (i=1; i<=MAX_TILE_NUMBER; ++i)
{
string currentTile = "tile" + i
currentTile.visible = false
}
I can feel it in my bones that there is an easy way to concatenate the string portion of the button name with the index "i" in my for loop and use this to reference the button controls one by one or in groups but I have not yet found it. please save me from imminent hair-rending insanity.
Many thanks.
Thanks
JB
Just put your buttons to a collection, a dictionary should fit best for you.
var buttons = new Dictionary<string, Button> { { "tile1", tile1 }, ... };
buttons.Add("tile2", tile2);
etc.
and then operate with this dictionary. For example, make the first three buttons invisible
var firstThree = buttons.Take(3);
foreach (var b in firstThree)
b.Value.Visible = false;
You can pick buttons however you wish by forming LINQ queries on this dictionary.
There is function to find control by name
see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.find%28VS.80%29.aspx

Variable number of textboxes - ASP C#

I have a simple dropdown list, currently using asp:ListItem to populate tho this will be changed to a db at some later point.
Depending on the users choice, I then want to present them with between 2 and 5 Lables and Textboxes so that they can enter values then run an algorythm.
The Lables and Textboxes, let call em items, will always have 1 and 2 shown, however 3-5 can be present in any combination but preferably always in order so 5 never comes before 3 and 4 etc.
I also dont want there to be any gaps if say 3 and 4 are missing, so making them invisible isnt an option either.
Is there some form of table or listbox option than can allow me to simply do the above ? Or do I have to change the text etc for each "line" as required ?
I basically want the following (example with Lines/items 3 and 4 not required) -
Item1 VALUE ______
Item2 VALUE ______
Item5 VALUE ______
Many thanks, George.
The best thing is that according to user choice make Item3 as Item5 and hide all other controls
:)
Use jQuery or basic Javascript to set the display property as none for the items that you don't want to be displayed. This way there wouldn't be any gaps on the UI for the hidden items.

Categories

Resources