Update selectedindices based on textbox change - c#

I've got a form that contains a listview which pulls in ticket info from a database. The database objects are all abstracted into a class library. There is a tabpage below the listview which displays various details of the tickets.
My problem is that I've implemented a search at the top of this form which isn't updating that tabpage, only the listview gets updated. After typing in keywords the listview refreshes properly and any items that dont' contain the keywords are removed until the text from the search box is cleared. But I can't get the tabpage to exhibit the same behavior. The tabpage still always contains all tickets.
For example, if I were to search for something where only 1 ticket was returned in the listview and say that ticket was the 10th ticket on record; the tabpage would show me details for the very first ticket. How can I get the tabpage to exhibit the same behavior as my listview after a search is made?
The tabpage currently gets filled with this function:
private void FillTicket()
{
try
{
if (listView1.SelectedIndices.Count > 0)
{
CTicket thistkt = comp.Tickets[listView1.SelectedIndices[0]];
dedit1.DocumentHTML = thistkt.LineItems.GetCombinedProblem();
dedit2.DocumentHTML = thistkt.LineItems.GetCombinedResolution();
lvAssignmentHistory.Items.Clear();
foreach (CInc_AssignmentHistory a in thistkt.AssignmentHistory)
{
ListViewItem itm = new ListViewItem();
itm.Text = a.pAsgn_Datetime.ToString();
itm.SubItems.Add(a.pAsgn_Group_fr);
itm.SubItems.Add(a.pAsgn_from);
itm.SubItems.Add(a.pAsgn_Group_to);
itm.SubItems.Add(a.pAsgn_to);
itm.SubItems.Add(a.pChanged_By);
lvAssignmentHistory.Items.Add(itm);
}
when this is called:
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//FillTicket();
if (txtBox_TicketSearch.Text != "")
{
FillTicketNothing();
}
else
{
FillTicket();
}

It seems to me you only update the tab page if the user selects different items in your ListView.
If your listView1_SelectedIndexChanged method is only a handler for the ListView.SelectedIndexChanged event, it is only called when the selection in listView1 changes, not when it's content is changed (without changing selection).
So you should call FillTicket when you changed the content of listView1.Items after your search.
Also, your FillTicket method only updates the tabpage if there are selected items in your ListView:
if (listView1.SelectedIndices.Count > 0)
I don't know if there is an else-branch for that if. If not, there will nothing change on your tab page if no items were selected. You may want to use listView1.Items.Count.

Related

C# combobox takes the control of whole form

I have a form with some textboxes and some other items specially combobox.
When I selected an item from combobox that has label "Land" in screenshot, I can't do anything with other elements in form!
that combobox turns blue (means selected and focused) and user cant fill other boxes!
private void cb_land_DropDownClosed(object sender, EventArgs e)
{
foreach (Lands j in lands)
{
if (j.landName.Equals(cb_land.Text))
{
tb_countrykz.Text = j.landKZ;
break;
}
}
}
code for fill combobox from database :
this.länderTableAdapter.Fill(this.test_tab.länder);
I have 3 other combobox in my form and they work fine.
this combobox locks other boxes and does not let user to click on any other elements in form.

Which Item is user focused in listview xamarin forms

I am a student learning xamarin forms, I am trying to create a basic chat app in this I want to know how to get position of current item in listview that's user watching. When a new message received i want to know if user is at bottom or not if at bottom focus the new and if not at the bottom then just add not by adding focus to it.
you get the selected item from the Xamarin.Forms.ListView.SelectedItem property of your ListView.
If your ListView.ItemSource is of a type that allows using IndexOf you can now do something like
int position = (yourlistview.ItemSource as ObservableCollection<your type>).IndexOf(yourlistview.SelectedItem)
Update:
ok I think i understood what you want.
In most cases more than one item is currently shown when using a listview. So their exists not a single index
but i think you just want to know if the last item of the list is visible/the user has scrolled to the end?
If so ListView has an ItemAppearing event. I use it for example to load more data from an websource if the user scrolled through the first 100 items.
You could do something like this
listview.ItemAppearing += listviewItemAppearing;
listview.ItemDisappearing += listviewItemDisappearing;
bool m_scrolledToEnd;
private void listviewItemDisappearing(object sender, ItemVisibilityEventArgs e)
{
if(e.Item == yourlastiem)
m_scrolledToEnd = false;
}
private void listviewItemAppearing(object sender, ItemVisibilityEventArgs e)
{
if(e.Item == yourlastiem)
m_scrolledToEnd = true;
}
if you realy need to know if a specific index is shown you could create a List<int> m_idxlist;
and in the appearing event add the index of the item to the list
and in the disappearing event remove the index of the item from the list.
Then you will have a list where all indexes of the items currently shown are stored.
From the Documentation
ListView supports selection of one item at a time. Selection is on by
default. When a user taps an item, two events are fired: ItemTapped
and ItemSelected. Note that tapping the same item twice will not fire
multiple ItemSelected events, but will fire multiple ItemTapped
events. Also note that ItemSelected will be called if an item is
deselected.
To detect selecting an item, you can add a method, onSelection:
void OnSelection (object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null) {
return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
}
DisplayAlert ("Item Selected", e.SelectedItem.ToString (), "Ok");
//((ListView)sender).SelectedItem = null; //uncomment line if you want to disable the visual selection state.
}
To disable selection just set the selectedItem to null:
SelectionDemoList.ItemSelected += (sender, e) => {
((ListView)sender).SelectedItem = null;
};

How access to selected items of a ListView from another class? in WPF C#

I have two views of WPF (Vista1.xaml and Vista2.xaml), and they are part of a MainWindow.xaml.
In the Vista1.xaml i have a listview, where the user selects some items by clicking and the Vista2.xaml has a button.
I want that when the user clicks on the button of Vista2.xaml, get the items that the user previously selected in the listview of Vista1.xaml.
I have class in the Vista1.xaml.cs ListViewItem_PreviewMouseLeftButtonDown method that captures the user selects the item in the listview.
the code is as follows
private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var item = sender as ListViewItem;
if (item != null && item.IsSelected)
{
...
}
}
I appreciate your help
Add a public property to you MainPage that contains the data you want to pass between Vista1 and Vista2. Then, in your event handler of Vista1, set the property and on the button click of Vista2 read the property.

Deselect combobox item

I have a databound combobox:
using(DataContext db = new DataContext())
{
var ds = db.Managers.Select(q=> new { q.ManagerName, q.ManagerID});
cmbbx_Managers.BindingContext = new BindingContext();
cmbbx_Managers.DataSource = ds;
cmbbx_Managers.DisplayMember = "ManagerName";
cmbbx_Managers.ValueMember = "ManagerID";
}
When the form loads neither item is selected, but when the user chooses an item it cannot be deselected. I tried to add cmbbx_Managers.items.Insert(0, "none"), but it does not solve the problem, because it is impossible to add a new item to the databound combobox.
How do I allow a user to deselect a combobox item?
To add an item to your databound ComboBox, you need to add your item to your list which is being bound to your ComboBox.
var managers = managerRepository.GetAll();
managers.Insert(0, new Manager() { ManagerID = 0, ManagerName = "(None)");
managersComboBox.DisplayMember = "ManagerName";
managersComboBox.ValueMember = "ManagerID";
managersComboBox.DataSource = managers;
So, to deselect, you now simply need to set the ComboBox.SelectedIndex = 0, or else, use the BindingSource.CurrencyManager.
Also, one needs to set the DataSource property in last line per this precision brought to us by #RamonAroujo from his comment. I updated my answer accordingly.
The way you "deselect" an item in a drop-down ComboBox is by selecting a different item.
There is no "deselect" option for a ComboBox—something always has to be selected. If you want to simulate the behavior where nothing is selected, you'll need to add a <none> item (or equivalent) to the ComboBox. The user can then select this option when they want to "deselect".
It is poor design that, by default, a ComboBox appears without any item selected, since the user can never recreate that state. You should never allow this to happen. In the control's (or parent form's) initializer, always set the ComboBox to a default value.
If you really need a widget that allows clearing the current selection, then you should use a ListView or ListBox control instead.
To deselect an item suppose the user presses the Esc key, you could subscribe to the comboxBox KeyDown event and set selected index to none.
private void cmbbx_Managers_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape && !this.cmbbx_Managers.DroppedDown)
{
this.cmbbx_Managers.SelectedIndex = -1;
}
}

ASP .Net : Filter ListView items dynamically based on user input

I have a ListView with DataPager control. I'm trying to display a series of questions.Each question along with it's choices belongs to an item in the listview and I am displaying one question per page. The choices are radio buttons with autopostback enabled.
Now, based on the user's response to the first question I want to filter the rest of the questions in the listview to display only the ones relevant to the user's choice. Also, I don't want to completely remove the other questions from the listview because if the user navigates to the first question and changes his response, I want the list to change accordingly. Is there a way to dynamically hide the pages that contain the irrelevant questions?
Thanks.
Note: The list items are populated by a custom sql query:
SELECT questionNumber,questionText,OptionA,OptionB,OptionC,OptionD,OptionE,OptionF,OptionG,OptionH,OptionI,OptionJ,OptionK,OptionL,Other,questionTag,subType from Questionairre
In this query the value present in the field "subType" should be compared with the choice made in the first question.. The subtype field for the first question will not be checked. The following code is what I have so far. The variable "firstChoice" is the static variable that stores the value postedback from the first question.
protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
ListViewDataItem di = (ListViewDataItem)e.Item;
Label q=new Label();
foreach(Control control in di.Controls)
{
if (control.GetType() == typeof(Label))
{
q = (Label)control;
if (q.ID == "subType" && q.Text != firstChoice)
{
di.Visible = false;
}
}
}
}

Categories