I'm currently trying to get access to information that is passed in through an object sender.
The application I am working on is a winforms application with a list view. I want to get the number of the ListViewItem that the user has pressed on. The ListView item that I have pressed on is correct when I debug.
However I am unaware of how to get the information I want from the object sender. I want to access the ListViewItem number,
look at the posted image ListViewItem: {24919} in this case
so I can use this number as index, when I search in a database.
Does anyone have a fast tip so I can continue with my program ?
private void InvoiceListView_SelectedIndexChanged(object sender, EventArgs e)
{
//Connect to db and search based on the the listviewItemnumber.
}
Currently the object sender containsmethod;
You can type-check the sender and work with the result:
private void InvoiceListView_SelectedIndexChanged(object sender, EventArgs e)
{
if (!(sender is ListView listView)) return;
//work with the listView object from here:
listView.Items = ...
}
You can get the item selected by casting the sender to ListView and then get corresponding value as given below:
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
ListView lw = (ListView)sender;
foreach(ListViewItem lvi in lw.SelectedItems )
{
MessageBox.Show(lvi.SubItems[0].Text);
}
}
Related
So i have been browsing around the great mind of Google but have not found a working solution to this; I have a listbox (instanceSelection) and a label (instanceTxt). I want the instanceTxt to be the same as the instanceSelection's text when I select an item in the collection.
Here is the code line that I thought would work earlier:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
instanceTxt.Text = (string)this.instanceSelection.SelectedValue.Text;
}
But in one time it didn't change, and another code block it changed to "0". I also sometimes get a null error when using "ToString".
Thanks,
William
Try this:
private void instanceSelection_SelectedIndexChanged(object sender, EventArgs e)
{
if(instanceSelection.SelectedIndex > -1)
instanceTxt.Text = instanceSelection.Items[instanceSelection.SelectedIndex].ToString();
}
I have two DropDownList with OnSelectedIndexChanged="SelectedIndexChanged" but I need to know in the C# code withch one is the one I used.
How can I know that?
Answering the questions:
I'm using Web Forms and I'm trying to change some GridViews Source from a choseen option in a DDL but the web had the same DDL (with different IDs) in several places and I can't delete them...
The general form for an event handler is:
OnSomeEvent(object sender, EventArgs e)
sender is a reference to the object that is raising the event.
In your case, sender is a reference to the DropDownList whose selected index has changed. So you should use something like this:
private void SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList the_list_that_changed = (DropDownList)sender;
int ids = the_list_that_changed.SelectedIndex;
}
The first parameter sender represents the object raising the event. Hence the Sender reference to your DropDownList whose triggered the selected index changed.
private void SelectedIndexChanged(object sender, EventArgs e)
{
if (((DropDownList)sender).ID == "firstDropDownID")
{
//To Do for first dropdown
}
else
{
//To Do for second dropdown
}
}
Hi in my clicked event I am able to get the details of the button clicked with this
private async void BedGridView_ItemClick(object sender, ItemClickEventArgs e)
{
CommonVariables.PatientDetailsDict["bed_number"] = (e.ClickedItem as BedModelV2).bed_number;
}
I was wondering if there is something similar for my holding event so I could get the information of the selected item. As the code below does not work with HoldingState or originalsource.
private async void BedGridView_Holding(object sender, HoldingRoutedEventArgs e)
{
CommonVariables.PatientDetailsDict["bed_number"] = (e.OriginalSource as BedModelV2).bed_number;
}
I'm guessing you're using WP8.1 because GridView is supported in WP8.
To answer you question you can always get the SelectedItem of your GridView my referencing it by name or converting the sender object in the holding event like so:
<!-- define the gridview -->
<GridView x:Name="myGV" Holding="myGV_Holding"></GridView>
private void myGV_Holding(object sender, HoldingRoutedEventArgs e)
{
var selectedItem = this.myGV.SelectedItem; // reference by name
// var selectedItem = (sender as GridView).SelectedItem; // reference by converting the sender
}
Once you got the .SelectedItem it will be an object based off your ViewModel so then you can convert it back like you did in your original code.
CommonVariables.PatientDetailsDict["bed_number"] = (selectedItem as BedModelV2).bed_number;
I would like to use drag and drop between two Listviews (AllListView and PreListView). This is how far I did get:
In the function where the AllListView is filled with Items, I use something like that to assosiate the myCustomDataObject to the single ListviewItem:
ListViewItem newItem = new ListViewItem();
newItem.Text = myCustomDataObject.getName();
newItem.Tag = myCustomDataObject;
lst_All.Items.Add(newItem);
There are my Eventhandler for the two Listviews:
AllListView:
private void OnAllDragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
// How Do I add my CustomDataObject?
}
private void OnAllItemDrag(object sender, ItemDragEventArgs e)
{
base.DoDragDrop(lst_All.SelectedItems[0], DragDropEffects.Move);
// Do I have to Do something to pass my CustomDataObject?
}
PreListView:
private void OnPreDragEnter(object sender, DragEventArgs e)
{
//If there one of myCustomDataObject go on
e.Effect = DragDropEffects.Move;
}
private void OnPreDragDrop(object sender, DragEventArgs e)
{
// Get Here myCustomDataObject to generate the new Item
lst_Pre.Items.Add("Done...");
}
So my question is, how to achieve that myCustomDataObject is found in “OnPreDragDrop”. I have tried many versions of e.Data.Getdata() and e.Data.Setdata(), but I did not got very far.
You are dragging an object of type ListViewItem. So you first want to check that the dragged item is indeed of that type. And you probably want to make sure it is a happy kind of item, one that has the proper Tag value. Thus:
private void OnPreDragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(typeof(ListViewItem))) {
var item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
if (item.Tag is CustomDataObject) {
e.Effect = DragDropEffects.Move;
}
}
}
In the Drop event, you actually want to implement the logical "Move" operation, removing the item from the source ListView and adding it to the destination ListView. No need for checks anymore, you already performed them in your DragEnter event handler. Thus:
private void OnPreDragDrop(object sender, DragEventArgs e) {
var item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
item.ListView.Items.Remove(item);
lst_Pre.Items.Add(item);
}
Note that you probably thought for a minute that the mistake was to drag the ListViewItem instead of the CustomDataObject. It was not, dragging ListViewItem made it easy to remove the item from the source ListView.
List view normally doesn't have drag and drop facility. But you can make it to do drag and drop with some changes with some extra code. Here is a link to help your problem. I hope you'll get something from it.
http://support.microsoft.com/kb/822483
When you call DoDragDrop, you are assigning the data. Make that your custom data object instead of the ListViewItem.
If you need the ListViewItem, then add a reference to it to your custom data class.
I am trying to produce a list in a listbox using C# in VS2012, the idea is to click on one of many picture boxes which contain images stored as resources, as the different boxes are clicked, I want the action event to add the name property as a list item in the ListBox.
I got it to add the string System.drawing.bitmap using :
private void PB1_Click( object sender, EventArgs e )
{
LB1.Items.Add(PB1.Image.ToString());
}
and
private void PB1_Click( object sender, EventArgs e )
{
LB1.Items.Add(Properties.Resources.wrist1.ToString());
}
But I can't seem to find an access to the Object property 'Name'. I tried wrist1.GetPropertyItem(Name) but it throws an error and will only accept an int.
any ideas anyone???
private void PB1_Click( object sender, EventArgs e )
{
LB1.Items.Add(this.Name);
}
And name each PictureBox properly. ?