How to add items from a textbox to an array? C# - arrays

I want to add items from 13 textboxes to the 13 elements of an array. All at one time using a button. How would I go about that?
List<double> scoreArray = new List<double>();
TextBox[] textBoxes = { week1Box, week2Box, week3Box, week4Box, week5Box, week6Box, week7Box, week8Box, week9Box, week10Box, week11Box, week12Box, week13Box };
for (int i = 0; i < textBoxes.Length; i++)
{
scoreArray.Add(Convert.ToDouble(textBoxes[i].Text));
}

Put an event handler on the button's Click event.
In the handler, extract the textbox values and set them into the values of the array.
Easy peasy!

Related

How to bind the multiple selected items of list box in xaml

I have Flags Enum value which I have bound into the item source of listbox. I have used SelectionMode as multiple. I want assign all the selected items of the listbox to a flag Enum property. How can I bind the selected items?
private void ListBox_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
ListBox list = sender as ListBox;
this.Weekdays=list.SelectedItems.GetType().GetEnumValues().GetEnumerator()
as Weekdays;
}
Here weekdays is Flags-Enum of type Weekdays.I want bind all the selected items of listbox to Weekdays.
The code below is all that you need! list.SelectedItems contains all the selected items, if multiple selection mode is selected. You can track them during selection changed event of list. You can't assign the values to an enum, instead create a list and use that list for storing the selected items using the same data class as the list source.
private List<DataClass> SelectedItemsList = new List<DataClass>();
if (list.SelectedItems.Count >= 0)
{
for (int i = 0; i < list.SelectedItems.Count; i++)
{
SelectedItemsList.Add(list.SelectedIndices[i]);
}
}

How to get value of cell at current row GridView winforms devexpress

I need to know which row I selected and get value of cell(Ex:idproduct) before to click Edit button.
As #brendon is referring to, if gridView is the current View on your GridControl:
// Get your currently selected grid row
var rowHandle = gridView.FocusedRowHandle;
// Get the value for the given column - convert to the type you're expecting
var obj = gridView.GetRowCellValue(rowHandle, "FieldName");
You can use the GridView's GetRowCellValue method to retrieve the focused row value.
http://documentation.devexpress.com/#windowsforms/DevExpressXtraGridViewsGridGridView_GetRowCellValuetopic
See also: http://documentation.devexpress.com/windowsforms/CustomDocument753.aspx
public int idproductx;
public void tProductGridView_RowClick(object sender, RowClickEventArgs e)
{
if (e.Clicks > 0)
{
idproductx = (int)((GridView)sender).GetRowCellValue(e.RowHandle, "idproduct ");
}
}

Assigning values to array of 'Combo Boxes' in a 'Group Box' using for each loop in c#

I have 10 comboBox in a groupBox
for I just want to display a calculated value in respective comboBox like this say if I set a varible double i=08.00; then on button click cmboBox should display values like this
CB1-08.00
CB2-09.50
CB3-10.00
CB4-10.50
CB5-11.00
CB6-11.50
.... and so on upto CB10 But I am getting output like this
And Code
private void button1_Click(object sender, EventArgs e)
{
double i=08.00;
foreach (var comboBox in groupBox1.Controls.OfType<ComboBox>())
{
comboBox.Text = i.ToString("00.00");
i = i + 0.5;
}
}
Your combobox order is different in the collection so it inserts the numbers randomly. May be you can name your combobox for instance like cmb1,cmb2,cmb3 etc. and if you update your code it will run.
Your controls in the Controls collection are not sorted by their appearance on the form. You will need to find a way to sort them if you need different values in each based on their position.
Foreach loop doesn't give the collection in the order you wanted. The way to go forward is to give a tag id to each combo box, then you can use that to assign a value to them them.
So your first combo box will start with tag id 0, and the last one will have 8,
double val = 08.00;
for (int i = 0; i < groupBox1.Controls.Count; ++i)
{
var combobox = groupBox1.Controls[i] as ComboBox;
int tag = int.Parse(combobox.Tag.ToString());
double value = val + (0.5 * tag);
combobox.Text = value.ToString("00.00");
}
Make sure you tag the cobbo box in the order you wanted them.

CommandProperty to button column added dynamically in DataGrid doesn't work

I have a DataGrid bound to an ObservableCollection. I add cols dynamically. Cols struc is 1st col is TextBlock rest all are Buttons. I have certain issues with the buttons :
I want to set Command for that col, calling a function "OpenTORWindow" with 2 parameters (String, String). I can't make out how do I set it. Code to add cols is as :
FrameworkElementFactory buttonTemplate = null;
for (int i = 0; i < GlobalUtils.TOR_List.Count; i++)
{
buttonTemplate = new FrameworkElementFactory(typeof(Button));
switch (i) {
case 0:
buttonTemplate.SetBinding(Button.ContentProperty,
new Binding("CLVButtonText"));
break;
case 1:
buttonTemplate.SetBinding(Button.ContentProperty,
new Binding("MKBLButtonText"));
break;
}
buttonTemplate.SetBinding(Button.CommandProperty, new Binding("MyCommand"));
RoutedEventHandler handler = new RoutedEventHandler(OpenNewWindow);
buttonTemplate.AddHandler(Button.ClickEvent, handler, true);
this.seivesTorGrid.Columns.Add(new DataGridTemplateColumn()
{
Header = GlobalUtils.TOR_List[i].TOR_Id,
CellTemplate = new DataTemplate() { VisualTree = buttonTemplate }
});
}
I assign MyCommand with:
MyCommand = new RelayCommand(param => this.OpenWindow(s.SeiveIdSize))
But the MyCommand is never triggered. Then I added AddHandler, that's working.
Any idea why CommandProperty is not working.
The button you are adding shares the DataContext from the current row in the DataGrid, so when you call 'MyCommand', WPF searches the object in TOR_List, and as it probably does not exists, it will not execute.
You can check the output windows to check for binding errors.
To achieve what you want, you'll have to create the command in the object from which TOR_List is a list for, or use RelativeSource.

Creating an Hours ComboBox and Minutes ComboBox in Windows Forms

I want to create 2 ComboBox controls. I want the first ComboBox to show Hours and the second to show Minutes.
Furthermore, I'd like to first create an Hours list and Minutes list and then bind each list to its corresponding ComboBox. Finally, I would like to combine both ComboBox controls into a single custom control.
Can anyone provide some hints on how I should proceed?
Not sure if this is what you want; see ComboBox class, to add items to combo box programmatically.
Assuming that you have two combo boxes for hours(cbxHours) and minutes(cbxMinutes), you can call SetHours(), and SetMinutes() to populate your combos.
void SetHours()
{
for (int i = 0; i < 2400; i+=100) {cbxHours.Items.Add(string.Format("{0:0###}", i));}
}
void SetMinutes()
{
for (int i = 0; i < 60; i++) { cbxMinutes.Items.Add(string.Format("{0:0#}", i)); }
}
You can accomplish your binding objective in a straightforward fashion with the following approach:
comboBox1.DataSource = Enumerable.Range(1, 12).ToList();
comboBox2.DataSource = Enumerable.Range(1, 60).ToList();

Resources