Code Behind Manipulations/Giving custom names to Variables/Combining names - c#

So my problem goes, like, I want to have dynamic seted names, something like this:
for(int i=0;i<5;i++)
{
CheckBox i = new CheckBox();
}
2 bad it does not work ^^
Is it even possible to get values like that from variables, and transform them into variables names? Or should I just set thoes things staticly?
Ow yeah and afeter setting something like that I would like to do something like this to set the content, bascily it will go in the same loop
fe. i want to have 5 buttons and i have made 5 strings in the AppResources, their names goes like : a0,a1,a2,a3,a4;
So now I'm creating the buttons and using the content.
for(int i=0;i<5;i++)
{
CheckBox i = new CheckBox();
i.Content = Resources.AppResources.a+"i";
}
So, anyone can help?^^

You need to use an array:
ArrayList<CheckBox> checkboxes = new ArrayList<CheckBox>();
for(int i=0;i<5;i++)
{
checkboxes.Add(new CheckBox());
}

Related

C# giving radio buttons and panels a value

I am very new to C# and am trying to make a form program with a lot of radio buttons, i was hoping to have something to the effect of
radioButton1.value = 1;
radioButton2.value = 2;
panel1.Value = Selected radiobuttons value;
or just something like that without having to write out so many if/switch statements.
You can use LINQ to get the selected button.
Assume that your radioButtons are in form, so the variable this means the class form.
var selectedButton = this.Controls.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked);
Console.WriteLine(selectedButton.Text);

C# Updating multiple text boxes with similar names

I have multiple textbox names such as R1TotalCost, R2TotalCost, R3TotalCost, all the way up to R25TotalCost. Is there anyway to edit the text values, and or text colours to them all using a code simular to this
for (i=1; i <=25, i++) {
string TextBoxName ="R" + i + "TotalCost";
TextBoxName.text = "£25";
TextBoxName.Foreground = Brushes.Green;
}
I think the best way to go about this since you seem to want to update them all at once, would be to create an Array or List that contains all of the TextBox elements. Then your can go through them all like below!
foreach (TextBox tb in myTextBoxes) {
tb.Content = "UPDATED CONTENT!";
}

WPF DataGrid editable cells with Items put directively

So, I have a DataGrid, that I want to manipulate programmatically a lot.
string[] values = new string[something.Count];
for (int i = 0; i < somethingElse.Count; i++)
{
if (condition)
values[i] = Data[i].ToString();
else
values[i] = "";
}
var style = new System.Windows.Style(typeof(DataGridRowHeader));
style.Setters.Add(new Setter(DataGridRowHeader.ContentProperty, Data[0].ToString()));
MyGrid.Items.Add(new DataGridRow()
{
HeaderStyle = style,
Item = values
});
This I do in a loop and I am able to fill in my grid with all the data I need.
Later, I am able to access cells, edit them, take their values, whatever I want and need.
However, when user wants to use the grid as you would in MS Excel, the cells are not editable.
So I went the other way and created a :
ObservableCollection<ObservableCollection<string>> gridData = new ObservableCollection<ObservableCollection<string>>();
//*** ... *** the gridData is filled in the same way, you can imagine
MyGrid.ItemsSource = gridData;
This does fill in the data perfectly the same way, more than that, the data are now editable.
But my custom row headers disappeared.
I need them, also, I do not think I want to use binding for row header values.
Can the first approach be somehow modified to still be editable, but rows with data being filled the very same way?
I eventually solved this combining both the abovementioned approaches.
First, I generate ObservableCollection<ObservableCollection<string>> dataCollection, fill it with data.
Next I generate an ObservableCollection<DataGridRow> rowCollection collection.
In my declarative part of the loop from the first approach, that you can see in the question, I do this
rowCollection.Add(new DataGridRow()
{
HeaderStyle = style,
Item = dataCollection[i] //for the corresponding iteration element
});
This all ends up when setting
MyGrid.ItemsSource = rowCollection;
Till now (very little time of testing), this seems to be what I was looking for, hope it might help someone else aswell.

How to check only one radio button at a time (others disable)

I am working in silverlight c# and i have situation where i create radio buttons programatically. My code to do so is this:
Grid childGrid = CreateChildGrid();
int NumberOfRadioButton =0;
RadioButton[] RadioBut= new RadioButton[5];
int count = 0;
foreach (var item in param.Component.Attributes.Items)// this param.Component.Attributes.Items value is 4 in fact.
{
NumberOfRadioButton++;
RadioBut[count] = new RadioButton();
RadioBut[count].GroupName = item;
RadioBut[count].Content = item;
sp.Children.Add(RadioBut[count]);
count++;
}
Problem: The problem is it checks all the button where as i want only one button checked at a time . I mean if one checked the others must be disable.
Could some one please help me to achieve my target ? Thanks a lot.
Note: I am using silverlight to do so.
You should set the GroupName property of all RadioButtons to the same, so they will be "grouped", meaning only one of them can be selected at a time.
So this line:
rbs[count].GroupName = item;
Should be something like this:
rbs[count].GroupName = "MyRadioButtonGroup";
Of course the string can be anything, as long as it is the same for all RadioButtons.
You'll have to assign these radio buttons to a common group. Check Radio Button Group Name
RadioButton[] rbs = new RadioButton[5];
rbs.GroupName = "Add your common groupname here"; // added code
int count = 0;

How to remove row from datagrid in asp.net

I have a datagrid dgCompanies declare like this:
// bind the data to the datagrid
dgCompanies.PageSize = pageSize;
dgCompanies.DataSource = rdr;
Then I need to check the records inside this datagrid:
int j = 0;
foreach (DataGridItem item in dgCompanies.Items)
{
HtmlGenericControl name = (HtmlGenericControl)item.Cells[j].FindControl("SpanTitle");
string drstring = name.InnerHtml.Trim();
if (checkingfunction(drstring))
{
//do removing record from datagrid.
I tried this: item.Cells.Remove(item.Cells[j]); but the result still there, don't see anything removed!
}
j = j + 1;
}
dgCompanies.DataBind();
How could I remove that record from datagrid dgCompanies when if condition is satisfy ?
I agree with #Andrei, it would be best to just not return that data. Otherwise you're returning and churning through data that is unnecessary. However, if in the context of you can't filter the data up front, I suppose you could add a css class "donotshow" to your rows (see How do I specify CSS classes for specific rows in a GridView?).
Then using jquery use
$('.donotshow').hide();
Again, in this regard, you are still returning all that HTML to the browser, so that will make your page size larger than necessary. Additionally, using this method could screw up pagination if you using it (example, if it says "rows 1-10", but 5 rows are hidden, then that will look goofy).
Hope this helps

Categories

Resources