I currently have a button object that starts a wav file playing, as well as a progress bar and a timer, all of which are toolbox objects in Visual Studio 2010. The problem is that, when I open the form, the progress bar automatically starts filling up. How can I get it to start only when I click on the button?
Here is the code I have so far:
private void playsong1_Click(object sender, EventArgs e)
{
SoundPlayer sndplayer = new SoundPlayer(Programming_assignment.Properties.Resources.Back_In_Black);
sndplayer.Play();
timer1.Enabled = true;
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value < 256)
{
progressBar1.Value++;
}
}
Looks like you have timer1.Enabled = true or timer1.Start() in form's constructor or Load event handler. BTW check timer's Enabled property in designer - maybe you set it to true by default.
Related
I have a NotifyIcon and I set balloon text with MouseMove event. The balloon text comes from a database. This results continuous database query.
private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
{
//database operations.......
}
How can I prevent this? I want to set balloon text once when mouse on NotifyIcon.
Use the BalloonTipShown event (https://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.balloontipshown(v=vs.110).aspx)
The behaviour you are looking for matched that event alot better then the MouseMove event
Another approach would be to add a Timer to your Form and set its Interval to a delay like 1 second. This delay would be how often the user could hit the database. Setup a Flag that gets reset by the Timer and check it in your NotifyIcon event. Something like:
private bool AllowUpdate = true;
private void notifyIcon1_MouseMove(object sender, MouseEventArgs e)
{
if (AllowUpdate)
{
AllowUpdate = false; // don't allow updates until after delay
// ... hit the database ...
// ... update your text ...
timer1.Start(); // don't allow updates until after delay
}
}
private void timer1_Tick(object sender, EventArgs e)
{
// reset so it can be updated again
AllowUpdate = true;
timer1.Stop();
}
When changing AxWindowsMediaPlayer URL in PlayStateChange Event, it doesn't start playing automatically, just changes to "Ready" state.
I have an "AxWindowsMediaPlayer" Control in my C# WinForms program. when I normally change the URL property of WindowsMediaPlayer1, it works fine and plays new mp3 file automatically.
When the song ended WindowsMediaPlayer1 State changes to Stopped and I Want next URL automatically start Playing.
I used PlayStatChange event, so when player state is Stopped, URL Will change, but Not playing automatically!
The player goes to Ready State until I press the play button on the WindowsMediaPlayer1.
Here is the Code:
private void Form1_Load(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "6.mp3"; //Works fine
}
private void button1_Click(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "4.mp3"; //Works fine. It changes the music.
}
private void WindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1) //1 is for "Stopped" State
WindowsMediaPlayer1.URL = "5.mp3";
// Here is the problem.
// URL Will change but player goes to "Ready" State
// But not in "playing" until I press the play button in control.
}
Any help would be appreciated.
As mentioned in media player documentations, you should not set the Url from event handler code. Instead you can play next file this way:
private void axWindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1)
{
this.BeginInvoke(new Action(() => {
this.axWindowsMediaPlayer1.URL = #"address of nextfile";
}));
}
}
Also as another option you can consider using a playlist.
I found this note on msdn about player.URL:
"Do not call this method from event handler code. Calling URL from an event handler may yield unexpected results."
so I tried another way to solve it and its worked.
added a timer and a bool varible to check if WindowsMediaPlayer1 is "Stopped"
Here is the solution:
public partial class Form1 : Form
{
bool nextURL = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "5.mp3";
}
private void WindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1) // 1 is consider for "Stopped" State
{
nextURL = true; // if the song ended "nextURL" flag sets to true
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (nextURL)
{
WindowsMediaPlayer1.URL = "6.mp3";
nextURL = false;
}
}
I cannot figure this out
i'm making a windows form application with visual basic in c#
i have a scan button and it scans everything in the folder and lists all of the files in the listbox
if you click it another time the list of files appear again
how can you make it so you can only press the scan button once, and then you can press it again if you click the browse button?
the browse button is to select the folder you want to scan
thanks
This is pretty trivial
private void ScanButtonClick(object sender, EventArgs e)
{
// do something
(sender as Button).Enabled = false;
}
private void BrowseButtonClick(object sender, EventArgs e)
{
ScanButton.Enabled = true;
}
Its a bit unclear if you're writing in C# or vb.net, but since the question is tagged as C#...
private void btnScan_Click(object sender, EventArgs e) {
btnScan.Enabled = false;
// other code here
}
private void btnBrowse_Click(object sender, EventArgs e) {
btnScan.Enabled = true;
//other code here
}
I tried this in my windows form application in C# and it works fine!
private void button3_Click_1(object sender, EventArgs e)
{
int count = 0;
count++;
//add your code here
if (count == 1) {
button3.Enabled = false;
//only one click allowed
}
}
C# question
Here is what I am trying to do. When I click a button, I want the checkboxlist to smoothly change from say (200,10) to (200,100) in size. I am successful at getting to size to change instantaneously, but I want it to look smooth.
Here is the code I wrote:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (checkedListBox1.Height < 100)
{
checkedListBox1.Size = new Size(checkedListBox1.Size.Width, checkedListBox1.Size.Height + 1);
}
else
{
timer1.Enabled = false;
}
}
I have used this coding to move objects smoothly, but never to change sizes.
So when you run this code, the box just flickers and it seems like its trying to change size, but it doesn't, and the loop never ends.
Thanks!
You need to set IntegralHeight to false to that the box can be a height that is not a multiple of the item height.
For the flickering, you should probably double-buffer the form which contains this control.
I have the following code-
private void button1_Click(object sender, EventArgs e)
{
_soundplayer.Play();
timer1_Tick();
}
private void timer1_Tick()
{
pictureBox1.Image = imageList1.Images[imgIndex++];
}
For some reason this brings back the error in the Form1.Designer.cs -
Error 1 No overload for 'timer1_Tick' matches delegate 'System.EventHandler'
When button1 is clicked the image in pictureBox1 should change every 2 seconds with the timer tick, however I can't get past this error. Please advise.
The Tick event is an event of type EventHandler. It requires two arguments for the event handler:
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Image = imageList1.Images[imgIndex++];
}
Which requires you to modify the Click event handler like this:
private void button1_Click(object sender, EventArgs e)
{
_soundplayer.Play();
timer1_Tick(this, EventArgs.Empty);
}
Using the designer to add event handlers can keep you out of trouble like this. Select the timer, click the lightning bolt icon in the Properties window and double-click Tick.
Start timer, when you clicked on a button. And set timer interval to 2000 milliseconds. Timer_tick event will be created automatically every 2 seconds.
private void timer1_Tick()
{
pictureBox1.Image = imageList1.Images[imgIndex++];
}
private void button1_Click(object sender, EventArgs e)
{
_soundplayer.Play();
timer1_Start();
}