Setting column span for button variable - c#
I have created a grid in my Xamarin application which has 4 rows and 24 columns.
I have 12 buttons that use the same properties, so to save myself creating all 12 separately, I have done it like this
var functionButton = new Style(typeof(Button))
{
Setters = {
new Setter { Property = BackgroundColorProperty, Value = Color.FromHex ("#F0E68C") },
new Setter { Property = Button.TextColorProperty, Value = Color.Black },
new Setter { Property = Button.CornerRadiusProperty, Value = 0 },
new Setter { Property = Button.FontSizeProperty, Value = 10 }
}
};
then add them to the grid as below
controlGrid.Children.Add(new Button { Text = "End" + Environment.NewLine + "Sale", Style = functionButton }, 0, 0);
controlGrid.Children.Add(new Button { Text = "Repeat" + Environment.NewLine + " Last", Style = functionButton }, 1, 0);
controlGrid.Children.Add(new Button { Text = "Void" + Environment.NewLine + "Line", Style = functionButton }, 2, 0);
and so on, until all 12 have been added.
However, I want each button to take up 2 columns.
Usually, if I'd created the buttons separately I could use Grid.SetColumnSpan(Button1, 2);
But that doesn't seem very efficient, as I'd need to create all 12 buttons separately. Is there another way that I can set the column span using this method of creating the buttons that I've currently got?
You can use a for loop.
var btnLabels = new[] {
"End" + Environment.NewLine + "Sale",
"Repeat" + Environment.NewLine + " Last",
"Void" + Environment.NewLine + "Line"
};
Button buttonCtrl;
for (int row= 0; row < btnLabels.Length; row++)
{
buttonCtrl = new Button { Text = btnLabels[row], Style = functionButton };
Grid.SetColumnSpan(btn, 2);
controlGrid.Children.Add(btn, row, 0);
}
Related
How to fill picker with a list
I am updating older code but part of it must stay the same. I have now picker that needs to be filled with list. My list public List<TimeoutBetweenSentences> FillTimoutOptions() { var newListTimeoutBetweenSentenceses = new List<TimeoutBetweenSentences>() { new TimeoutBetweenSentences() { Position = 0, Text = "+ 0 sekund", Value = 0 }, new TimeoutBetweenSentences() { Position = 1, Text = "+ 1 sekunda", Value = 1 }, new TimeoutBetweenSentences() { Position = 2, Text = "+ 2 sekundy", Value = 2 }, new TimeoutBetweenSentences() { Position = 3, Text = "+ 3 sekundy", Value = 3 }, new TimeoutBetweenSentences() { Position = 4, Text = "+ 4 sekundy", Value = 4 }, new TimeoutBetweenSentences() { Position = 5, Text = "+ 5 sekund", Value = 5 }, }; return newListTimeoutBetweenSentenceses; } List<TimeoutBetweenSentences> allOptions = FillTimoutOptions(); sentencePausesStepper.Items.Add(allOptions.Select(m => m.Text).ToList().ToString()); however this displays just as "System collections" DO zou have any idea?
this is adding an entire list as ONE element sentencePausesStepper.Items.Add(allOptions.Select(m => m.Text).ToList().ToString()); to add elements of one list to another, use AddRange instead sentencePausesStepper.Items.AddRange(allOptions.Select(m => m.Text).ToList().ToString()); or better, do this sentencePausesStepper.ItemsSource = allOptions; sentencePausesStepper.ItemDisplayBinding = new Binding("Text");
How can I deserialize and display these instances
I created these instances for coffee types and in final I want to deserialize them and display them per instance. I created a foreach loop but it gives me a lot of repeated data and If possible I want to display data instance per instance. Is there something else that I can use to display them? public void CoffeeTypes() { Coffee coffeeType1 = new Coffee() { CoffeeID = "321", CoffeePrice = 1.50, TSpoonsSugar = 'N', CoffeeAmountRatio = 1, MilkAmountRatio = 0, Size = 'S' }; Coffee coffeeType2 = new Coffee() { CoffeeID = "322", CoffeePrice = 2.99, TSpoonsSugar = 'N', CoffeeAmountRatio = 1, MilkAmountRatio = 0, Size = 'M' }; Coffee coffeeType3 = new Coffee() { CoffeeID = "323", CoffeePrice = 4.50, TSpoonsSugar = 'N', CoffeeAmountRatio = 1, MilkAmountRatio = 0, Size = 'L' }; Coffee coffeeType4 = new Coffee() { CoffeeID = "413", CoffeePrice = 1.99, TSpoonsSugar = 'Y', CoffeeAmountRatio = 1, MilkAmountRatio = 0, Size = 'S' }; List<Coffee> coffeeData = new List<Coffee>() { coffeeType1, coffeeType2, coffeeType3, coffeeType4 }; Coffee.SerializeData(coffeeData, filePath); List<Coffee> coffeePosibilities = Coffee.DeserializeData<Coffee>(filePath); Coffee.coffeeInstance = coffeePosibilities; foreach (var coffee in coffeePosibilities) { Console.WriteLine("Coffee Type 1 : Coffee ID: " + coffee.CoffeeID); Console.WriteLine("Coffee Type 2 : Coffee ID: " + coffee.CoffeeID); Console.WriteLine("Coffee Type 3 : Coffee ID: " + coffee.CoffeeID); Console.WriteLine("Coffee Type 4 : Coffee ID: " + coffee.CoffeeID); }
Change your foreach loop to output information for your current element only foreach (var coffee in coffeePosibilities) { Console.WriteLine($"Coffee Type : Coffee ID: {coffee.CoffeeID}"); }
Move existing item to the top of a listview
I add items from an array into a ListView like below: for (int i = 0; i < dataKeyList.Count; i++) { CallTabLv.Items.Add(new { Label = " " + keyArray[i], Value = valueArray[i] }); } If the keyArray[i] contains an item called CustomerName, how can I move it and its value to the top of the list ? Edit: Let say: string[] arr1 = new string[] { "one", "two", "three" }; string[] arr2 = new string[] { "1", "2", "3" }; for (int i = 0; i < arr1.Length; i++) { listView.Items.Add(new { C1 = arr1[i], C2 = arr2[i] }); } Output: Col Col2 ----------- one 1 two 2 three 3 I want to move "three 3" to the top of the list, so it would be like: Col Col2 ----------- three 3 two 2 one 1 Any suggestions?
Instead of adding item, You can use [Items.Insert][1] to insert an item in particular position,It can be used To Change position of an existing item in nth index. ListViewItem item= new ListViewItem("Test");//Define item here; if(!CallTabLv.Items.Contains(theItem)) { CallTabLv.Items.Insert(0,item); } else { n = CallTabLv.Items.IndexOf(item); CallTabLv.Items.RemoveAt(n); CallTabLv.Items.Insert(0, item); }
How to create area range chart
I want to create area range chart as given in the following link I want to add data to ranges using loop on my data. What should be the type of the ranges to create chart? Please suggest. Thanks in advance. Here's the JSFiddle code: HTML: <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/highcharts-more.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div> JS: $(function () { var ranges = [[1246406400000,33,22],[1246492800000,24,12],[1246579200000,15,1],[1246665600000,28,17],[1246752000000,22,12],[1246838400000,34,22],[1246924800000,30,19],[1247011200000,27,15],[1247097600000,35,24],[1247184000000,29,14],[1247270400000,32,20],[1247356800000,32,21],[1247443200000,34,23],[1247529600000,19,9],[1247616000000,31,21],[1247702400000,22,7],[1247788800000,25,11],[1247875200000,19,6],[1247961600000,33,18],[1248048000000,33,18],[1248134400000,21,7],[1248220800000,31,19],[1248307200000,25,15],[1248393600000,29,19],[1248480000000,34,23],[1248566400000,21,9],[1248652800000,27,12],[1248739200000,19,4],[1248825600000,32,19],[1248912000000,32,20],[1248998400000,16,1]], ranges2 = [[1246406400000,22,-22],[1246492800000,12,-12],[1246579200000,1,-1],[1246665600000,17,-17],[1246752000000,12,-12],[1246838400000,22,-22],[1246924800000,19,-19],[1247011200000,15,-15],[1247097600000,24,-24],[1247184000000,14,-14],[1247270400000,20,-20],[1247356800000,21,-21],[1247443200000,23,-23],[1247529600000,9,-9],[1247616000000,21,-21],[1247702400000,7,-7],[1247788800000,11,-11],[1247875200000,6,-6],[1247961600000,18,-18],[1248048000000,18,-18],[1248134400000,7,-7],[1248220800000,19,-19],[1248307200000,15,-15],[1248393600000,19,-19],[1248480000000,23,-23],[1248566400000,9,-9],[1248652800000,12,-12],[1248739200000,4,-4],[1248825600000,19,-19],[1248912000000,20,-20],[1248998400000,1,-1]], ranges3 = [[1246406400000,-22,-45],[1246492800000,-12,-30],[1246579200000,-1,-17],[1246665600000,-17,-43],[1246752000000,-12,-40],[1246838400000,-22,-45],[1246924800000,-19,-43],[1247011200000,-15,-45],[1247097600000,-24,-50],[1247184000000,-14,-37],[1247270400000,-20,-44],[1247356800000,-21,-42],[1247443200000,-23,-42],[1247529600000,-9,-37],[1247616000000,-21,-40],[1247702400000,-7,-24],[1247788800000,-11,-27],[1247875200000,-6,-27],[1247961600000,-18,-34],[1248048000000,-18,-46],[1248134400000,-7,-36],[1248220800000,-19,-48],[1248307200000,-15,-30],[1248393600000,-19,-49],[1248480000000,-23,-50],[1248566400000,-9,-38],[1248652800000,-12,-27],[1248739200000,-4,-26],[1248825600000,-19,-45],[1248912000000,-20,-40],[1248998400000,-1,-17]]; $('#container').highcharts({ title: { text: 'Sentiment Flood Map' }, xAxis: { type: 'datetime' }, yAxis: { title: { text: null } }, tooltip: { crosshairs: true, shared: true, valueSuffix: '' }, legend: { }, series: [ { name: 'Positive', data: ranges, type: 'arearange', lineWidth: 0, linkedTo: ':previous', color: Highcharts.getOptions().colors[2], fillOpacity: 0.8, zIndex: 0 } , { name: 'Neutral', data: ranges2, type: 'arearange', lineWidth: 0, linkedTo: ':previous', color: Highcharts.getOptions().colors[1], fillOpacity: 0.8, zIndex: 0 } , { name: 'Negative', data: ranges3, type: 'arearange', lineWidth: 0, linkedTo: ':previous', color: Highcharts.getOptions().colors[3], fillOpacity: 0.8, zIndex: 0 } ] }); });
Here is an example the produces a Chart graphic just like the one in the linked image. Note: After I have created a few testdata I calculate a dummy series with a transparent color that will make the whole data stack up so that the median of the "neutral" series sits nicely on one horizontal line. int numPoints = 30; // create some test data List<int> neutralData = new List<int>(); List<int> negativeData = new List<int>(); List<int> positiveData = new List<int>(); List<int> dummyData = new List<int>(); for (int i = 0; i < numPoints; i++) { // the real data series, using a Random R: positiveData.Add(R.Next(i + 22)); neutralData .Add(R.Next(i + 33)); negativeData.Add(R.Next(i + 44)); // calculate the transparent bottom series: dummyData.Add( - neutralData[i] / 2 - negativeData[i]); } // set up the Chart: chart1.ChartAreas.Add("StackedArea"); // if necessary Series s0 = chart1.Series.Add(" "); Series s1 = chart1.Series.Add("negative"); Series s2 = chart1.Series.Add("neutral"); Series s3 = chart1.Series.Add("positive"); foreach (Series s in chart1.Series) s.ChartType = SeriesChartType.StackedArea; s0.Color = Color.Transparent; s1.Color = Color.FromArgb(200, Color.Red); s2.Color = Color.FromArgb(200, Color.LightSlateGray); s3.Color = Color.FromArgb(200, Color.Green) // now add the data points: for (int i = 0; i < numPoints; i++) { s0.Points.AddXY(i, dummyData[i]); s1.Points.AddXY(i, negativeData[i] ); s2.Points.AddXY(i, neutralData [i]); s3.Points.AddXY(i, positiveData[i]); } If you want to show a tooltip similar to the one from your example you can add this to the AddXY loop: int a2 = dummyData[i] + negativeData[i]; int a3 = a2 + neutralData[i]; int a4 = a3 + positiveData[i]; string tt = string.Format( "Data Point {0}\r\nPositive: {1} - {2}\r\n" + "Neutral: {2} - {3}\r\nNegative: {3} - {4}", i, a4, a3, a2, dummyData[i]); s1.Points[i].ToolTip = tt; s2.Points[i].ToolTip = tt; s3.Points[i].ToolTip = tt; Here is an example image:
You can not create a chart exactly like the one shown in link but you can use different series on your chart. I am posting the code that creates a chart and populates it with 2 series, you can add as many series as you want. DataSet dataSet; ConnectionClass.GetInstance().connection_string = Properties.Settings.Default.MindMuscleConnectionString; ConnectionClass.GetInstance().Sql = "Select Count(MemberInfo.memberName) as 'Members', CompetitionName as 'Competition' FROM MemberInfo, MemberBodyInfo, Competition WHERE MemberInfo.memberID = MemberBodyInfo.memberID AND MemberBodyInfo.weight >= Competition.CompetitionCategory and MemberBodyInfo.weight <= Competition.CompetitionCategory + 5 group by CompetitionName;"; dataSet = ConnectionClass.GetInstance().GetConnection; chart1.Series["Series1"].Name = "Members"; chart1.Series["Members"].YValueMembers = "Members"; chart1.Series["Members"].XValueMember = "Competition"; chart1.Series.Add("Members2"); chart1.Series["Members2"].ChartType = SeriesChartType.StackedArea; chart1.Series["Members2"].IsValueShownAsLabel = true; chart1.Series["Members2"].YValueMembers = "Members"; chart1.Series["Members2"].XValueMember = "Competition"; this.chart1.Titles.Add("Competition Participants"); // Set the chart title chart1.Series["Members"].ChartType = SeriesChartType.StackedArea; chart1.Series["Members"].IsValueShownAsLabel = true; // To show chart value chart1.DataSource = dataSet; chart1.DataBind(); I have not actually created a new series from some different data... Both series are same here but I have just showed you an example.
Get value from listBox using List
I am using a ListBox and the data is a class: private class Duration { public int Value { get; set; } public string Label { get; set; } } I bind the class in this way: var durations = new List<Duration>() { new Duration() { Value = 5, Label = "5 minutes" }, new Duration() { Value = 10, Label = "10 minutes" }, new Duration() { Value = 15, Label = "15 minutes" }, new Duration() { Value = 30, Label = "30 minutes" }, new Duration() { Value = 45, Label = "45 minutes" }, new Duration() { Value = 60, Label = "1 hour" }, new Duration() { Value = 90, Label = "1 hour and half" } }; this.listTime.DataSource = durations; this.listTime.DisplayMember = "Label"; this.listTime.ValueMember = "Value"; Everything works fine and the labels are show. When i go to read the selected value, I am not able to recover the value of the selected item. I was expecting to be able to do this: int value = listTime.SelectedItems[0].Value; or at least this: Duration value = listTime.SelectedItems[0]; but this gives me error, what I am doing wrong? How is the right way to get the value of the selected item on the ListBox?
if (listTime.SelectedIndex != -1) { var item = listTime.SelectedItem as Duration; //or as suggested by 'Stu' var item = (Duration)listTime.SelectedItem; MessageBox.Show(item.Value.ToString()); }
If you use Listbox this code is Ok: listTime.Items[0].Value