I need to focus TextCells one by one via a button click.
I tried listView.ScrollTo.
private void Button_Clicked_1(object sender, EventArgs e)
{
listViewJson.ItemTapped += ListViewJson_ItemTapped;
}
private void ListViewJson_ItemTapped(object sender, ItemTappedEventArgs e)
{
var focusing = e.Item;
listViewJson.ScrollTo(focusing, ScrollToPosition.MakeVisible, true);
}
Firstly, try to define an index to detect which cell to be selected. Then change the index via button click like:
int selectedIndex = 0;
private void MyBtn_Clicked(object sender, EventArgs e)
{
if (selectedIndex == dataList.Count) selectedIndex = 0;
myListView.SelectedItem = dataList[selectedIndex++];
}
when the TextCell is selected the ListView's ItemSelected event will fire, you can put your code in it like:
private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//try to do something
}
Here is my code behind for you referring to:
ObservableCollection<string> dataList = new ObservableCollection<string>();
int selectedIndex = 0;
public MainPage()
{
InitializeComponent();
for (int i=0; i<10; i++)
{
dataList.Add("item" + i);
}
myListView.ItemsSource = dataList;
myListView.ItemSelected += MyListView_ItemSelected;
MyBtn.Clicked += MyBtn_Clicked;
}
private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//try to do something
}
private void MyBtn_Clicked(object sender, EventArgs e)
{
if (selectedIndex == dataList.Count) selectedIndex = 0;
myListView.SelectedItem = dataList[selectedIndex++];
}
Related
after clicking my value and pressing my OK_button I cant get the Value out of the listView to save it somewhere else. I cant use listView1.FindItemWithText because I don't have a text to search for.. Idk how to look for the clicked value after I pressed the OK_button
//Create dummy data to display
myData = new string[dataListSize];
for (int i = 0; i < dataListSize; i++)
{
myData[i] = String.Format("{0}", i);
}
}
private void listView1_SearchForVirtualItem(object sender, SearchForVirtualItemEventArgs e)
{
e.Index = Array.FindIndex(myData, s => s == textBox1.Text.ToString());
}
private void listView1_RetrieveVirtualItem(object sender, RetrieveVirtualItemEventArgs e)
{
e.Item = new ListViewItem(myData[e.ItemIndex]);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
String MyString = textBox1.Text.ToString();
ListViewItem lvi = listView1.FindItemWithText(MyString.TrimEnd());
//Select the item found and scroll it into view.
if (lvi != null)
{
listView1.SelectedIndices.Clear();
listView1.SelectedIndices.Add(lvi.Index);
listView1.EnsureVisible(lvi.Index);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e){ }
private void OK_button_Click(object sender, EventArgs e)
{
try
{
// OK -> Daten übernehmen
int iCount = this.listView1.SelectedIndices.Count;
if (iCount != 1)
{
MessageBox.Show("Value is empty");
return;
}
DialogResult = DialogResult.OK;
Close();
}
catch (Exception)
{
//WriteProtokoll(ex.ToString(), 0);
Close();
}
}
I found out that I can get my value with:
string txt = listView1.FocusedItem.Text;
I try to switch panels on tabControls on UserControl.
Like this
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible=true;
panel2.Visible=false;
.....
panelN.Visible=false;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible=false;
panel2.Visible=true;
.....
panelN.Visible=false;
}
private void buttonN_Click(object sender, EventArgs e)
{
panel1.Visible=false;
panel2.Visible=false;
.....
panelN.Visible=true;
}
but when N exceeds 6, switching panels doesn't work well.
Some panels don't visible ,even if the Button event occurs!
So could you tell me how to switch multi panels.
If possible, could you tell me the smart way to switch panels.
The above code seems to be bad readability.
You could make all of the buttons fire the same handler, then do something like this:
private void btn_Clicked(object sender, EventArgs e)
{
Button btn = (Button)sender;
int numButtons = 6;
int index = int.Parse(btn.Name.Replace("button", ""));
for(int i=1; i<=numButtons; i++)
{
Control ctl = this.Controls.Find("panel" + i, true).FirstOrDefault();
if (ctl != null)
{
ctl.Visible = (i == index);
if (i == index)
{
ctl.BringToFront();
}
}
}
}
I have a probeleme with my code. I want have the same select between two listbox, but i have stackoverflow error, i understand my error but I don't find a solution. Someone can help me ?
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int i = 0;
while(i<listBox1.Items.Count)
{
listBox2.SetSelected(i, listBox1.GetSelected(i));
i++;
}
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
int i = 0;
while (i < listBox2.Items.Count)
{
listBox1.SetSelected(i, listBox2.GetSelected(i));
i++;
}
}
You could remove the event handler before setting the selected indexes and then add the event handler again.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int i = 0;
listBox2.SelectedIndexChanged -= listBox2_SelectedIndexChanged;
while (i < listBox1.Items.Count)
{
listBox2.SetSelected(i, listBox1.GetSelected(i));
i++;
}
listBox2.SelectedIndexChanged += listBox2_SelectedIndexChanged;
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
int i = 0;
listBox1.SelectedIndexChanged -= listBox1_SelectedIndexChanged;
while (i < listBox2.Items.Count)
{
listBox1.SetSelected(i, listBox2.GetSelected(i));
i++;
}
listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
}
I have a ListBox in a WPF application that has a MouseMove event handler attached. What I would like to do is to use this event to get the index of the item the mouse is over.
Simplified example of my code:
<StackPanel>
<ListBox x:Name="MyList" MouseMove="OnMouseMove"/>
<Separator/>
<Button>Beep</Button>
</StackPanel>
public CodeBehindConstructor()
{
List<string> list = new List<string>();
list.Add("Hello");
list.Add("World");
list.Add("World"); //Added because my data does have duplicates like this
MyList.ItemsSource = list;
}
public void OnMouseMove(object sender, MouseEventArgs e)
{
//Code to find the item the mouse is over
}
I would try using ViusalHelper HitTest method for that, something like this :
private void listBox_MouseMove(object sender, MouseEventArgs e)
{
var item = VisualTreeHelper.HitTest(listBox, Mouse.GetPosition(listBox)).VisualHit;
// find ListViewItem (or null)
while (item != null && !(item is ListBoxItem))
item = VisualTreeHelper.GetParent(item);
if (item != null)
{
int i = listBox.Items.IndexOf(((ListBoxItem)item).DataContext);
label.Content = string.Format("I'm on item {0}", i);
}
}
Try this:
public void OnMouseMove(object sender, MouseEventArgs e)
{
int currentindex;
var result = sender as ListBoxItem;
for (int i = 0; i < lb.Items.Count; i++)
{
if ((MyList.Items[i] as ListBoxItem).Content.ToString().Equals(result.Content.ToString()))
{
currentindex = i;
break;
}
}
}
You can also try this much shorter option:
public void OnMouseMove(object sender, MouseEventArgs e)
{
int currentindex = MyList.Items.IndexOf(sender) ;
}
However I'm not too sure whether it will work with your method of binding.
Option 3:
A little hacky but you could get the point value of the current location and then use IndexFromPoint
E.g:
public void OnMouseMove(object sender, MouseEventArgs e)
{
//Create a variable to hold the Point value of the current Location
Point pt = new Point(e.Location);
//Retrieve the index of the ListBox item at the current location.
int CurrentItemIndex = lstPosts.IndexFromPoint(pt);
}
I have a DataGrid and two up and down buttons so that the user can scroll through each datagrid item on button click; either up or down. How do I go about doing this? I have tried SelectedIndex but doesn't seem to work for DataGrid.
ExampleCode:
private void btnDownCheckedMedication_Click(object sender, RoutedEventArgs e)
{
if (MedicationCheckedInDatagrid.SelectedIndex > 0)
{
MedicationCheckedInDatagrid.SelectedIndex = MedicationCheckedInDatagrid.SelectedIndex - 1;
}
}
private void btnUpCheckedMedication_Click(object sender, RoutedEventArgs e)
{
MedicationCheckedInDatagrid.SelectedIndex = MedicationCheckedInDatagrid.SelectedIndex + 1;
}
You nearly had it... use SelectedItem instead:
private void btnDownCheckedMedication_Click( object sender, RoutedEventArgs e )
{
if(dataGrid.SelectedIndex > 0 ) {
dataGrid.SelectedItem = dataGrid.Items[dataGrid.SelectedIndex - 1];
}
}
private void btnUpCheckedMedication_Click( object sender, RoutedEventArgs e )
{
if(dataGrid.SelectedIndex < dataGrid.Items.Count - 1) {
dataGrid.SelectedItem = dataGrid.Items[dataGrid.SelectedIndex + 1];
}
}