WindowsMediaPlayer: Cannot set maximum value - c#

I'm trying to set a maximum value of a metro theme trackbar using an integer from WindowsMediaPlayer's current media however it keeps throwing the following error:
Specified argument was out of the range of valid values.
Parameter name: Maximal value is lower than minimal one
This means that the maximum value is not being set at all, I'm not sure why.
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;
using MetroFramework;
using MetroFramework.Forms;
using VideoLibrary;
using System.IO;
using System.Threading;
using System.Diagnostics;
using WMPLib;
using UITimer = System.Windows.Forms.Timer;
namespace Composer
{
public partial class Form1 : MetroForm
{
private string _pDirectory;
private string[] _songs;
private int _sIndex;
private WindowsMediaPlayer wmp;
private UITimer _timer;
public Form1()
{
InitializeComponent();
}
private void metroTrackBar1_Scroll(object sender, ScrollEventArgs e)
{
wmp.settings.volume = metroTrackBar1.Value;
}
private void metroTile3_Click(object sender, EventArgs e)
{
playAudio(Path.Combine(_pDirectory, _songs[_sIndex]));
}
private void playAudio(string path)
{
wmp.URL = path;
wmp.controls.play();
_timer.Start();
displayHeader(path);
metroTrackBar2.Maximum = (int)wmp.currentMedia.duration;
}
private void t_Tick(object sender, EventArgs e)
{
metroTrackBar2.Value = (int)wmp.controls.currentPosition;
}
private void displayHeader(string song)
{
MethodInvoker invoke = new MethodInvoker(delegate
{
metroTile1.Text = Path.GetFileNameWithoutExtension(_songs[_sIndex]);
});
this.Invoke(invoke);
}
private void Form1_Load(object sender, EventArgs e)
{
string bin;
_sIndex = 0;
_pDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), #"Composer\");
bin = Path.Combine(_pDirectory, "bin");
_timer = new UITimer();
_timer.Interval = 1000;
_timer.Tick += new EventHandler(t_Tick);
if (!Directory.Exists(_pDirectory))
{
Directory.CreateDirectory(_pDirectory);
if(!Directory.Exists(bin))
{
Directory.CreateDirectory(bin);
}
} else
{
_songs = Directory.GetFiles(bin);
}
wmp = new WindowsMediaPlayer();
//MessageBox.Show(_pDirectory);
}
private void metroTile4_Click(object sender, EventArgs e)
{
if(_sIndex == (_songs.Length - 1))
{
_sIndex = 0;
} else
{
_sIndex++;
}
playAudio(Path.Combine(_pDirectory, _songs[_sIndex]));
}
}
}

Related

Why is System.Speech.Recognition not detecting or inserting into textbox?

On visual studio code; I'm trying to get a text box to be used as a speech to text. However, when I enable it no error get thrown, nor does any speech appear. So, not entirely sure were I went wrong here. Very new to C# and visual studio. Any help is appreciated!! :) My mic is set to the default device and is able to detect me speaking. Other function such as copy to clipboard and clear work.
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.Speech;
using System.Speech.Recognition;
using System.Threading;
namespace MercyProto
{
public partial class Form1 : Form
{
public Grammar grammar;
public Thread RecThread;
public Boolean RecognizerState = true;
public SpeechRecognitionEngine recognizer;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GrammarBuilder build = new GrammarBuilder();
build.AppendDictation();
grammar = new Grammar(build);
recognizer = new SpeechRecognitionEngine();
recognizer.LoadGrammar(grammar);
recognizer.SetInputToDefaultAudioDevice();
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>
(recognizer_SpeechRecognized);
RecognizerState = true;
RecThread = new Thread(new ThreadStart(RecThreadFunction));
RecThread.Start();
}
public void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
//Recognizer recognizes the speech
if (!RecognizerState)
return;
this.Invoke((MethodInvoker)delegate
{
textBox1.Text += (" " + e.Result.Text.ToLower());
//This will add a space between each word you say
});
}
public void RecThreadFunction()
{
while (true)
{
try
{
recognizer.Recognize();
}
catch
{
//Handles errors
//Won't hear you, nothing will happen
}
}
private void button21_Click(object sender, EventArgs e)
{
RecognizerState = true;
}
private void button22_Click(object sender, EventArgs e)
{
RecognizerState = false;
}
private void button23_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox1.Text);
}
private void button24_Click(object sender, EventArgs e)
{
textBox1.Text = String.Empty;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}

Why I am coming across "Format Exception" while execution in C# TCP/IP Application?

The code is as follows:
The ServerForm 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;
using SimpleTCP;
namespace TCPIP
{
public partial class ServerForm : Form
{
public ServerForm()
{
InitializeComponent();
}
SimpleTcpServer server;
private void Form1_Load(object sender, EventArgs e)
{
server = new SimpleTcpServer();
server.Delimiter = 0x13; //enter
server.StringEncoder = Encoding.UTF8;
server.DataReceived += Server_DataReceived;
}
private void Server_DataReceived(object sender, SimpleTCP.Message e)
{
StatusText.Invoke((MethodInvoker)delegate ()
{
StatusText.Text = e.MessageString;
e.ReplyLine(string.Format("You said: {0}",e.MessageString));
});
// throw new NotImplementedException();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void StartButton_Click(object sender, EventArgs e)
{
StatusText.Text += "Server Starting !";
System.Net.IPAddress ip = new System.Net.IPAddress(long.Parse(HostText.Text)); //error here
server.Start(ip,Convert.ToInt32(PortText.Text));
}
private void StopButton_Click(object sender, EventArgs e)
{
if(server.IsStarted)
{
server.Stop();
}
}
}
}
The Code of the ClientForm is as follows:
using SimpleTCP;
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 Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SimpleTcpClient client;
private void ConnectButton_Click(object sender, EventArgs e)
{
ConnectButton.Enabled = false;
}
private void Form1_Load(object sender, EventArgs e)
{
client = new SimpleTcpClient();
client.StringEncoder = Encoding.UTF8;
client.DataReceived += Client_DataReceived;
}
private void Client_DataReceived(object sender, SimpleTCP.Message e)
{
StatusText.Invoke((MethodInvoker)delegate ()
{
StatusText.Text = e.MessageString;
//...
});
//throw new NotImplementedException();
}
private void SendButton_Click(object sender, EventArgs e)
{
client.WriteLineAndGetReply(TextMessage.Text, TimeSpan.FromSeconds(4));
}
}
}
The issue in the above code is that it is 'build'ing correctly and even when I am debug it with the new instance, the code is running fine, but will I debug, as soon as I press the "start" button in the Server form it shows the error in line :
System.Net.IPAddress ip = new System.Net.IPAddress(long.Parse(HostText.Text));
The error is: System.FormatException: 'Input string was not in a correct format.'
Please refer the Screenshot for details and suggest a potential fix to the issue.Image of Screenshot of Error inLine
Clearly HostText.Text is returning a value that can't be parsed into a long.
This exception is coming from long.Parse, which is really a language shortcut for Int64.Parse, whose documentation states that it will throw this exception if the input string is not formatted correctly.

Passing data between two forms

first of all i want you to know that i know that there a lot of results for this question, but i have searched far and wide still haven't come up with a solution for my problem.
i have tried to do the following:
1.constructor
2.objects
3.properties
4.delegates
but none of my implementation of them really did worked as wanted (in this "solution" i have used properties
when i press "back" on the "pop up" screen i dont in the main screen the value i choose from in the "pop up" screen
basically, it's something like, i have main screen and a "pop up"
the main screen
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 BakaritCV
{
public partial class FrmProdChoose : MetroFramework.Forms.MetroForm
{
CVFeedUtilities utilities = new CVFeedUtilities();
Mixtures mixture;
public string selectedDefault = " 102";
string t;
public FrmProdChoose(string t)
{
InitializeComponent();
this.t = t;
}
public FrmProdChoose()
{
InitializeComponent();
}
private void btnHome_Click(object sender, EventArgs e)
{
FrmMain frmload = new FrmMain();
utilities.moveBetweenScreens(this, frmload);
}
private void mixtureBtn_Click(object sender, EventArgs e)
{
utilities.loadPopUp(this, mixture);
}
private void FrmProdChoose_Load(object sender, EventArgs e)
{
mixture = new Mixtures(this);
mixtureBtn.Text = selectedDefault;
}
public string Selected
{
get { return selectedDefault; }
set { selectedDefault = value; }
}
}
}
the "pop up"
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 BakaritCV
{
public partial class Mixtures : MetroFramework.Forms.MetroForm
{
string[] mixture = new string[] { "102", "103", "104", "105" };
MetroFramework.Controls.MetroTile[] tiles;
FrmProdChoose form;
string selectedDefault;
CVFeedUtilities utilities = new CVFeedUtilities();
public Mixtures(FrmProdChoose form)
{
InitializeComponent();
this.form = form;
}
private void btnHome_Click(object sender, EventArgs e)
{
form.Selected = selectedDefault;
utilities.closePopUp(this, form);
}
private void Mixtures_Load(object sender, EventArgs e)
{
tiles = new MetroFramework.Controls.MetroTile[] { tileOne, tileTwo, tileThree, tileFour};
for (int i = 0; i < mixture.Length; i++)
tiles[i].Text = mixture[i];
}
private void tileOne_Click(object sender, EventArgs e)
{
tileOne.BackColor = Color.ForestGreen;
removeBackColor(1);
}
private void tileTwo_Click(object sender, EventArgs e)
{
tileTwo.BackColor = Color.ForestGreen;
removeBackColor(2);
}
private void tileThree_Click(object sender, EventArgs e)
{
tileThree.BackColor = Color.ForestGreen;
removeBackColor(3);
}
private void tileFour_Click(object sender, EventArgs e)
{
tileFour.BackColor = Color.ForestGreen;
removeBackColor(4);
}
private void tileFive_Click(object sender, EventArgs e)
{
tileFive.BackColor = Color.ForestGreen;
removeBackColor(5);
}
public void removeBackColor(int index)
{
for (int i = 0; i < tiles.Length; i++)
{
if (i == index - 1)
{
selectedDefault = tiles[i].Text;
continue;
}
else tiles[i].BackColor = Color.DeepSkyBlue;
}
}
}
}
and the functions loadPopUp and closePopUp
public void loadPopUp(Form from, Form to)
{
to.Tag = from;
to.Show(from);
}
public void closePopUp(Form from, Form to)
{
to.Tag = from;
if (!to.Visible)
to.Show(from);
from.Hide();
}

In which part of the program should I put a function that keeps the application running continuosly in c#

Form1.cs
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.Text.RegularExpressions;
namespace Temporizador
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
/*if (escolherHoraTxt.InvokeRequired == true)
escolherHoraTxt.Invoke((MethodInvoker)delegate { escolherHoraTxt.Text = "Invoke was needed"; });*/
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void escolherFicheiroBtn_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.ShowDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string fileName;
fileName = dlg.FileName;
link.Text = fileName;
//MessageBox.Show(fileName);
}
}
private void escolherHoraTxt_MouseClick(object sender, EventArgs e)
{
if (escolherHoraTxt.Text == "--:--:--")
escolherHoraTxt.Text = " ";
}
private void escolherHoraTxt_TextChanged(object sender, EventArgs e)
{
if (escolherHoraTxt.Text == "--:--:--")
escolherHoraTxt.Text = " ";
}
private void gravarBtn_Click(object sender, EventArgs e)
{
}
private void gravarBtn_Click_1(object sender, EventArgs e)
{
String s = escolherHoraTxt.Text;
Horas hora = new Horas();
hora.IsValidTime(s);
hora.compareTime(s);
}
}
}
Horas.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Temporizador
{
class Horas
{
private String m_timeNow;
public String timeNow
{
get
{
return m_timeNow;
}
set
{
m_timeNow = value;
}
}
public Horas()
{
}
public string getCurrentTime()
{
m_timeNow=DateTime.Now.ToString("HH:mm:ss");
return m_timeNow;
}
public void IsValidTime(String theTime)
{
string[] timeArray = theTime.Split(new[] { ":" }, StringSplitOptions.None);
try{
Convert.ToDateTime(theTime);
}
catch (FormatException e)
{
MessageBox.Show(e.Message);
}
}
public void compareTime(String theTime)
{
string currentTime=getCurrentTime();
if (currentTime == theTime)
MessageBox.Show("SIM");
else
MessageBox.Show("NAO");
}
}
}
/I´m trying to make an application that it is always running and that will close and then open a file in a certain hour choosen by the user. I intend to use something like while(1){...} but I don´t know in what part of the program should I put this. Could someone help me please?/

adding a counter class to winform

I have a project that wants
A method that returns the current count.
A Constructor that sets the count to zero.
I have the first few down but need help with the return count to 0 and then the constructor. I need to do this by adding a counter class but I'm confused about the way to add it.
Can some one help me out?
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 Project10TC
{
public partial class Form1 : Form
{
int zero = 0;
int i = 1;
public Form1()
{
InitializeComponent();
}
private EventHandler myCounter;
// end of Form class
private class myCounter()
{
myCounter = new myCounter( );
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Clark\nCS 1400\n Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString();
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = (zero).ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
public class Counter
{
public int Value { get; private set; }
public void Increment()
{
Value = Value + 1;
}
public void Decrement()
{
if (Value > 0) Value = Value - 1;
}
public Counter()
{
Value = 0;
}
}

Categories

Resources