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
Related
This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I am a newbie in C# and I have questions as below:
I have a Form1 name is setting Port Name, Baud Rate, Parity... of modbus protocol and I can open serial Port.
Also, I have another Form is called Form2, When Port is opened i want to close Form1 and Port alway Open => I can do it. But this problem that was I want to get data such as FC03 HolodingRegister, FC01 WriteSingleCoil... for Form2 but didnot.
I used delegate to transfer data from Form 1 to Form 2 but I could not use button Form2 to send FC01 signal.
How to use FC01, FC03,04... for Form2 when Form 1 connected.
Code Form1:
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;
using Modbus.Device;
namespace ATS
{
public partial class frCommunication : Form
{
SerialPort serialPort = new SerialPort();
ModbusMaster Master;
public delegate void truyendulieu(string text);
public truyendulieu truyendata;
public delegate void truyendulieu1(string text1);
public truyendulieu1 truyendata1;
public delegate void truyendulieu2(string text2);
public truyendulieu2 truyendata2;
public frCommunication()
{
InitializeComponent();
}
private void frCommunication_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
cboxPort.Items.AddRange(ports);
cboxPort.SelectedIndex = 0;
}
private void btnConnect_Click(object sender, EventArgs e)
{
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
try
{
serialPort.PortName = cboxPort.Text;
serialPort.BaudRate = Convert.ToInt32(cboxBaudRate.Text);
serialPort.DataBits = Convert.ToInt32(cboxDataBits.Text);
serialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cboxStopBits.Text);
serialPort.Parity = (Parity)Enum.Parse(typeof(Parity), cboxParity.Text);
serialPort.Open();
Master = ModbusSerialMaster.CreateRtu(serialPort);
Master.Transport.Retries = 0; // don't have to to retries
Master.Transport.ReadTimeout = 300;//miliseconds
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (serialPort.IsOpen)
{
lblDisplay.Text = "Connected";
lblDisplay.ForeColor = System.Drawing.Color.Red;
cboxBaudRate.Enabled = false;
}
else
{
lblDisplay.Text = "Disconnected";
MessageBox.Show("Error!");
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
btnConnect.Enabled = true;
btnDisconnect.Enabled = false;
try
{
serialPort.Close();
lblDisplay.Text = "Disconnected";
lblDisplay.ForeColor = System.Drawing.Color.Green;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (serialPort.IsOpen)
{
ushort[] holding_register = Master.ReadHoldingRegisters(1, 0, 10);
txtV_Grid.Text = Convert.ToString(holding_register[0]);
txtC_Grid.Text = Convert.ToString(holding_register[1]);
txtP_Grid.Text = Convert.ToString(holding_register[2]);
}
}
private void btnStart_Click(object sender, EventArgs e)
{
if (txtV_Grid.Text.Length > 0 || txtC_Grid.Text.Length > 0 || txtP_Grid.Text.Length > 0)
{
if (truyendata != null || truyendata1 != null)
{
truyendata(txtV_Grid.Text);
truyendata1(txtC_Grid.Text);
truyendata2(txtP_Grid.Text);
}
this.Hide();
}
}
private void txtV_Grid_TextChanged(object sender, EventArgs e)
{
if (truyendata != null)
{
truyendata(txtV_Grid.Text);
}
}
private void txtC_Grid_TextChanged(object sender, EventArgs e)
{
if (truyendata1 != null)
{
truyendata1(txtC_Grid.Text);
}
}
private void txtP_Grid_TextChanged(object sender, EventArgs e)
{
if (truyendata2 != null)
{
truyendata2(txtP_Grid.Text);
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void btnOn_ACB_Grid_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen)
{
DialogResult dl = MessageBox.Show("Would you like to turn On ACB_GRID", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dl == DialogResult.Yes)
{
Master.WriteSingleCoil(1, 0, true);
}
else
{
Master.WriteSingleCoil(1, 0, false);
}
}
}
private void btnOff_ACB_Grid_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen)
{
DialogResult dl = MessageBox.Show("Would you like to turn Off ACB_GRID", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dl == DialogResult.Yes)
{
Master.WriteSingleCoil(1, 0, false);
}
else
{
Master.WriteSingleCoil(1, 0, true);
}
}
}
}
}
Code Form2:
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 SymbolFactoryDotNet;
namespace ATS
{
public partial class frMain : Form
{
public frMain()
{
InitializeComponent();
}
private void communicationToolStripMenuItem_Click(object sender, EventArgs e)
{
frCommunication frm = new frCommunication();
frm.ShowDialog();
}
private void standardControl1_Load(object sender, EventArgs e)
{
}
private void LoadData(string data)
{
txtV.Text = "";
txtV.Text = data;
}
private void LoadData1(string data1)
{
txtC.Text = "";
txtC.Text = data1;
}
private void LoadData2(string data2)
{
txtP.Text = "";
txtP.Text = data2;
}
private void btnConnect_Click(object sender, EventArgs e)
{
frCommunication frm = new frCommunication();
frm.truyendata = new frCommunication.truyendulieu(LoadData);
frm.truyendata1 = new frCommunication.truyendulieu1(LoadData1);
frm.truyendata2 = new frCommunication.truyendulieu2(LoadData2);
frm.ShowDialog();
}
private void txtV_TextChanged(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)
{
if(picOn.Visible == false)
{
picOn.Visible = true;
picOff_Grid.Visible = false;
// standardControl3.DiscreteValue2 = true;
}
else
{
picOn.Visible = false;
picOff_Grid.Visible = true;
}
}
private void frMain_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToString("HH:mm:ss dd-MM-yyyy");
}
private void btnOn_Grid_Click(object sender, EventArgs e)
{
}
}
}
When I understand you right, you will in Form1 open a connection, close the Form, open a new Form2 and use this connection there?
Well, when that's the case, you could make an special Connection Singleton to hold this connection then you can use it in your Form2
using System;
namespace Sandbox
{
public sealed class Connection
{
private static readonly Lazy<Connection> _instance = new Lazy<Connection>(() => new Connection());
public static Connection Instance => _instance.Value;
private Connection()
{ }
// Implement your Connection Code here.
}
}
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
{
}
}
}
}
I have created a Desktop app using Windows Form (c#) in visual Studio 2019.first, in my form I created a panel and several buttons for navigation. for the crud, I have created a user control and now I need to get this user control to the form but it shows in the toolbox neither in the codes
form Home
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.Runtime.InteropServices;
namespace WinFormsApp1
{
public partial class Home : Form
{
static Home _obj;
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn
(
int nLeftRect, // x-coordinate of upper-left corner
int nTopRect, // y-coordinate of upper-left corner
int nRightRect, // x-coordinate of lower-right corner
int nBottomRect, // y-coordinate of lower-right corner
int nWidthEllipse, // height of ellipse
int nHeightEllipse // width of ellipse
);
public static Home Instance
{
get {
if (_obj == null) {
_obj = new Home();
}
return _obj;
}
}
public Home()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
panel2.Height = 46;
btnSideAdd.Visible = false;
_obj = this;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
this.Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
}
public Button BtnSideAdd {
get { return btnSideAdd; }
set { btnSideAdd = value; }
}
public Panel PanelControl1
{
get { return PanelControl1; }
set { PanelControl1 = value; }
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
sidePanel.Height = button4.Height;
sidePanel.Top = button4.Top;
}
private void button5_Click(object sender, EventArgs e)
{
sidePanel.Height = button5.Height;
sidePanel.Top = button5.Top;
}
private void button7_Click(object sender, EventArgs e)
{
sidePanel.Height = button7.Height;
sidePanel.Top = button7.Top;
}
private void button10_Click(object sender, EventArgs e)
{
sidePanel.Height = button10.Height;
sidePanel.Top = button10.Top;
}
private void button2_Click(object sender, EventArgs e)
{
sidePanel.Height = button2.Height;
sidePanel.Top = button2.Top;
}
private void button1_Click(object sender, EventArgs e)
{
sidePanel.Height = btnSideAdd.Height;
sidePanel.Top = btnSideAdd.Top;
}
private void button8_Click(object sender, EventArgs e)
{
if (panel2.Height == 140)
{
panel2.Height = 46;
}
else
{
panel2.Height = 140;
}
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
}
private void panel2_Paint_1(object sender, PaintEventArgs e)
{
}
private void button11_Click(object sender, EventArgs e)
{
}
private void button12_Click(object sender, EventArgs e)
{
}
private void button6_Click(object sender, EventArgs e)
{
sidePanel.Height = button6.Height;
sidePanel.Top = button6.Top;
}
private void button3_Click(object sender, EventArgs e)
{
sidePanel.Height = button3.Height;
sidePanel.Top = button3.Top;
}
private void button9_Click(object sender, EventArgs e)
{
sidePanel.Height = button9.Height;
sidePanel.Top = button9.Top;
}
}
}
user control AddWorks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinFormsApp1.Asram
{
public partial class AddWork : UserControl
{
public AddWork()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void label5_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
Im a beginner and this my assignment project I need to fix this so that I can move in to other parts
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
I am trying to make my listBox (lstFiles) selectable, so it's able to display the image file within a pictureBox (pictureBox1) and change after selecting another file from listBox, Im creating a webcam program that takes pictures which works, but having trouble with with displaying the images.
I have tried many ways but can't get it to work from selecting the filename from the listbox
Any help would be grateful thank you
This is what I have so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Pinvoke;
using System.IO;
namespace TestAvicap32
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeDevicesList();
}
private void InitializeDevicesList()
{
splitContainer1.Panel1.Enabled = true;
splitContainer1.Panel2.Enabled = false;
foreach (CaptureDevice device in CaptureDevice.GetDevices())
{
cboDevices.Items.Add(device);
}
if (cboDevices.Items.Count > 0)
{
cboDevices.SelectedIndex = 0;
}
}
private void btnStart_Click(object sender, EventArgs e)
{
int index = cboDevices.SelectedIndex;
if (index != -1)
{
splitContainer1.Panel1.Enabled = false;
splitContainer1.Panel2.Enabled = true;
((CaptureDevice)cboDevices.SelectedItem).Attach(pbImage);
}
}
private void btnStop_Click(object sender, EventArgs e)
{
splitContainer1.Panel1.Enabled = true;
splitContainer1.Panel2.Enabled = false;
((CaptureDevice)cboDevices.SelectedItem).Detach();
}
private void btnSnapshot_Click(object sender, EventArgs e)
{
try
{
Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture();
image.Save(#"c:\webcapture\" + DateTime.Now.ToString("HH.mm.ss-dd-MM-yy") + ".png", ImageFormat.Png);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btntimer_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
btntimerstop.Visible = true;
btntimer.Visible = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
Image image = ((CaptureDevice)cboDevices.SelectedItem).Capture();
image.Save(#"c:\webcapture\" + DateTime.Now.ToString("HH.mm.ss-dd-MM-yy") + ".png", ImageFormat.Png);
}
private void btntimerstop_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
btntimer.Visible = true;
btntimerstop.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
btntimerstop.Visible = false;
foreach (DriveInfo di in DriveInfo.GetDrives())
lstDrive.Items.Add(di);
}
private void lstFolders_SelectedIndexChanged(object sender, EventArgs e)
{
lstFiles.Items.Clear();
DirectoryInfo dir = (DirectoryInfo)lstFolders.SelectedItem;
foreach (FileInfo fi in dir.GetFiles())
lstFiles.Items.Add(fi);
}
private void lstDrive_SelectedIndexChanged(object sender, EventArgs e)
{
lstFolders.Items.Clear();
try
{
DriveInfo drive = (DriveInfo)lstDrive.SelectedItem;
foreach (DirectoryInfo dirInfo in drive.RootDirectory.GetDirectories())
lstFolders.Items.Add(dirInfo);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
//I don't know If I need this?
}
}
}
Try like below... it will work....
private void lstFiles_SelectedIndexChanged(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(((FileInfo)lstFiles.SelectedItem).FullName);
}