Need to calculate when adding coin - c#

I am building a vending machine and I am stuck on adding coins.
Coin should automatically be calculated when I click on the assigned button But Instead I am just getting the value inside textbox here's the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VendingMachine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void operator_Click(object sender, EventArgs e)
{
new Form2().Show();
this.Hide();
}
private void fiveP_Click(object sender, EventArgs e)
{
balance.Text = ((double)balance.Text + 0.05).ToString();
}
private void tenP_Click(object sender, EventArgs e)
{
balance.Clear();
balance.Text = balance.Text + "0.10";
}
private void twentyP_Click(object sender, EventArgs e)
{
balance.Clear();
balance.Text = balance.Text + "0.20";
}
private void fiftyP_Click(object sender, EventArgs e)
{
balance.Clear();
balance.Text = balance.Text + "0.50";
}
private void onePound_Click(object sender, EventArgs e)
{
balance.Clear();
balance.Text = balance.Text + "1.00";
}
private void twoPound_Click(object sender, EventArgs e)
{
balance.Clear();
balance.Text = balance.Text + "2.00";
}
}
}
Coin Class
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
namespace VendingMachine
{
[Serializable]
internal class Coin : ISerializable, IComparable
{
public string coinName = "";
public double coinValue = 0.0;
public int coinBalance = 0;
public string Name
{
get { return this.coinName; }
set { this.coinName = value; }
}
public double Value
{
get { return this.coinValue; }
set { this.coinValue = value; }
}
public int Balance
{
get { return this.coinBalance; }
set { this.coinBalance = value; }
}
public Coin(string coin_name)
{ this.coinName = coin_name; }
public Coin(SerializationInfo info, StreamingContext ctxt)
{
this.coinValue = (double)info.GetValue("CoinValue", typeof(double));
this.coinName = (string)info.GetValue("CoinName", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("CoinValue", this.coinValue);
info.AddValue("CoinName", (object)this.coinName);
}
public int CompareTo(object obj)
{
if (obj is Coin)
return this.Value.CompareTo(((Coin)obj).Value);
else
throw new ArgumentException("object is not a Coin");
}
public static IComparer sortByCoinName()
{
return (IComparer)new Coin.sortByCoinNameHelper();
}
private class sortByCoinNameHelper : IComparer
{
int IComparer.Compare(object a, object b)
{
return ((Coin)a).Name.CompareTo(((Coin)b).Name);
}
}
}
}

if balance is the textbox you are working with, you are clearing it with the button press, erasing whatever was in it prior to the button press, so it will always be "" + whatever. also, you can't add strings like that, "1.00" + "2.00" == "1.002.00", != "3.00"

You are adding strings rather than numbers. You'll need to convert your strings into numbers to add them together, and you shouldn't clear your results first.
For example:
private void fiveP_Click(object sender, EventArgs e)
{
// balance.Clear(); <- You don't need this.
balance.Text = ((double)balance.Text + 0.05).ToString();
}

that is because you are using a string type for calculating.. there is a difference between calculating something and displaying it. in short:
float value = 0;
void print()
{
balance.Text = string.Format("{0:0.00}", value);
}
private void fiveP_Click(object sender, EventArgs e)
{
value += 0.05f;
print();
}
private void tenP_Click(object sender, EventArgs e)
{
value += 0.10f;
print();
}
private void twentyP_Click(object sender, EventArgs e)
{
value += 0.20f;
print();
}
private void fiftyP_Click(object sender, EventArgs e)
{
value += 0.50f;
print();
}
private void onePound_Click(object sender, EventArgs e)
{
value += 1;
print();
}
private void twoPound_Click(object sender, EventArgs e)
{
value += 2;
print();
}

in your code you are not adding numbers, example:
you have this code:
private void tenP_Click(object sender, EventArgs e)
{
balance.Clear();
balance.Text = balance.Text + "0.10";
}
the value of balance.Text = "0.10"
then, if the next code is executed:
private void fiftyP_Click(object sender, EventArgs e)
{
balance.Clear();
balance.Text = balance.Text + "0.50";
}
the value of balance.Text = "0.50"
the point is that your are trying to add strings not numbers and you are clearing your previous value

Related

C# can't use variable from IF statement

I'm making easy password generator, but i cant pick int from try and string from if. Here's the code. I hope you help me. I cant make this I as textbox and i cant do nothing with it.
private void button3_Click(object sender, EventArgs e)
{
try
{
int i = Int32.Parse(textBox2.Text);
return;
}
catch
{
}
CreatePassword(i);
}
and here is part of CreatePassword function
public string CreatePassword(int length)
{
if (checkBox2.Checked)
{
const string src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
return src;
}
else
{
const string src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
return src;
}
}
There are several problems with your code. First, you're trying to access the variable i outside of the scope in which it is declared; it's not visible outside of the try statement. Second, it seems like you're expecting the password to be generated from the integer you parsed, but you're explicitly returning before the password can be created. Thirdly, you're not doing anything with the created password, just throwing it away.
Try the following:
private void button3_Click(object sender, EventArgs e)
{
try
{
int i = Int32.Parse(textBox2.Text);
string password = CreatePassword(i);
// TODO: use the 'password' string for something.
return;
}
catch
{
}
}
You should also consider using int.TryParse instead, which won't throw an exception.
if (int.TryParse(textbox2.Text, out int i) {
string password = CreatePassword(i);
// Do something with 'password'
} else {
// Display an error.
}
From your example, it looks like all your need is Int32.TryParse:
int.TryParse(textBox2.Text, out int i);
CreatePassword(i);
However, to answer your original question: you need to initialize i variable outside of the try block in order to be able to use it after it. For instance:
int i = 0;
try
{
i = int.Parse("test");
}
catch
{
}
Console.WriteLine(i); // 0
You logic is a bit flawed. If Textbox2 does not contain a valid integer, you ignore the exception and just create a password. What kind of password you expect to create?
I think you mean to do something like this:
private void button3_Click(object sender, EventArgs e)
{
try
{
int i = Int32.Parse(textBox2.Text);
CreatePassword(i);
}
catch
{
// Show a messagebox or something
}
}
Guys all thanks for help. I did it. Here you have my source of my application as thanks for you all. My app completely work and i believe you understand my logic :D
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Password_generator
{
public partial class Form1 : Form
{
private bool _dragging = false;
private Point _start_point = new Point(0, 0);
public Form1()
{
InitializeComponent();
}
public string CreatePassword(int length)
{
string src;
var sb = new StringBuilder();
Random RNG = new Random();
src = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (checkBox2.Checked)
{
src += "1234567890";
}
if (checkBox3.Checked)
{
src += "##$%^&*()";
}
for (var i = 0; i < length; i++)
{
var c = src[RNG.Next(0, src.Length)];
sb.Append(c);
}
textBox1.Text = sb.ToString();
if (checkBox1.Checked)
{
try
{
File.AppendAllText("hesla.txt", textBox1.Text + Environment.NewLine);
}
catch(Exception o)
{
MessageBox.Show("Něco se nepovedlo! " + Environment.NewLine + "(" + o.Message + ")");
}
}
return textBox1.Text;
}
private void button3_Click(object sender, EventArgs e)
{
try
{
int i = Int32.Parse(textBox2.Text);
CreatePassword(i);
}
catch
{
MessageBox.Show("Musíš zadat číslo!");
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
_dragging = false;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (_dragging)
{
Point p = PointToScreen(e.Location);
Location = new Point(p.X - this._start_point.X, p.Y - this._start_point.Y);
}
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
_dragging = true;
_start_point = new Point(e.X, e.Y);
}
private void panel3_Paint(object sender, PaintEventArgs e)
{
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
try
{
Clipboard.SetText(textBox1.Text);
}
catch
{
}
}
}
}

serialport1 does not exist in context

using System.Collections.Generic;
using System.ComponentModel;
using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication21
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Red;
label1.Text = " OCCUPIED";
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
this.Invoke(new EventHandler(DoUpdate));
}
private void DoUpdate(object s, EventArgs e)
{
string InputString = serialPort1.ReadExisting();
string[] data = InputString.Split(',');
textBox1.Text = data[0];
textBox2.Text = data[1];
int b;
int d;
bool result = Int32.TryParse(data[0], out b);
bool sonuc = Int32.TryParse(data[1], out d);
if (result)
{
if (b < 224)
{
button1.BackColor = Color.Red;
label1.Text = " OCCUPIED";
}
else
{
button1.BackColor = Color.Lime;
label1.Text = "AVAILABLE";
}
}
if (sonuc)
{
if (d< 224)
{
button1.BackColor = Color.Red;
label1.Text = " OCCUPIED";
}
else
{
button1.BackColor = Color.Lime;
label1.Text = "AVAILABLE";
}
}
int b = Int16.Parse(a);
textBox1.Text= a.ToString();*/
private void button3_Click(object sender, EventArgs e)
{
listBox1.Visible = true;
listBox1.Items.Add("Değerler alınıyor!");
if (!serialPort1.IsOpen)
{
serialPort1.PortName = "COM11";
serialPort1.Open();
}
}
private void button4_Click(object sender, EventArgs e)
{
serialPort1.Close();
listBox1.Items.Add("Durduruldu!");
}
}
}
I am getting an error:
"serialport1 does not exist in context"
What can l do? Whenever I want to compile I get the above mention error.
Do I have to add any library?
I am using visual Studio 2010

Graph plotting C#

I am working on an interface in C#, which reads data from the serial port and displays it on a graph. I wrote the following code, but the graph doesn't display anything. What is wrong? I don't know exactly what should I modify.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public long rt = 0;
string dataReceived = string.Empty;
private delegate void SetTextDeleg(string text);
// public string kp, kd, ki;
public int distanta;
public string potentiometru;
public Form1()
{
InitializeComponent();
this.chart1.ChartAreas[0].AxisX.Minimum = 0;
this.chart1.ChartAreas[0].AxisY.Minimum = 0;
this.chart1.ChartAreas[0].AxisY.Maximum = 55;
}
private void Connect_button_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
Connect_button.Enabled = false;
}
serialPort1.BaudRate = 9600;// Convert.ToUInt16(comboBox1.Text);
serialPort1.PortName = comboBox3.Text;
serialPort1.DataBits = 8;
serialPort1.StopBits = (System.IO.Ports.StopBits)2;
timer1.Start();
serialPort1.Open();
}
private void Sent_Button_Click(object sender, EventArgs e)
{
// kp = Convert.ToString(Kp_textbox.Text);
//kd = Convert.ToString(Kd_textbox.Text);
//ki = Convert.ToString(Ki_textbox.Text);
//serialPort1.Write(kp);
//serialPort1.Write(ki);
//serialPort1.Write(kd);
//string transmit = "$kp$" + kp + "$kd$" + kd + "$ki$" + ki;
// $kp$0.25
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string x = serialPort1.ReadLine();
this.BeginInvoke(new SetTextDeleg(DataReceived), new object[] { x });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void DataReceived(string data)
{
dataReceived = data.Trim();
if (rt < 2)
return;
if (dataReceived.Contains("."))
{
senzor_textbox.Text = dataReceived;
this.chart1.Series["Distance"].Points.AddXY(0, dataReceived);
}
else
{
potentiometru_textbox.Text = dataReceived;
this.chart1.Series["SetPoint"].Points.AddXY(0, dataReceived);
}
rt = 0;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
rt++;
}
private void button2_Click(object sender, EventArgs e)
{
if(serialPort1.IsOpen)
serialPort1.Close();
}
}
}

Calculator, clear window when second variable is typed after chosen operator

So I have this calculator http://gyazo.com/589156935eec141c3aedf83b9f960d29 (not enough reputation sorry)
When I type [1] and then [2] the display shows [12]
If I press a operator for example [+] the number 12 is still supposed to be shown in the display.
But, if I now start typing new numbers The old ones are supposed to be removed from the display. But i can't get this to work.
My form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Miniräknare
{
public partial class Form1 : Form
{
Miniräknare miniräknare;
public Form1()
{
InitializeComponent();
miniräknare = new Miniräknare(0, 0, "", 0, false);
}
private void btnEquals_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.doEquals();
}
private void btnNum1_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("1", tbxWindow.Text);
}
private void btnNum2_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("2", tbxWindow.Text);
}
private void btnNum3_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("3", tbxWindow.Text);
}
private void btnNum4_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("4", tbxWindow.Text);
}
private void btnNum5_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("5", tbxWindow.Text);
}
private void btnNum6_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("6", tbxWindow.Text);
}
private void btnNum7_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("7", tbxWindow.Text);
}
private void btnNum8_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("8", tbxWindow.Text);
}
private void btnNum9_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand("9", tbxWindow.Text);
}
private void btnNum0_Click(object sender, EventArgs e)
{
if (tbxWindow.Text != "") tbxWindow.Text = miniräknare.getOperand("0", tbxWindow.Text);
}
private void btnOperatorDivision_Click(object sender, EventArgs e)
{
}
private void btnOperatorTimes_Click(object sender, EventArgs e)
{
}
private void btnOperatorPlus_Click(object sender, EventArgs e)
{
miniräknare.Op = "+";
}
private void btnOperatorMinus_Click(object sender, EventArgs e)
{
miniräknare.Op = "-";
miniräknare.Change = true;
}
private void btnDecimal_Click(object sender, EventArgs e)
{
tbxWindow.Text = miniräknare.getOperand(",", tbxWindow.Text);
}
private void btnClear_Click(object sender, EventArgs e)
{
}
private void btnSin_Click(object sender, EventArgs e)
{
}
private void btnCos_Click(object sender, EventArgs e)
{
}
private void btnTan_Click(object sender, EventArgs e)
{
}
private void btnSquared_Click(object sender, EventArgs e)
{
}
private void btnModulus_Click(object sender, EventArgs e)
{
}
private void btnExponential_Click(object sender, EventArgs e)
{
}
private void btnlogarithm_Click(object sender, EventArgs e)
{
}
private void btn1OverX_Click(object sender, EventArgs e)
{
}
private void btnLn_Click(object sender, EventArgs e)
{
}
private void btnPi_Click(object sender, EventArgs e)
{
}
private void btnMemoryClear_Click(object sender, EventArgs e)
{
}
private void btnMemoryRecall_Click(object sender, EventArgs e)
{
}
private void btnMemorySave_Click(object sender, EventArgs e)
{
}
}
}
My class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Miniräknare
{
class Miniräknare
{
private double first;
private double second;
private string op;
private double memory;
private bool change;
public Miniräknare(double first, double second, string op, double memory, bool change)
{
this.first = 0;
this.second = 0;
this.op = "";
this.memory = 0;
this.change = false;
}
public double First
{
get {return first; }
set { first = value; }
}
public double Second
{
get { return second; }
set { second = value; }
}
public string Op
{
get { return op; }
set { op = value; }
}
public double Memory
{
get { return memory; }
set { memory = value; }
}
public bool Change
{
get { return change; }
set { change = value; }
}
public string getOperand(string t, string textBox)
{
textBox = textBox + t;
if (t.Equals(","))
{
change = true;
second = double.Parse(textBox);
}
else if (op.Equals(""))
{
if (!change)
{
textBox = "";
change = true;
textBox = textBox + t;
}
first = double.Parse(textBox);
}
else
{
if (!change)
{
textBox = "";
change = true;
textBox = textBox + t;
}
second = double.Parse(textBox);
}
return textBox;
}
/* public string calculateAnswer()
{
} */
public string doEquals()
{
if (op == "-" ) return (first - second).ToString();
else return null;
}
}
}
In the following block after pressing "+" button "change" is true and the block is skipped when type the first digit of the second number.
else
{
if (!change)
{
textBox = "";
change = true;
textBox = textBox + t;
}
second = double.Parse(textBox);
}
since you assigned textBox at the beginning of the getOperand method, it will return the value combining what you already had on the screen with the new char.
public string getOperand(string t, string textBox)
{
textBox = textBox + t;
This should do the trick:
public string getOperand(string t, string textBox)
{
if (t.Equals(","))
{
textBox = textBox + t;
change = true;
second = double.Parse(textBox);
}
else if (Op.Equals(""))
{
textBox = textBox + t;
if (!change)
{
textBox = "";
change = true;
textBox = textBox + t;
}
first = double.Parse(textBox);
}
else
{
if (!change)
{
textBox = textBox + t;
}
else
{
textBox = t;
change = false;
}
second = double.Parse(textBox);
}
return textBox;
}
I know this is not https://codereview.stackexchange.com/ and this does not answer the question asked (which has already been answered) and might be marked as off-topic, but wanted to show the changes as suggested in the various comments in an orderly fashion, just to help you improve your coding (current and future) experience.
Changes that can be made to your Miniräknare class (Comments added to explain):
public class Miniräknare
{
public Miniräknare()
{
// Have a default constructor that sets all the default properties
First = 0;
Second = 0;
Op = "";
Memory = 0;
Change = false;
}
public Miniräknare(double first, double second, string op, double memory, bool change)
{
// If you have a constructor with parameters, use the parameters to set your properties
First = first;
Second = second;
Op = op;
Memory = memory;
Change = change;
}
// Use automatic properties, this improves readability and less confusion (As per D Stanley in comments)
public double First { get; set; }
public double Second { get; set; }
public string Op { get; set; }
public double Memory { get; set; }
public bool Change { get; set; }
public string getOperand(string t, string textBox)
{
// Apply changes as per the accepted answer
textBox = textBox + t;
if (t.Equals(","))
{
Change = true;
Second = double.Parse(textBox);
}
else if (Op.Equals(""))
{
if (!Change)
{
textBox = "";
Change = true;
textBox = textBox + t;
}
First = double.Parse(textBox);
}
else
{
if (!Change)
{
textBox = "";
Change = true;
textBox = textBox + t;
}
Second = double.Parse(textBox);
}
return textBox;
}
public string doEquals()
{
if (Op == "-") return (First - Second).ToString();
else return null;
}
}
Changes in your Form1:
Instantiate your miniräknare variable now like this, since you where setting them to the defaults with your original input parameters.
miniräknare = new Miniräknare();
Replace all your btnNum1_Click to btnNum9_Click event handlers with this single event handler to improve readability and volume of your code (Check additional comments in the code):
private void btnNumber_Click(object sender, EventArgs e)
{
// !! Remember !! to set the Tag value of each of your buttons to their corresponding values
// Then change btnNum1 to btnNum9's Click events to btnNumber_Click
// Additionally you can also just use ((Button)sender).Text if their text values will never change
// You could even do this with your operators, unless you have specific code for the button (like you have for btnNum0)
tbxWindow.Text = miniräknare.getOperand(((Button)sender).Tag as String, tbxWindow.Text);
}

openfiledialog file and directory?

I m trying to make simple mp3 player.when i use this
listBox2.Items.Add(openFileDialog1.FileName);
to add songs to my listbox it is working but it shows file directory so i changed like this
listBox2.Items.Add(openFileDialog1.SafeFileName);
then it looks song name on listbox1 but when i click play button it is not working:(
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;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication12222
{
public partial class Form1 : Form
{
[DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
public string Pcommand;
public bool isOpen;
public Form1()
{
InitializeComponent();
}
public void Stop()
{
Pcommand = "close MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
isOpen = false;
}
public void Start()
{
Pcommand = "open \"" + listBox1.SelectedItem + "\" type mpegvideo alias MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
isOpen = true;
Play(true);
}
private void button10_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Media File(*.mpg,*.dat,*.avi,*.wmv,*.wav,*.mp3)|*.wav;*.mp3;*.mpg;*.dat;*.avi;*.wmv";
openFileDialog1.ShowDialog();
if (openFileDialog1.FileName != ""){
// listBox1.Items.Add(openFileDialog1.SafeFileName);
listBox2.Items.Add(openFileDialog1.FileName);
}
}
private void button1_Click(object sender, EventArgs e)
{
Start();
}
private void button4_Click(object sender, EventArgs e)
{
Stop();
}
public void Play(bool loop)
{
if (isOpen)
{
Pcommand = "play MediaFile";
if (loop)
Pcommand += " REPEAT";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
}
}
int x;
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex ==listBox1.Items.Count-1 ) { x++; }
else
{
Stop();
listBox1.SelectedIndex = listBox1.SelectedIndex + 1;
Start();
}
}
int y;
private void button5_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex ==0) { y++; }
else
{
Stop();
listBox1.SelectedIndex = listBox1.SelectedIndex - 1;
Start();
}
}
private void button3_Click(object sender, EventArgs e)
{
listBox1.SelectedIndex = 0;
Stop();
Start();
}
private void button6_Click(object sender, EventArgs e)
{
listBox1.SelectedIndex = listBox1.Items.Count - 1;
Stop();
Start();
}
private void button9_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
Pcommand = "close MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
isOpen = false;
}
private void button8_Click(object sender, EventArgs e)
{
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
}
}
extra question is it possible to mix (random) a listbox .I add songs on mp3 player then when i want to click mix button i want list mixed.Is there any listbox command for this?
Create a custom class:
public class FileItem
{
public string FilePath { get; set; }
public string ShortName { get; set; }
}
And then create a new instance of this class when you get a file from the OpenFileDialog, save the openFileDialog1.FileName to FilePath property, and then get the short file name using the Windows.IO.Path.GetFileName() method.
Rather than adding the file path to the ListBox directly from the OpenFileDialog, add this instance of your class.
And change the DisplayMember property of your ListBox to "ShortName", this way the "short name" of your file path will be displayed in the ListBox.
You should be able to use the link below to shuffle your playlist.
Random playlist algorithm
Thanks,
Naval

Categories