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;
}
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 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++];
}
I want to run a progress bar on a form through the use of a timer.
I have tried multiple ways and have not been able to get it to work.
I hope someone here can help me with this.
private void SplashScreen_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 1000;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
}
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
you are assigning event_handler like
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
and you are changing the progressBar value in
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
change event handler to
splashScreenTimer.Tick += new EventHandler(timer_Tick);
or move codes to the other event handler timer1_Tick which should be in your form
For running the progressBar full in 4 seconds you can do like this
private void Form1_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 30;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer_Tick);
}
int waitingTime = 0;
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value < 100)
{
progressBar.Value++;
}
else
{
if (waitingTime++ > 35)
this.Close();
}
}
I am making a windows app.
A button1 which gets the items in listBox1 from server at the start.
A button2 which starts the timer1.
A timer1 which removes items from listBox1 .
A progressBar1 which shows the progress of this process.
Here is the code:
private void button1_Click(object sender, EventArgs e)
{
jabber.Send("<iq type='get' to='" + textBox1.Text + "#conference.jabber.com' id='qip_1026'><query xmlns='http://jabber.org/protocol/muc#admin'><item affiliation='outcast' /></query></iq>");
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Maximum = listBox1.Items.Count;
timer1.Start();
timer1.Interval = 4000;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text + "#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
The above code works well till there is one item left in listBox1.
The error is:
System.ArgumentOutOfRangeException was unhandled
Message=InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
It raises an error when listBox1 reaches 0. I want to stop the timer when listbox1 is empty or gets no items or 0 items.
The problem is in this code:
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text + "#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
So what is happening is that you are using count to check >0 then calling jabber to do the work, It the call becomes slow- you will see multiple timers getting fired back. So a big queue will be collected there. You need to modify the code a bit here using lock to hold the list and allow jabber to do its work:
private void timer1_Tick(object sender, EventArgs e)
{
lock (listBox1)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text +
"#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" +
listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
}
Lock will also ensure that the items are removed correctly.
To save the file as per comment below :
public class ChatHistoryManager
{
private readonly RichTextBox richTextBox;
private Timer timer = new Timer();
public ChatHistoryManager(RichTextBox richTextBox)
{
this.richTextBox = richTextBox;
this.InitializeTimer();
}
public string Location { get; set; }
private void InitializeTimer()
{
this.timer.Tick += timer_Tick;
this.timer.Enabled = true;
this.timer.Interval = (int) TimeSpan.FromHours(1).TotalMilliseconds;
}
void timer_Tick(object sender, EventArgs e)
{
this.SaveFile();
}
public void SaveFile()
{
//Save the file to the location
this.richTextBox.SaveFile(this.Location,RichTextBoxStreamType.RichText);
}
public void Stop()
{
this.timer.Stop();
}
}
Now we need to set in form like this:
private void Form1_Load(object sender, EventArgs e)
{
ChatHistoryManager chatHistoryManager = new ChatHistoryManager(this.richTextBox1);
chatHistoryManager.Location = #"C:\Development\test.txt";
}
try this
int count = City.Items.Count - 1;
for (int i = count; i > 0; i--){
City.Items.RemoveAt(i);
}
Here is what worked for me.
private void button1_Click(object sender, EventArgs e)
{
jabber.Send("<iq type="get" to="" + textBox1.Text + "#conference.jabber.com" id="qip_1026"><query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="outcast" /></query></iq>");
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
// Set the timer interval to four seconds
timer1.Interval = 4000;
// Start the timer
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
// Disable the timer while we are working in this procedure so
// it doesn't tick while we are working in this procedure
timer1.Enabled = False;
// Send only if there are items in the ListBox
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type="set" to="" + textBox7.Text + "#conference.jabber.com"><query xmlns="http://jabber.org/protocol/muc#admin"><item jid="" + listBox1.Items[0].ToString() + "" affiliation="none" /></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
// Re-enable only if there are items left in the ListBox
if (listBox1.Items.Count > 0)
{
timer1.Enabled = True;
}
}
I am working on a simple mediaplayer application. It works great but I want to add some extra features. I have added a trackbar control.How can i set trackbar length the same as the music's length ?
Like if the song is halfways the trackbars halfways.This is what I have so far
string[] files, indexed_files;
private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == DialogResult.OK) {
files = ofd.SafeFileNames;
indexed_files = ofd.FileNames;
for (int i = 0; i < files.Length; i++)
{
listBox1.Items.Add(files[i]);
}
}
button4.Enabled = true;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = indexed_files[listBox1.SelectedIndex];
progressBar1.Maximum =(int) axWindowsMediaPlayer1.currentMedia.duration;
axWindowsMediaPlayer1.PlayStateChange += axWindowsMediaPlayer1_PlayStateChange;
}
void axWindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
trackBar1.Value = (int)axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
}
int index = 0;
private void button4_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count != 0) {
axWindowsMediaPlayer1.URL = indexed_files[index];
trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
index++;
index = (index % listBox1.Items.Count);
}
}
This will bring you the desired outcome.In my example i just placed the url in the form load for demonstration purposes.The openstatechanged event its to set the trackbar maximum since you need to wait for the player to load the file,after that the code its pretty self-explanatory:
public partial class Form1 : Form
{
Timer t;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = "YourUrlHere";
t = new Timer();
t.Interval = 1000;
t.Tick += new EventHandler(t_Tick);
}
void t_Tick(object sender, EventArgs e)
{
trackBar1.Value = (int)this.axWindowsMediaPlayer1.Ctlcontrols.currentPosition;
}
private void axWindowsMediaPlayer1_OpenStateChange(object sender, AxWMPLib._WMPOCXEvents_OpenStateChangeEvent e)
{
if (axWindowsMediaPlayer1.openState == WMPLib.WMPOpenState.wmposMediaOpen)
{
trackBar1.Maximum = (int)axWindowsMediaPlayer1.currentMedia.duration;
t.Start();
}
}
}
Yes its a timer:),and probably it is best to set it bellow 1000 for reasons of delay.
So you should now add a timer and insert the following code in timer Tick event handler:
trackbar.Value = this.axWindowsMediaPlayer1.ctlControls.CurrentPosition;