Im using WMPLib to make an easy mp3player in C#. Im almost done but theres one more thing I want to do.
I Would like to how far gone the song is and also, how much is left of the song.
using for example the progressbar.
thanks
Adam
private void timer1_Tick(object sender, EventArgs e)
{
double percent = 0;
if (mp.Length != 0)
percent = ((double) wplayer.controls.currentPosition / wplayer.controls.currentItem.duration);
progressBar1.Value = (int)(percent * progressBar1.Maximum);
}
I have an Idea , just try to add statusStrip to your Project Form , and try to add a ToolStripStatusLabel and ToolStripProgressBar to it, and then you can use this simple code , it Works 100% :
public void Sound_Progress(ToolStripStatusLabel l1, ToolStripProgressBar psb)
{
//NASSIM LOUCHANI
int i = Convert.ToInt32(Player.controls.currentItem.duration);
int j = Convert.ToInt32(Player.controls.currentPosition);
int Defrence = (i-j);
l1.Text = Player.controls.currentPositionString + " | " + Player.controls.currentItem.durationString;
psb.Maximum = i;
psb.Minimum = 0;
if (Defrence == i)
psb.Value = i;
else if (Defrence != i)
psb.Value = Defrence;
else if (Defrence == 0)
l1.Text = "";
}
And don't forget to add a Timer to your Project Form and put the Sound_Progress(your ToolStripStatusLabel, your ToolStripProgressBar) into your Timer_Tick() Event .
Thank you !
Related
im making a project for university, for which i use windows forms. i have to create a program, where you can generate an several amount of textboxes and calculate all values. the calcutation is about the sum and average of every value combined. therefore i have two buttons, one to create the textboxes and another one to do the calculation (doTheMath_Click).
heres my code so far:
button for the calculation:
private void doTheMath_Click(object sender, EventArgs e)
{
int radius = int.Parse(numberofnumbers.Text)
int sum = 0;
for (int i = 0; i < zahlen.Length; i++)
{
sum += numbers[i];
}
double average = (double)sum / (double)radius;
total.Text = sum.ToString();
averagee.Text = average.ToString();;
}
int newtextboxn = 8;
int alingment = 200;
public TextBox addnewtextbox()
{
TextBox textbox = new TextBox();
this.Controls.Add(textbox);
if (newtextboxn % 18 == 0)
{
alingment += 200;
newtextboxn = 8;
textbox.Top = newtextboxn * 27;
textbox.Left = alingment;
}
else
{
textbox.Top = newtextboxn * 27;
textbox.Left = alingment;
}
newtextboxn = newtextboxn + 1;
return textbox;
}
button to print the textbox:
public void printTextbox(int radius)
{
for (int i = 0; i < radius; i++)
{
addnewtextbox();
}
}
private void printTheBox_Click(object sender, EventArgs e)
{
int radius = int.Parse(numberofnumbers.Text);
printTextboxandLabels(radius);
}
I had the idea to save the values of the textboxes in an array, but i dont know if it would work, because the array length should also be dynamically and i also dont know, how to initialize the array in the end. My other idea was to save the values in a list, but theres the same problem about the initialization.
i hope, that my problem is understandable and that you can help me.
i already surfed around stackoverflow, but i didnt found an idea to solve my problem.
thx
Just create a List where you store the created set of textboxes. Then you can use the code to loop over this list and retrieve the content of each textbox.
List<TextBox> textboxslist = new List<TextBox>();
and inside addnewtextbox() method fill textboxlist using The Add method.
textboxlist.Add(textbox);
then inside loop code in doTheMath_Click button you can access the values of each textbox by using Text Property.
for (int i = 0; i < textboxlist.Count; i++) {
int textboxvalue = int.Parse(textboxlist[i].Text);
sum += textboxvalue;
}
I need to add a custom seekbar / trackbar as you may say in c# windows forms. But the issue is there is almost no documentation on vlc dot net forms library. I need to know how to add a custom seekbar in windows forms application.
remember, i am not using vlc activeX plugin*. **Rather i am using nuget package of dot net library of vlc and everything is working fine. I have added toggle play and pause button, stop button, able to get current time, able to get total time and everything else. But i have no idea how to add a seekbar so that when i seek, the video moves to that position. Please help me with full code.
I successfully finish, thank you it was a good practice for me. I've added the media in formdeneme() method
You have to make public the object which is in VlcControl.cs class.(private VlcMediaPlayer myVlcMediaPlayer;){Very important}
public int a = 0 ;`
public int c = 0;
public delegate void UpdateControlsDelegate(); //Execute when video loads
public formdeneme()
{
InitializeComponent();
myVlcControl.Play("file:///C:/Users/1315k/Downloads/machine.mp4");
// You can add your media like above.
//Event handler for 'current media time' label
this.vlcControl1.PositionChanged += new System.EventHandler<Vlc.DotNet.Core.VlcMediaPlayerPositionChangedEventArgs>(this.vlcControl1_PositionChanged);
//Event handler for setting trackBar1.Maximum on media load
vlcControl1.Playing += new System.EventHandler<VlcMediaPlayerPlayingEventArgs>(SetProgresMax);
}
// This is the main function which you looking.
private void trackBar1_Scroll(object sender, EventArgs e)
{
myVlcControl.myVlcMediaPlayer.Time = trackBar1.Value * 1000;
int b = (int)myVlcControl.myVlcMediaPlayer.Time / 1000;
int d = b / 60;
b = b - d * 60;
label1.Text = d+":"+b + "/"+ c + ":" + a;
// The Time value is milisecond, you have divide 1000 for be second.
}
private void formdeneme_Load(object sender, EventArgs e)
{
a = (int)myVlcControl.myVlcMediaPlayer.Length / 1000;
trackBar1.Maximum = a;
c = a / 60;
a = a - c * 60;
label1.Text = 0 + "/" + c+":"+a;
}
You can add a button that can change media and trackbar.Maximum value.
UPDATED
Thanks to askepott
He added some codes below, I didn't try but Looks good to me.
In order to have a label that displays the current media time, add this delegate function, it's called function (currentTrackTime) below and declaration at the top of this post. Also, don't forget to add the vlcControl1_PositionChanged event handler at the top.
//Update current video time label (delegate)
public void InvokeUpdateControls()
{
if (this.InvokeRequired)
{
this.Invoke(new UpdateControlsDelegate(currentTrackTime));
}
else
{
currentTrackTime();
}
}
//Update current video time label
private void currentTrackTime()
{
int b = (int)vlcControl1.VlcMediaPlayer.Time / 1000;
int d = b / 60;
b = b - d * 60;
label1.Text = d+":"+b + "/"+ c + ":" + a; //min : sec /
}
//Add this to currentTrackTime() if you want your trackbar to automatically update it's value based on current media position
trackBar1.Value = b;
//Invoke update controls on video position change
private void vlcControl1_PositionChanged(object sender, Vlc.DotNet.Core.VlcMediaPlayerPositionChangedEventArgs e)
{
InvokeUpdateControls();
}
//Furthermore, in case you have trouble getting and setting the vlcControl1.Length when loading the video, use this:
//Fire event when the video starts
private void SetProgresMax(object sender, VlcMediaPlayerPlayingEventArgs e)
{
Invoke(new Action(() =>
{
trackBar1.Value = trackBar1.Minimum;
var vlc = (VlcControl)sender;
trackBar1.Maximum = (int)vlc.Length / 1000;
a = (int)vlc.Length / 1000; // Length (s)
c = a / 60; // Length (m)
a = a % 60; // Length (s)
label1.Text = 0 + "/" + c+":"+a;
}));
can someone tell me why I can't run my program? I'm trying to create doughts and crosses in windows form applications I tried changing the code and stuff but I've tried everything but I think something is wrong with my function at the bottom. By now, I wanted to program to run, generate 9 buttons and when I click on them an "X" or "O" would appear depending whose turn is it.
PS. I haven't added the win condition function yet I wanted to test if the program is working as it should.
Thanks in advance.
public partial class Form1 : Form
{
Button[] gameButtons = new Button[9]; //array of buttons for markers(X's and O's)
bool cross = true; //cross is set to true if the next marker is to be a cross
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "More Complex Version of Noughts and Crosses";
this.BackColor = Color.BlanchedAlmond;
this.Width = 400;
this.Height = 400;
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 3) * 50;
int y = 50 + (i / 3) * 50;
this.gameButtons[i].Location = new System.Drawing.Point(x, y);
this.gameButtons[i].Name = "btn" + (index + 1);
this.gameButtons[i].Size = new System.Drawing.Size(50, 50);
this.gameButtons[i].TabIndex = i;
//this.gameButtons[i].Text = Convert.ToString(index);
this.gameButtons[i].UseVisualStyleBackColor = true;
this.gameButtons[i].Visible = true;
gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender,index);
this.Controls.Add(gameButtons[i]);
}
}
private void buttonHasBeenPressed(object sender,int i)
{
if (((Button)sender).Text == "")
{
if (cross == true)
{
((Button)sender).Text = "X";
gameButtons[i] = 'X';
}
else
{
((Button)sender).Text = "O";
gameButtons[i] = 'O';
}
cross = !cross;
}
}
}
Edit: The first problem was solved, thanks a lot to everyone :) But now I'm struggling to find the win condition. I've used this code but I'm getting a compiler error that I don't seem to understand how to fix it. This is the code I've made up:
private void threeInARow(int a, int b, int c, object sender)
{
if (gameButtons[a]==gameButtons[b] && gameButtons[a]==gameButtons[c])
{
if (gameButtons[a]='X')
{
MessageBox.Show("the winner is crosses");
}
else
{
MessageBox.Show("the winner is noughts");
}
}
The error is on my my first if it says "cannot implicitly convert type char to system.windows.forms.button"
Firstly, you have some compile errors:
// These two lines both throw the error:
// Cannot implicitly convert type 'char' to 'System.Windows.Forms.Button'
gameButtons[i] = 'X';
gameButtons[i] = 'O';
This is because you're trying to set a button to a character. This is not necessary, since you've already changed the text of the button, and you can remove these lines.
Next, you get a runtime exception when clicking on a button, on this line:
// The following line fails with the error:
// Unable to cast object of type 'WinFormTest.Form1' to type 'System.Windows.Forms.Button'.
if (((Button)sender).Text == "")
This is because the sender is the Form1 object, and not the button. The reason for this is that, in your assignment of the event to the button Click, you are passing sender instead of sender1 to the event, and, since the assignment of this event is happening in the Form.Load event, sender is the Form1. So you need to change the assignment to pass sender1 instead:
gameButtons[i].Click += (sender1, ex) => this.buttonHasBeenPressed(sender1, index);
The next problem you're having (since you've modified the code in the original question) is in the threeInARow method here:
if (gameButtons[a] = 'X') // Error: Cannot implicitly convert type 'char' to 'Button'
The reason for this is that gameButtons is an array of Button objects, so gameButtons[a] represents a Button, and you can't assign the character 'X' to a Button (they are two different types). Since you've already assigned a value to the Text property of each button (which is of string type), you can just use that instead.
Also, you are using a single = sign, which is an assignment. You want to do a comparison, which uses a double == sign. So, putting these together, you will get:
if (gameButtons[a].Text == "X")
You have added similar problematic code to your buttonHasBeenPressed method, which you should just remove since we can compare the Text properties and don't need this additional assignement:
gameButtons[i] = 'X'; // Remove these invalid assignments
That takes care of the compile error, but you still have another problem in your comparisons with this line:
if (gameButtons[a] == gameButtons[b] && . . .
This line is asking if the Button reference in the array at index a is pointing to the exact same object as the Button reference in the array at index b. This will never be the case because you (correctly) initialized your array with 9 unique buttons.
What you really want to do is compare the Text property of each button, like so:
if (gameButtons[a].Text == gameButtons[b].Text && . . .
Lastly, you have included an Object parameter named sender to your method that you aren't using, so you might as well remove that (or do something with it in your method).
So, putting this all together you have:
private void threeInARow(int a, int b, int c)
{
if (gameButtons[a].Text == gameButtons[b].Text && gameButtons[a].Text == gameButtons[c].Text)
{
if (gameButtons[a].Text == "X")
{
MessageBox.Show("the winner is crosses");
}
else
{
MessageBox.Show("the winner is noughts");
}
}
}
See code below :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Button[] gameButtons = new Button[9]; //array of buttons for markers(X's and O's)
bool cross = true; //cross is set to true if the next marker is to be a cross
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "More Complex Version of Noughts and Crosses";
this.BackColor = Color.BlanchedAlmond;
this.Width = 400;
this.Height = 400;
for (int i = 0; i < gameButtons.Length; i++)
{
int index = i;
this.gameButtons[i] = new Button();
int x = 50 + (i % 3) * 50;
int y = 50 + (i / 3) * 50;
this.gameButtons[i].Location = new System.Drawing.Point(x, y);
this.gameButtons[i].Name = "btn" + (index + 1);
this.gameButtons[i].Size = new System.Drawing.Size(50, 50);
this.gameButtons[i].TabIndex = i;
//this.gameButtons[i].Text = Convert.ToString(index);
this.gameButtons[i].UseVisualStyleBackColor = true;
this.gameButtons[i].Visible = true;
gameButtons[i].Click += new EventHandler(buttonHasBeenPressed);
this.Controls.Add(gameButtons[i]);
}
}
private void buttonHasBeenPressed(object sender, EventArgs e)
{
if (((Button)sender).Text == "")
{
if (cross == true)
{
((Button)sender).Text = "X";
//gameButtons[i].Text = "X";
}
else
{
((Button)sender).Text = "O";
//gameButtons[i].Text = 'O';
}
cross = !cross;
}
}
}
}
In C# im trying to make a little game type program and im trying to make a loading bar that uses the Progress bar and the text is using a Label, for example the Progress bar is 1 - 25 and i want the label text to update while the bar is, heres an example:
private void StartLoading_Click(object sender, EventArgs e)
{
MainProgressBar.Maximum = 25;
int P = 0;
while (P < 25)
{
// Delay
System.Threading.Thread.Sleep(130);
// Increase Progress
P++;
// Set Progress Bar Value
MainProgressBar.Value = P;
// Set Text Above Progress Bar
LoadingText.Text = P + "/25";
}
}
Ps. I dont want some Huge code, i want it to be simple like this
State of the art is this snippet for you:
private void StartLoading_Click(object sender, EventArgs e)
{
const int max = 25;
var progressHandler = new Progress<int>(value=>{
LoadingText.Text = value + "/" + max;
MainProgressBar.Value = value;
});
var progress = progressHandler as IProgress<int>;
await Task.Run(() =>
{
int P = 0;
while (P < 25)
{
Thread.Sleep(130);
progress?.Report(++P);
}
}
}
This processes your long running task (Sleep in this case), in a seperate Thread and reuse the value via the Progress-Class. This way your GUI is updated in GUI-Thread as recommended and you will get the updates accordingly. Further it's not recommended to use Application.DoEvents();, because there are many pitfalls you have to know about.
I have a method that shows when a process bar is in execution and when is successfully completed.
I worked fine, but I would like to add a percentage showing a 100% if is complete and less if it got stuck some where.
I have made several research online but I could not adapt anything to the solution that I am looking for.
This is my code:
private void progressBar()
{
int i;
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
for (i = 0; i <= 100; i++)
{
progressBar1.Value = i;
}
}
I use the method call on my execution button by calling it with the follow:
progressBar();
Thanks
I have adjust the prograssBar method with the following lines.
The solution works.
Thanks
int percent = (int)(((double)progressBar1.Value / (double)progressBar1.Maximum) * 100);
progressBar1.Refresh();
progressBar1.CreateGraphics().DrawString(percent.ToString() + "%",
new Font("Arial", (float)8.25, FontStyle.Regular),
Brushes.Black,
new PointF(progressBar1.Width / 2 - 10, progressBar1.Height / 2 - 7));
In order to implement the progress in your operation, the operation's length must be calculated first. if it's not possible, you can't show a progress bar for that operation. (maybe only a loading gif)
but if so, There is an interface (IProgress) which can help you implement the progress reports.
First thing you should know, You must do the main task on another thread, and report the progress to the UI Thread. a simple example of this work would be something like this.
Progress.cs
public class Progress<T> : IProgress<T>
{
private readonly Action<T> _progressAction;
public Progress(Action<T> action)
{
_progressAction = action;
}
public void Report(T value)
{
_progressAction?.Invoke(value);
}
}
Your code would be like this, in which the task starts after you click a button named ButtonBase
Progress<int> MyProgressObject { get; set; }
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
MyProgressObject = new Progress<int>(ProgressAction);
ThreadPool.QueueUserWorkItem(TimeConsumingTask);
}
public void TimeConsumingTask(object state)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);
MyProgressBar.Dispatcher.Invoke(() => ProgressAction(i));
}
}
public void ProgressAction(int progress)
{
MyProgressBar.Value = progress;
}
I know It might look difficult but this is the proper way of doing time consuming tasks and prevent UI block
If you use it as a part of backgroundworker it works perfectly
I added a Label in the middle of the progressbar
And i added last row in my bgw_ProgressChanged method
private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
p_bar.Value = e.ProgressPercentage;
fnameLbl.Text = e.UserState.ToString();
percentLbl.Text = "%" + (e.ProgressPercentage).ToString();
}
ProgressPercentagevalue comes from the method below
foreach (var item in filebox1)
{
System.IO.File.Move(item, Path.Combine(destdir, Path.GetFileName(item)));
++counter;
int tmp = (int)((counter* 100) / totfiles);
bgw.ReportProgress(tmp, "File transfered : " + Path.GetFileName(item));
Thread.Sleep(100);
}
Totfiles is the number of files that I get from server.
Thread.Sleep(100) let's you see for a short time what is displayed with fnameLbl.Text
int total = ;
int val = ;
double createDivider = total / 100;
int percent = val / createDivider;
this value (percent) is the right percent '%' of total