Replace Images at the Top of ImageList and ListView - c#

I will load say 10 images in imagelist and list View. then later, I want to replace the first 5 (top 5 in the list). But The new images are always added at the bottom of the list in the display.
Initialize_ListView()
for (int i = 0; i < 10; i++)
{
AddItemTo_ListView(i);
}
Now I later use this code to replace the top 5
for (int i = 0; i < 5; i++)
{
ImageAndThumb imgObj = new ImageAndThumb(output);
Replace(imgObj , i)
}
private void Initialize_ListView()
{
this.imageList1.Images.Clear();
this.listView1.Clear();
this.listView1.View = View.SmallIcon;
this.imageList1.ImageSize = new Size(75, 80);
this.listView1.SmallImageList = this.imageList1;
}
private void AddItemTo_ListView(int index)
{
ImageAndThumb imgObj = new ImageAndThumb(output);
ImageAndThumbLst.Add(imgObj);
this.imageList1.Images.Add(ImageAndThumbLst[index].Thumb);
ListViewItem item = new ListViewItem();
item.ImageIndex = index;
this.listView1.Items.Add(item);
}
private void Replace(ImageAndThumb obj, int indx)
{
ImageAndThumbLst[indx] = obj;
this.imageList1.Images[indx] = ImageAndThumbLst[indx].Thumb;
ListViewItem item = new ListViewItem();
item.ImageIndex = indx;
this.listView1.Items[indx]=item;
}

For anyone making the same mistake, I was creating a new item in he replace function which is unnecessay. The new replace function should look like this
private void Replace(ImageAndThumb obj, int indx)
{
ImageAndThumbLst[indx] = obj;
this.imageList1.Images[indx] = ImageAndThumbLst[indx].Thumb;
listView1.Refresh();
}

Related

How to check if checkbox is selected?

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.

Populate TextBoxes from a List

I am trying to populate TextBoxes from a list. I have been able to populate ComboBoxes with comboList:
var comboList = new System.Windows.Forms.ComboBox[4];
comboList[0] = cmbSite1Asset;
comboList[1] = cmbSite2Asset;
comboList[2] = cmbSite3Asset;
comboList[3] = cmbSite4Asset;
List<CRCS.CAsset> assets = _rcs.Assets;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int i = 0; i < 4; ++i)
{
comboList[i].Items.Add(id);
}
}
But when I try and apply the same principle to TextBoxes
var aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;
for (int n = 0; n < 8; ++n)
{
aosList[n].Items.Add(id);
}
}
TextBox does not like Items.Add ( aosList[n]Items.Add(id); )
I am looking fore a reference or guidance resolving this issue. Thanks!
You should use ComboBox for your problem,instead of iterating on each element,You simply use below lines to populate combobox.
comboList.DataSource=assets;
comboList.DisplayMember="ID";
comboList.ValueMember="ID";
However,if you want your values in TextBox,you can use TextBox.AppendText Method, but it will not work like ComboBox as it will contain texts+texts+texts, will not have indexes like ComboBox.
private void AppendTextBoxLine(string myStr)
{
if (textBox1.Text.Length > 0)
{
textBox1.AppendText(Environment.NewLine);
}
textBox1.AppendText(myStr);
}
private void TestMethod()
{
for (int i = 0; i < 2; i++)
{
AppendTextBoxLine("Some text");
}
}
A Combobox is a collection of items, and so has an Items property from which you can add/remove to change it's contents. A Textbox is just a control that displays some text value, so it has a Text property which you can set/get, and which denotes the string that is displayed.
System.Windows.Forms.TextBox[] aosList = new System.Windows.Forms.TextBox[8];
aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;
for (int n = 0; n < 8; ++n)
{
aosList[n].Text = assets[n].ID; // make sure you have 8 assets also!
}
int i = 1;
foreach (var asset in assets)
{
this.Controls["txtAsset" + i].Text = asset.ID;
i++;
}

Trying to display default selected value from ListView

When I select a particular item in ListView item, I am able to display that particular item, and when the form loads I am getting the last item displayed by default (say the 10th item). However, what I want is to display the first item by default. How can I do this? I tried to do something like
listView1.Items[1].Selected = true;
but it's not working:
public partial class GroupExmStart : Form
{
string[] totDisplayQsn = null;
string[] QAndA = null;
string[] words = null;
private List<Question> questions;
ListViewItem lvi;
public GroupExmStart(string GroupName, string DurationID)
{
InitializeComponent();
this.GrpID=GroupName;
TopiID=db.GetTopicIDForGroup(GrpID);
string[] conf = db.GetConfiguration(Convert.ToInt16(DurationID)).Split('|');
Question qsn = new Question();
questions = qsn.Foo(TopiID, conf);
int z = Quiz(questions);
totQsn = Convert.ToInt16(conf[0]);
for (int kk = 1; kk <= totQsn; kk++)//using this I am adding items to listview
{
lvi = new ListViewItem();
lvi.Text = kk.ToString();
listView1.Items.Add(lvi);
}
totDisplayQsn = new string[totQsn + 1];
}
int Quiz(List<Question> questions)//using this I a passing set of questions to be displayed
{
foreach (Question question in questions)
{
DisplayQuestion(question);
}
return 0;
}
private void DisplayQuestion(Question question)//using this i am displaying questions
{
string Q = question.Text;
label5.Text = Q;
string OP1 = question.Option1;
string OP2 = question.Option2;
string OP3 = question.Option3;
string OP4 = question.Option4;
radioButton12.Text = OP4;
radioButton11.Text = OP4;
radioButton10.Text = OP4;
radioButton9.Text = OP4;
}
}
private void listView1_MouseClick(object sender, MouseEventArgs e)//using this i am selection particular item and displaying it
{
if (totDisplayQsn.GetLength(0) >= 0)
{
if (listView1.SelectedItems.Count > 0)
{
var q = Convert.ToInt16(listView1.SelectedItems[0].Text);
var selectedQuestion = questions[q-1];
DisplayQuestion(selectedQuestion);
}
}
}
Thanks in advance for any help.
Try this
listView1.SelectedItems[0].Focused = true;
To select the first item, access it by its zero based index. Place the code just after your items initialization code.
for (int kk = 1; kk <= totQsn; kk++)//using this I am adding items to listview
{
lvi = new ListViewItem();
lvi.Selected = false;
lvi.Text = kk.ToString();
listView1.Items.Add(lvi);
}
listView1.Items[0].Selected = true;
DisplayQuestion(questions[0]);
Remove the following code:
int Quiz(List<Question> questions)//using this I a passing set of questions to be displayed
{
foreach (Question question in questions)
{
DisplayQuestion(question);
}
return 0;
}
Ok try the below code:
ListViewItem foundItem = listView1.FindItemWithText("Select4", false, 0, true);
if (foundItem != null)
{
listView1.Items[foundItem.Index].Selected = true;
}
That's All.

Displaying Particular Item On listView1_MouseClick

In Quize method I am passing qestions which contains set of all my Questions to be displayed using DisplayQuestion(),Question is my Class, Problem is that I am getting only First Question displayed, how can I get them displayed when i click on listviewItem if suppose questionscontains 10 Questions,than in listviewItem I have displayed numbers(1 2 3 4 5 ....10),when i click on each number how do i display that particular Questiondisplyed on click and if not clicked how all Questions displayed one by one using timer
public partial class GroupExmStart : Form
{
string[] randomQsn = new string[totQsn + 1]; //totQsn is the total number of question for e.g.10
public GroupExmStart(string GroupName, string DurationID)
{
InitializeComponent();
this.GrpID=GroupName;
TopiID=db.GetTopicIDForGroup(GrpID);
string[] conf = db.GetConfiguration(Convert.ToInt16(DurationID)).Split('|');
Question qsn = new Question();
var questions = qsn.Foo(TopiID, conf);
int z = Quiz(questions);
totQsn = Convert.ToInt16(conf[0]);
for (int kk = 1; kk <= totQsn; kk++)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = kk.ToString();
listView1.Items.Add(lvi);
}
randomQsn = new string[totQsn + 1];
timer1.Interval = 1000; //1000ms = 1sec
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
int Quiz(List<Question> questions)
{
foreach (Question question in questions)
{
DisplayQuestion(question);
}
return 0;
}
private void DisplayQuestion(Question question)
{
string Q = question.Text;
label5.Text = Q;
string OP1 = question.Option1;
string OP2 = question.Option2;
string OP3 = question.Option3;
string OP4 = question.Option4;
radioButton12.Text = OP1;
radioButton11.Text = OP2;
radioButton10.Text = OP3;
radioButton9.Text = OP4;
}
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
if (randomQsn.GetLength(0) >= 0)
{
if (listView1.SelectedItems.Count > 0)
{
//here how should i get That particular Question so that i can display it
//something like this ? //Convert.ToInt16(listView1.SelectedItems[0].SubItems[0].Text)
DisplayQuestion(question);
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
tik++;
if (tik == 60)
{
label1.Text = (Convert.ToInt16(label1.Text) - 1).ToString();
tik = 0;
}
}
}
Thanks for any help in advance
The following is what you are looking for. You must grab the text of the list view item and use that as the index of the question.
if (listView1.SelectedItems.Count > 0)
{
var q = Convert.ToInt16(listView1.SelectedItems[0].Text);
var selectedQuestion = questions[q - 1];
DisplayQuestion(selectedQuestion);
}
In order for this to work, modify your constructor to the following:
private List<Question> questions;
public partial class GroupExmStart : Form
{
string[] randomQsn = new string[totQsn + 1]; //totQsn is the total number of question for e.g.10
public GroupExmStart(string GroupName, string DurationID)
{
InitializeComponent();
this.GrpID=GroupName;
TopiID=db.GetTopicIDForGroup(GrpID);
string[] conf = db.GetConfiguration(Convert.ToInt16(DurationID)).Split('|');
Question qsn = new Question();
/// THIS IS MODIFIED //
questions = qsn.Foo(TopiID, conf);
int z = Quiz(questions);
totQsn = Convert.ToInt16(conf[0]);
for (int kk = 1; kk <= totQsn; kk++)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = kk.ToString();
listView1.Items.Add(lvi);
}
randomQsn = new string[totQsn + 1];
timer1.Interval = 1000; //1000ms = 1sec
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}

Declaring an Array of Textboxes

I'm trying to declare the array Scores as an array of textboxes. It doesn't have a size. I also need to declare it as an instance variable, and instantiate it in the method, CreateTextBoxes. I keep getting an error, "Scores is a field but is used like a type."
namespace AverageCalculator
{
public partial class AverageCalculator : Form
{
private TextBox[] Scores;
public AverageCalculator()
{
InitializeComponent();
}
private void AverageCalculator_Load(object sender, EventArgs e)
{
btnCalculate.Visible = false;
}
private void btnOK_Click(object sender, EventArgs e)
{
int intNumTextBoxes;
intNumTextBoxes = Convert.ToInt32(txtNumScores.Text);
this.Height = 500;
btnCalculate.Visible = true;
btnOK.Enabled = false;
}
private void CreateTextBoxes(int number)
{
Scores[number] = new Scores[number];
int intTop = 150;
for (int i = 0; i < 150; i++)
{
}
}
}
}
your CreateTextBoxes should probably be something like this:
private void CreateTextBoxes(int number)
{
Scores = new TextBox[number];
for (int i = 0; i < number; i++)
{
Scores[i] = new TextBox();
}
}
As Adil suggested, a List<TextBox> is probably better in this case.
You need to instantiate TextBox but number should be constant You can read more about the array creation expression here. Its better to use List instead of array if you want variable size.
Scores = new TextBox[number];
Using List
List<TextBox> Scores= new List<TextBox>();
Your code should read:
Scores = new TextBox[number];
// do things with this array
The problem is in
private void CreateTextBoxes(int number)
{
Scores[number] = new Scores[number];
int intTop = 150;
for (int i = 0; i < 150; i++)
{
}
}
When you are trying to initialize the array, you are using the name of the field as they type and are including an index to the field name. Just change the new type to TextBox and remove the index accessor like this:
private void CreateTextBoxes(int number)
{
Scores = new TextBox[number];
int intTop = 150;
for (int i = 0; i < 150; i++)
{
}
}
replace line 1 with line 2
Scores[number] = new Scores[number];
Scores[number] = new TextBox();
You can't do this.
Scores[number] = new Scores[number];
Use a list of TextBox.

Categories

Resources