ListPicker without selected item - c#

I have a ListPicker and when this is opened, by default the first item is selected.
In my app, user select an item on ListPicker, this item is sent to my database. The next time that user opens the picker, automatically the selected item is the same (previous). So, I need no selected item.
The ListPicker is populated from a collection. I tried according to other answers (list.SelectedIndex = -1;), but doesn't work.
My code:
public ObservableCollection<observacao> obsObservacao { get; set; }
public class observacao
{
public string descricao { get; set; }
public double valor { get; set; }
public string valoradicional { get; set; }
}
pickerPagto1.ItemsSource = obsObservacao;
pickerPagto1.UpdateLayout();
The Problem:
private void botaoObs_Click(object sender, RoutedEventArgs e)
{
testarObs = "1";
pickerPagto1.Open();
}

As others have noted the ListPicker control must always have a selected item.
The way around this is to add a default first item to the list. You havent posted the code where you populate your collection, but this might be a good generic solution:
protected ObservableCollection<observacao> _obsObservacao;
public ObservableCollection<observacao> obsObservacao
{
get { return _obsObservacao;}
set {
_obsObservacao = value;
observacao noSelection = new observacao();
noSelection.descricao ="Nothing Selected";
_obsObservacao.Insert(0, noSelection);
}
}

Try to set first item like "Select one of..." - and add condition, when this is selected, for not to write into database.

Related

Xamarin Checkbox isn't "linking" properly to ObservableCollection in App.xaml.cs

I'm, having a Listview, with bunch of "Modules" in one ObservableCollection, which has been set as the ItemsSource. This collection has been set as:
public static ObservableCollection<Module> Modules { get; set; } in App.xaml.cs to make it global.
When I click the checkbox in the list, it will launch this function:
private void ModuleCheckbox_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
var checkbox = (CheckBox)sender;
var item = (Module)checkbox.BindingContext;
item.ModuleIsChecked = checkbox.IsChecked;
foreach(Module m in App.Modules)
{
Console.WriteLine(m.Name + " Value: " + m.ModuleIsChecked);
}
}
As you can see, I'm trying to set the property: ModuleIsChecked to be the same, as the checkbox, which in this case should be true or false. Now, when I click the checkbox, and the foreach-loop goes trough, the values are correct. When I go to the new "page" by using the
Navigation.PushAsync(new NewSite()); function and run the loop again, none of the Modules have ModuleIsChecked value as set before. How is this possible?
Module has the following attributes:
public string Description { get; set; }
public bool IsVisible { get; set; }
public bool ModuleIsChecked { get; set; }
public ObservableCollection<Question> QuestionList { get; set; }
edit as requested, here's the code at the new page, where I run through the same list just to see, if my selections have been "stuck" into that ObservableCollection:
public partial class NewSite : ContentPage
{
public Questions()
{
InitializeComponent();
BindingContext = new App();
foreach(Module m in App.Modules)
{
Console.WriteLine("Module: {0} ModuleisChecked: {1}", m.Name, m.ModuleIsChecked);
}
And here, the Console shows, that all of the values are False. Any ideas?
Thank you to Jason in the comments! (I don't know how to link you here, sorry!)
The problem lies in here:
BindingContext = new App();
As Jason said, that creates new instance of the App instead of using the existing one, which I just edited. Commenting this out and I was able to get this working as intended. Thank you!
Can be marked as solved, thanks!

Declaring the datasource of another formBox, by the currently selected listBox item

Apologies if this has been asked already,
I want to know how to declare the datasource of a textbox or a listbox by the currently selected listBox object.
So box 1 has the list of items, and each selection prints to the next box with more information about them (ItemInfo)
Here is my code that I'm working with:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != null)
{
listBox2.DataSource = ItemStorage._?_?_?_?_(listBox1.SelectedItem);
}
}
public class ItemObject
{
public string ItemName { get; set; }
public string ItemInfo { get; set; }
public ItemObject(string itemName, string itemInfo)
{
ItemName = itemName;
ItemInfo = itemInfo;
}
public override string ToString()
{
return ItemName;
}
}
The list name is ItemStorage and the element I want to print to the other box is ItemInfo.
I have found the answer I was looking for:
Instead of using a listBox, I opted for a richTextBox and this is the code I used:
if (listBox1.SelectedIndex != null)
{
try { var Information = ((ItemObject)listBox1.SelectedItem).ItemInfo;
richTextBox2.Text = Information;
}
catch( Exception ex)
{
}
}
This provided me with only getting the information from the selected item I wanted to have, so my listbox has a full total of all the items, while the selection only shows me the information from the one ive selected.

DropDownList selects top value only

I have a textbox that will be filled by a button click.
When i Click the button, it will compare some values and then copy the value from a dropdownlist it to the first textbox.
But the dropdownlist only copies the first value, even if i select a second or third value, it will always copy the first.
Here is my code:
protected void BtnOrderKoppelen_Click(object sender, EventArgs e)
{
if(DDLLijnRequest.Text == "Lijn1")
{
TbProd5.Text = DDLVrijgeefOrder.Text;
}
else if (DDLLijnRequest.Text == "Lijn2")
{
TbProd6.Text = DDLVrijgeefOrder.Text;
}
else if (DDLLijnRequest.Text == "Lijn3")
{
TbProd7.Text = DDLVrijgeefOrder.Text;
}
else if (DDLLijnRequest.Text == "Lijn4")
{
TbProd8.Text = DDLVrijgeefOrder.Text;
}
}
DDLLijnRequest= a dropdownlist for my production line comparison
TbProd(5 till 8) = are textboxes where data from DDLVrijgeefOrder has to go to
DDLVrijgeefOrder = a dropdownlist where i select an order and want it to copy to the textboxes.
Is there a way that it could copy any value that i have selected in DDLVrijgeefOrder?
This is my pageload, it has no connection yet with the button click or the dropdownlist. Do i have to add something here? :
protected void Page_Load(object sender, EventArgs e)
{
if (Session["MesLogin"] == null)
{
Response.Redirect("StartPagina.aspx");
}
else
{
LblSession.Text = "Welcome" + Session["MesLogin"].ToString();
}
}
Thank you in advance!
Hope you bind the drop-down value all the time in Page_load event. Drop down data source should be binded only in very first time otherwise it will always bind to first value.
Example:
Void Page_Load()
{
if(!IsNotPostBack)
{
// bind drop down values here
}
}
If the above solution doesn't help, please post some more code like Page_Load event
Based on the context of your question you are using WinForms right?
The following link provides a good answer to your question:
How to create a drop down menu in WinForms and C#
Note that you can add any type of item to the ComboBox. If you don't
specify the DisplayMember and ValueMember properties, the ComboBox
uses the ToString method of the object in order to determine the text
displayed and you can retrieve the selected item (not selected value)
through the SelectedItem property.
class Person
{
public int PersonID { get; set }
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return FirstName + " " + LastName;
}
}
Eventually you can do
Person selectedPerson = (Person)myComboBox.SelectedItem;
int personID = selectedPerson.PersonID;

Read the Value of an Item in a listbox

I'm developing a small windows store application in C# where I have populated the values and content for a listbox using the following code snippet.
code 1 : adds the song title as a item to a listbox, using the Song class to create the item
private void addTitles(string title, int value)
{
Song songItem = new Song();
songItem.Text = title;
songItem.Value = value;
listbox1.Items.Add(songItem); // adds the 'songItem' as an Item to listbox
}
code 2 : Song class which is used to set values to each item ('songItem')
public class Song
{
public string Text { get; set; }
public int Value { get; set; }
public override string ToString()
{
return Text;
}
}
Population content to the listbox is functioning currently.
What I want is to get the 'Value' of each item on a Click event, on run-time.
For that purpose, how can I read(extract) the Value of the selected Item in the listbox, in C#? ( Value is the songItem.Value )
code 3 : I have tried this code, as trying to figure out a solution, but it didn't work
private void listbox1_tapped(object sender, TappedRoutedEventArgs e)
{
Int selectedItemValue = listbox1.SelectedItem.Value();
}
Therefore it would be really grateful if someone can help me, as I'm an Amateur.
not sure about the "TappedRoutedEventArgs", but I would do
private void listbox1_tapped(object sender, TappedRoutedEventArgs e)
{
var selectedSong = (Song)listbox1.SelectedItem;
if (selectedSong != null) {
var val = selectedSong.Value;
}
}
because SelectedItem is an Object (which doesn't know about the Value property), so you have to cast it to a Song first.
By the way, Value is a property, not a method, so you don't need the parenthesis.
Try like this:
Song song=listbox1.SelectedItem as Song;
or this:
var selected = listbox1.SelectedValue as Song;
Try something like this :
if (listbox1.SelectedRows.Count>0){
Song song=(Song)listbox1.SelectedItem;
int value=song.Value;
}

Listbox always returns wrong value of selected item

I've got a listbox in C# that's dynamically filled. I've got a problem with the selectedindex_changed of the listbox. Everytime it gives the value of the last item in the listbox and not the one I selected. I've got that same problem with a combobox on another page.
I don't know why this happens this way. Does anyone knows what I'm doing wrong?
First I make a new object Item to put text with the ID as the value in the listbox.
public class Item
{
public string Text { get; set; }
public int Value { get; set; }
public override string ToString()
{
return Text;
}
}
Here I fill the listbox with the text and the ID as its value. This works fine, the listbox gets filled as it should be.
private void FormDeelnemers_Load(object sender, EventArgs e)
{
BLPersoon blPersoon = new BLPersoon();
DBOpdracht.PersoonDataTable personen = blPersoon.GetAllPersonen();
//Item item = new Item(); -> edit: delete this
foreach (DBOpdracht.PersoonRow persoon in personen)
{
Item item = new Item(); -> edit: add this here
item.Text = persoon.naam;
item.Value = persoon.ID;
listBoxPersonen.Items.Add(item);
}
}
Here is the problem. It gives the value of the last item in the listbox and not the one I've selected. How do I get the selected one?
private void listBoxPersonen_SelectedIndexChanged(object sender, EventArgs e)
{
int nummer = (listBoxPersonen.SelectedItem as Item).Value;
MessageBox.Show(nummer.ToString());
//MessageBox.Show(listBoxPersonen.SelectedItem.ToString()); -> same problem
}
Thanks to #bmused who solved it in the comments, but could anyone explain what happened exactly?
i had the same problem with a combobox where i was adding items using a foreach. the question is what does the instantiation of an item during the insertion have to do with selecting from a combobox?
P.S. Sorry admins couldn't post this as comment.

Categories

Resources