How to check if checkbox is selected? - c#

I have little issue with checkbox added programmatically. I don't know how to check which checkbox are selected, when I hit "Send Button".
layout.RemoveAllViewsInLayout();
CheckBox _Options = new CheckBox(Activity);
ScrollView _Scroll = new ScrollView(Activity);
_Scroll.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
LinearLayout _LScroll = new LinearLayout(Activity);
_LScroll.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
_LScroll.Orientation = Orientation.Vertical;
_LScroll.SetGravity(GravityFlags.CenterHorizontal);
//_Scroll.AddView(_LScroll);
Button _Send = new Button(Activity);
_Send.Text = "Wyƛlij";
_Send.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
for (int i = 0; i < _Item.options.Length; i++)
{
_Options.Text = _Item.options[i];
_Options.Id = i;
_Options.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
_LScroll.AddView(_Options);
}
_Send.Click += delegate
{
_MultiAnswer._QuestionId = _Item.id;
for(int i = 0; i < _Item.options.Length; i++)
{
if (_Options.Selected == true)
{
_MultiAnswer._AnwserOptionIds.SetValue(i + 1, i);
}
}
output = JsonConvert.SerializeObject(_MultiAnswer);
SendJson(_Url, DataCache._Login, output);
SetLayout(layout, btn);
};
_Scroll.AddView(_LScroll);
layout.AddView(_Scroll);
layout.AddView(_Send);
I'll try to work on ID of checkbox, but I really don't know how to do it. I was thinking on method, which give me code which create checkbox, but still don't know how to check if checkbox is selected.

I understand that you've many checkbox controls. So add them to a list as follows:
List<Checkbox> checkboxes = new List<Checkbox>
{
chk1, chk2, chk3
};
When you want to know which ones are checked, you'll do this:
IEnumerable<Checkbox> checkedCheckboxes = checkboxes.Where(chk => chk.Checked);

This is a quick and dirty sample on how to generate dynamic cheboxes and retreive their state :
public class MainActivity : Activity
{
public class MyItem
{
public string[] options { get; set; }
public int id { get; set; }
}
public class MyMultiAnswer
{
public int _QuestionId { get; set; }
}
private List<CheckBox> _chkList = new List<CheckBox>();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
var _Item = new MyItem() { options =new string [] { "aaa", "bbb", "ccc" }, id=0 };
var _MultiAnswer = new MyMultiAnswer() { _QuestionId = 0 };
ScrollView _Scroll = new ScrollView(this);
_Scroll.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
LinearLayout _LScroll = new LinearLayout(this);
_LScroll.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
_LScroll.Orientation = Orientation.Vertical;
_LScroll.SetGravity(GravityFlags.CenterHorizontal);
TextView txView = new TextView(this);
//_Scroll.AddView(_LScroll);
Button _Send = new Button(this);
_Send.Text = "test";
_Send.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
for (int i = 0; i < _Item.options.Length; i++)
{
CheckBox _Options = new CheckBox(this);
_chkList.Add(_Options);
_Options.Text = _Item.options[i];
_Options.Id = i;
_Options.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
_LScroll.AddView(_Options);
}
_Send.Click += delegate
{
_MultiAnswer._QuestionId = _Item.id;
string strChkIds = "";
foreach (var chk in _chkList.Where(c => c.Checked))
{
//_MultiAnswer._AnwserOptionIds.SetValue(_Options.Id + 1, _Options.Id);
//do something
strChkIds += " - " + chk.Id;
}
// or
for (int i = 0; i < _Item.options.Length; i++)
{
if (_chkList[i].Checked == true)
{
//_MultiAnswer._AnwserOptionIds.SetValue(i + 1, i);
//do something
}
}
//output = JsonConvert.SerializeObject(_MultiAnswer);
//SendJson(_Url, DataCache._Login, output);
//SetLayout(layout, btn);
txView.Text = "selected ids " + strChkIds;
};
_Scroll.AddView(_LScroll);
_LScroll.AddView(_Send);
_LScroll.AddView(txView);
// Set our view from the "main" layout resource
SetContentView(_Scroll);
}
}
This is a sample about how you can achieve this in a minimum effort.

Related

Is there a way to add descriptive text beside each data point in BarChart BarChartSeries using OpenXML(C#) for PPT?

I am able to get the 0 as value. However I want to set a descriptive text like B does not have any data and similar is the case for D here in the chart.
I want to have text whenever a company does not have any value which is 0 in this case. This can happen for multiple companies so how can I automate this?
I am using this to put category data-Sample code
DocumentFormat.OpenXml.Drawing.Charts.CategoryAxisData categoryAxisData1 = new DocumentFormat.OpenXml.Drawing.Charts.CategoryAxisData();
C.StringReference stringReference2 = new C.StringReference();
C.Formula formula2 = new C.Formula
{
Text = "Sheet1!$A$2:$A$7"
};
C.StringCache stringCache2 = new C.StringCache();
C.PointCount pointCount2 = new C.PointCount() { Val = Convert.ToUInt32(companies.Count) };
stringCache2.Append(pointCount2);
int count = 0;
for (int i = companies.Count-1; i >= 0 ; i--)
{
C.StringPoint stringPoint = new C.StringPoint() { Index = Convert.ToUInt32(count) };
C.NumericValue numericValue = new C.NumericValue
{
Text = companies[i].CompanyName
};
stringPoint.Append(numericValue);
stringCache2.Append(stringPoint);
count++;
}
stringReference2.Append(formula2);
stringReference2.Append(stringCache2);
categoryAxisData1.Append(stringReference2);
Sample code for value on barchart-
C.Values values1 = new C.Values();
C.NumberReference numberReference1 = new C.NumberReference();
C.Formula formula3 = new C.Formula
{
Text = "Sheet1!$B$2:$B$7"
};
C.NumberingCache numberingCache1 = new C.NumberingCache();
C.FormatCode formatCode1 = new C.FormatCode
{
Text = "General"
};
numberingCache1.Append(formatCode1);
C.PointCount pointCount3 = new C.PointCount() { Val = Convert.ToUInt32(companies.Count) };
numberingCache1.Append(pointCount3);
for (int i = 0; i < companies.Count; i++)
{
C.NumericPoint numericPoint = new C.NumericPoint() { Index = Convert.ToUInt32(i) };
//C.NumericValue numericValue = new C.NumericValue
//{
// Text = (sliderValueForComapnies[i] > 0) ? sliderValueForComapnies[i].ToString() : "Data not avaialble",
//};
C.NumericValue numericValue = new C.NumericValue();
if (sliderValueForComapnies[i] > 0)
{
numericValue.Text = sliderValueForComapnies[i].ToString();
}
//else
//{
// this area will have the companies with 0 as value so what can I do here to add text?
//}
numericPoint.Append(numericValue);
numberingCache1.Append(numericPoint);
}
numberReference1.Append(formula3);
numberReference1.Append(numberingCache1);
values1.Append(numberReference1);

MVVM with dynamic the Michrochart-NugetPackage doesn't work

I want to add a dynamic micro chart to my application but it doesn't work. After a call from a method a value gets added and it makes a completely new micro chart for my chart to have the new values, but the change isn't visible in the app. So the old Values stayed and there is no new one. Thanks for helping me.
WeightList = new List<float>();
WeightList.Add(0);
WeightList.Add((float)74.3);
entries = new ChartEntry[30];
SyncArray();
private void SyncArray()
{
if (WeightList.Count != entries.Length)
{
entries = new ChartEntry[WeightList.Count];
}
for (int i = 0; i <= WeightList.Count - 1; i++)
{
if (i == WeightList.Count - 1 || i == 0)
{
entries[i] = new ChartEntry(WeightList[i]) { Label = "" + i, ValueLabel = "" + WeightList[i] };
}
else
{
entries[i] = new ChartEntry(WeightList[i]) { Label = "" + i };
}
}
chart = new LineChart() { Entries = entries, BackgroundColor = SKColors.Transparent };
Chart = chart;
}
public LineChart Chart
{
get => chart;
set => SetProperty(ref chart, value);
}
public float Weight
{
get => weight;
set
{
weight = value;
WeightList.Add(weight);
SyncArray();
}
}
Credits: #Jason
What to change:
private void SyncArray()
{
if (WeightList.Count != entries.Length)
{
entries = new ChartEntry[WeightList.Count];
}
for (int i = 0; i <= WeightList.Count - 1; i++)
{
if (i == WeightList.Count - 1 || i == 0)
{
entries[i] = new ChartEntry(WeightList[i]) { Label = "" + i, ValueLabel = "" + WeightList[i] };
}
else
{
entries[i] = new ChartEntry(WeightList[i]) { Label = "" + i };
}
}
Chart = new LineChart() { Entries = entries, BackgroundColor = SKColors.Transparent };
}

How to make a new index (ContentPage in my CarouselPage) for each label you gather from the db?

this is my code. I want to achieve so I make a new carouselpage for each label I gather from the db. So I have made a for int and I get the values out from the label but now when I want to take it a step further and make a carouselpage for each label I am not sure how to make it work.
My page is a carouselpage both in code and xaml: so
public partial class PicturePage : CarouselPage
and in xaml:
<CarouselPage> && </CarouselPage>
My code:
List<string> labelList = new List<string> ();
async void loadtest ()
{
var getItems = await parseAPI.getInformation ();
foreach (var items in getItems ["results"])
{
var value = "";
imagesList.Add (value = items ["label"].ToString());
}
base.OnCurrentPageChanged ();
var index = Children.IndexOf (CurrentPage);
for (int i = 0; i < labelList.Count; i++ && i < index; i++ ) { //how should I add a new index for each label I gather?
myLabel.Text = myLabel.Text + labelList [i]+ "\n";
}
}
Add them by setting the Children property and creating ContentPages on the fly at runtime:
for (int i = 0; i < imagesList.Count; i++){
var carouselChild = new ContentPage {
Padding = padding,
Content = new StackLayout {
Children = {
new Label {
Text = labelList[i],
FontSize = Device.GetNamedSize (NamedSize.Medium, typeof(Label)),
HorizontalOptions = LayoutOptions.Center
},
new Image { Source = imagesList[i] }
}
}
};
Children.Add (carouselChild);
}

How can I draw iterating ul-li List pattern?

I want to draw the Html as in the Preview.
To see preview click here. View Source of the page to view the ul -li rendering.
For this I wrote a method shown below
private void DrawTree(HtmlGenericControl ParentULTag, int count)
{
for (int i = 0; i < count; i++)
{
HtmlGenericControl ChildLi = new HtmlGenericControl("li");
HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
ChildLi.Controls.Add(ChildUl);
ParentULTag.Controls.Add(ChildLi);
ChildLi.InnerText = i.ToString();
DrawTree(ChildUl, i);
}
}
But it Draw it like this
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
I Initiated the method in the pageload as shown
HtmlGenericControl Initial = new HtmlGenericControl("ul");
DrawTree(Initial,4);
Whats the mistake in this code?
Try this:
private static HtmlGenericControl GenerateList(int count)
{
HtmlGenericControl parent = new HtmlGenericControl("ul");
for (int i = 1; i <= count; i++)
{
HtmlGenericControl ChildLi = new HtmlGenericControl("li");
ChildLi.InnerText = i.ToString();
HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
ChildLi.Controls.Add(ChildUl);
for (int j = 1; j <= i; j++) {
HtmlGenericControl FinalLi = new HtmlGenericControl("li");
FinalLi.InnerText = j.ToString();
ChildUl.Controls.Add(FinalLi);
}
parent.Controls.Add(ChildLi);
}
return parent;
}
Edit:
You should represent your input as a tree structure. Here I used a Node object that has an ID and Children properties:
class Node
{
public int ID { get; set; }
public Node[] Children { get; set; }
public Node(int id, Node[] children)
{
ID = id;
Children = children;
}
public Node(int id)
{
ID = id;
Children = new Node[] { };
}
}
class Program
{
public static HtmlGenericControl GetList(Node root)
{
HtmlGenericControl ul = new HtmlGenericControl("ul");
GetListImpl(root, ul);
return ul;
}
private static void GetListImpl(Node root, HtmlGenericControl rootElement)
{
foreach (var item in root.Children)
{
HtmlGenericControl li = new HtmlGenericControl("li");
li.InnerText = item.ID.ToString();
if (item.Children.Length > 0)
li.Controls.Add(GetList(item));
rootElement.Controls.Add(li);
}
}
public static string HtmlGenericControlToString(HtmlGenericControl control)
{
string contents = null;
using (System.IO.StringWriter swriter = new System.IO.StringWriter())
{
HtmlTextWriter writer = new HtmlTextWriter(swriter);
control.RenderControl(writer);
contents = swriter.ToString();
}
return contents;
}
static void Main(string[] args)
{
Node root = new Node(0,
new Node[]{
new Node(1, new Node[]{
new Node(1),
new Node(2)
}),
new Node(2, new Node[]{
new Node(1),
new Node(2, new Node[]{
new Node(1)
})
})
});
var ul = GetList(root);
var html = HtmlGenericControlToString(ul);
Console.WriteLine(html);
}
}
Try this
HtmlGenericControl Initial = new HtmlGenericControl("ul");
DrawTree(ref Initial,4);
now method
private void DrawTree(ref HtmlGenericControl ParentULTag, int count)
{
for (int i = 0; i < count; i++)
{
HtmlGenericControl ChildLi = new HtmlGenericControl("li");
HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
ChildLi.Controls.Add(ChildUl);
ParentULTag.Controls.Add(ChildLi);
ChildLi.InnerText = i.ToString();
DrawTree(ref ChildUl, i);
}
}
The problem was simple
private void DrawTree(HtmlGenericControl ParentULTag, int count)
{
for (int i = 0; i < count; i++)
{
HtmlGenericControl ChildLi = new HtmlGenericControl("li");
HtmlGenericControl ChildUl = new HtmlGenericControl("ul");
ChildLi.InnerText = i.ToString();
ChildLi.Controls.Add(ChildUl);
DrawTree(ChildUl, i);
ParentULTag.Controls.Add(ChildLi);
}
}
Please note the position of line ChildLi.InnerText = i.ToString();
but still don't know why its so

DynamicDataDisplay chart horizontal string axis

I need to draw some charts using DynamicDataDisplay3. Everything works fine, except I can't find a way to change X axis to strings instead of dates or integers. This is how I tried to do it but I get only 1 value on X axis:
int i = 0;
using (MySqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
i++;
Analyze build = new Analyze();
build.id = i;
build.build = Convert.ToString(reader[0]);
builds.Add(build);
n1.Add(Convert.ToInt32(reader[1]));
}
}
var datesDataSource = new EnumerableDataSource<Analyze>(builds);
datesDataSource.SetXMapping(x => x.id);
var numberOpenDataSource = new EnumerableDataSource<int>(n1);
numberOpenDataSource.SetYMapping(y => y);
CompositeDataSource compositeDataSource1 = new CompositeDataSource(datesDataSource, numberOpenDataSource);
chBuild.AddLineGraph(compositeDataSource1, new Pen(Brushes.Blue, 2), new CirclePointMarker { Size = 6, Fill = Brushes.Blue }, new PenDescription(Convert.ToString(cmbBuildVertical.SelectedItem)));
chBuild.Viewport.FitToView();
I made my own LabelProvider to handle something similar to this. I wanted to override my DateTime labels into integers, to represent something different. In your case you could use something like this :
public class StringLabelProvider : NumericLabelProviderBase {
private List<String> m_Labels;
public List<String> Labels {
get { return m_Labels; }
set { m_Labels = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref="ToStringLabelProvider"/> class.
/// </summary>
public StringLabelProvider(List<String> labels) {
Labels = labels;
}
public override UIElement[] CreateLabels(ITicksInfo<double> ticksInfo) {
var ticks = ticksInfo.Ticks;
Init(ticks);
UIElement[] res = new UIElement[ticks.Length];
LabelTickInfo<double> tickInfo = new LabelTickInfo<double> { Info = ticksInfo.Info };
for (int i = 0; i < res.Length; i++) {
tickInfo.Tick = ticks[i];
tickInfo.Index = i;
string labelText = "";
labelText = Labels[Convert.ToInt32(tickInfo.Tick)];
TextBlock label = (TextBlock)GetResourceFromPool();
if (label == null) {
label = new TextBlock();
}
label.Text = labelText;
res[i] = label;
ApplyCustomView(tickInfo, label);
}
return res;
}
}
You can construct your list of ticks, and send it to the LabelProvider you create. Like this :
StringLabelProvider labelProvider = new StringLabelProvider(yourLabelList);
yourAxis.LabelProvider = labelProvider;

Categories

Resources