Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I had some difficulty trying to make my program make buttons for each song, like say I have 6 songs in a folder, what program should do is get them all in and generate button to each song and when I press them they will play their own song. I can't really even think where to start at this point, I got FolderBrowsingDiaglog running I just need to figure this part out. I added the code that you gave me and it gives me an error of that i have illegal characters in path.
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderChoice = new FolderBrowserDialog();
if (folderChoice.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
int i = 0;
foreach (string fname in System.IO.Directory.GetFiles(folderChoice.SelectedPath + #"*.mp3"))
{
Button btn = new Button
{
Text = fname.Split('\\').LastOrDefault(),
Location = new Point(10, 10 + i++ * 30), //sample x,y
Size = new Size(100, 20),
Tag = fname, //For having the file location
};
this.Controls.Add(btn);
}
}
}
Well creating buttons is easy:
if(folderBrDlg.ShowDialog()==DialogResult.Ok)
{
int i = 0;
foreach(string fname in System.IO.Directory.GetFiles())
{
Button btn = new Button
{
Text = fname.Split('\\').LastOrDefault(),
Location = new Point(10, 10 + i++ * 30), //sample x,y
Size = new Size(100,20),
Tag = fname, //For having the file location
....
};
btn.Click += (s, e) =>
{
string fileToPlay = (string)((Button)s).Tag;
//now you only have to play it.
}
this.Controls.Add(btn);
}
}
Update:
I added the click event for the button to that on click of every button you can get the path of the related file, and you just have to use a player to play it.
This is my understanding of the question.
1. You want to make a program that plays one of your six songs when one of the buttons are clicked
FolderbrowsingDialog will allow to choose the files you want to play but it won't let you play them. Consider using the SoundPlayer class. SoundPlayer will give you the functionality to play the songs but these songs need to be .wav files.The microsoft link will help you get a better about the SoundPlayer class.
From this you should be able to finish your program.
https://learn.microsoft.com/en-us/dotnet/api/system.media.soundplayer?redirectedfrom=MSDN&view=netframework-4.7.2
Related
I don't know if I still have missing on this but whenever you want to add PictureBox, you just find it on Toolbox and drag it on the UI. But what I did is I coded it by adding PictureBox then what I want is I have to move it to the right. I coded it on a Timer and this is how I do.
private void enemyMove_Tick(object sender, EventArgs e)
{
GameMenu menu = new GameMenu();
if (menu.cmbox_Level.Text.Equals("Easy"))
{
this.Controls.Add(menu.EnemyTank);
menu.EnemyTank.Width = 26;
menu.EnemyTank.Height = 32;
menu.EnemyTank.BackgroundImage = Properties.Resources.Tank_RIGHT_v_2;
menu.EnemyTank.BackgroundImageLayout = ImageLayout.Stretch;
menu.EnemyTank.Left += 2;
}
}
Don't ask me if the Timer is started. Yes it already started, it was coded on the Form. But somehow when I start the program it doesn't move. But then I tried adding PictureBox, this time I tried dragging it to UI then add an Image on it, then coded it by moving to the right. When I start the program it works. But what I want is that whenever I start the button it will just make the random PictureBox move to the right. Idk what's the missing in here.
I made some assumptions. If I my assumptions aren't correct, please update your post for details. Following content is copied from my comment.
A new GameMenu will always create a new EnemyTank.
A EnemyTank's default Left value is fixed;
Your enemyMove_Tick is keep creating GameMenu and EnemyTank.
That is, every tick, a EnemyTank is created and value changed to "default value plus 2" in your code.
To avoid it, you have to keep EnemyTank instance somewhere instead of keep creating it.
Here's a example.
GameMenu m_menu;
void YourConstructor()
{
// InitializeComponent();
// something else in your form constructor
m_menu = new GameMenu();
m_menu.EnemyTank.Width = 26;
m_menu.EnemyTank.Height = 32;
m_menu.EnemyTank.BackgroundImage = Properties.Resources.Tank_RIGHT_v_2;
m_menu.EnemyTank.BackgroundImageLayout = ImageLayout.Stretch;
this.Controls.Add( m_menu.EnemyTank );
}
private void enemyMove_Tick(object sender, EventArgs e)
{
if (menu.cmbox_Level.Text.Equals("Easy"))
{
m_menu.EnemyTank.Left += 2;
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I want to retrieve data from notepad and display it randomly on label. The project is about lucky draw system where i need to make the employee id display randomly.
Below are my code:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("C:/ Users / Nor Asyraf Mohd No / Documents / TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
string strRead = sr.ReadLine();
Random random = new Random();
Label1.Text = strRead.ToString();
}
}
}
catch (Exception i)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(i.Message);
}
}
There are a couple of things wrong with your code. First, you should declare your Random variable once rather than inside a loop. Since Random uses the system time as a seed, if it's initialized inside a loop that's running quickly, you will likely get the same "random" number each iteration. I usually just declare it as a class field so it can be used by other methods as well.
Similarly, you can store the file path as a class level variable rather than each time the method is called. And if the file contents aren't changing, you should consider saving them in a List<string> class field one time at startup, rather than reading the file each time. But I'm going to assume that the file contents may change, so I'll leave that code in the method.
Next, you can use the File.ReadAllLines to get all the lines of the file at once, and then you can access a random index in the resulting string array of file lines.
In order to ensure you don't display the same item more than once, you can read the file lines once into a List<string>, and then, after you choose a random item from that list, remove it so it isn't used again.
For example:
private Random random = new Random();
private string testFilePath = #"c:\users\nor asyraf mohd no\documents\testfile.txt";
private List<string> testFileLines;
private void Form1_Load(object sender, EventArgs e)
{
if (!File.Exists(testFilePath))
{
MessageBox.Show($"The file does not exist: {testFilePath}");
}
else
{
try
{
testFileLines = File.ReadAllLines(testFilePath).ToList();
}
catch (Exception ex)
{
MessageBox.Show($"Could not read file {testFilePath}. Exception details: {ex}");
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (testFileLines != null && testFileLines.Count > 0)
{
var randomLine = testFileLines[random.Next(testFileLines.Count)];
Label1.Text = randomLine;
testFileLines.Remove(randomLine);
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a C# application in which it does some basic printing options to Zebraprinets.
In my application i am opening the .net paint using the C# application. By using .net paint i need to convert the a .jpeg file to pcx format. I am using it now manually..
So my real question is, can i perform all these actions from my C# application. i want my application to perform the functions of .net paints
For example. after opening the .net paint. I want to import the file using Open + O..Select file from folder #c:\users\user\sample.jpeg then in .net paint perform the functions (ctrl + Shift + L) then (ctrl + Shift + G). After all these save ctrl + Shift + S in location C:\out\.
Can i do this.
Please let me know.
code snippet:-
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
string filename = "";
if (ofd.ShowDialog() == DialogResult.OK)
{
filename = System.IO.Path.GetFullPath(ofd.FileName);
}
// MessageBox.Show(filename, "file");
pictureBox1.ImageLocation = filename;
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
DialogResult result = MessageBox.Show("Do you wish to continue?", "Save Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
System.Diagnostics.Process.Start(#"C:\Program Files\Paint.NET\PaintDotNet.exe");
// here i need to perform the function like
//Open + O`
//ctrl + Shift + L)` then `
//(ctrl + Shift + G)`. then save
//`ctrl + Shift + S`
}
else
{
return;
}
}
You can go and download Magick.NET. It is a .NET graphics manipulation library that supports over 100 image formats including PCX. From their website, here is an example of how to convert one image format to another.
EDIT
Here is an example to convert a jpeg to a pcx:
using ImageMagick;
namespace JpegToPcx
{
class Program
{
static void Main(string[] args)
{
using (MagickImage image = new MagickImage("MyFile.jpeg"))
{
image.Write("MyFile.pcx");
}
}
}
}
You can just reference the Paint.NET assemblies directly and use a surprising amount of its functionality that way. I don't know about the API you are referring to specifically but I have used it in the past to generate sprites from .pdn files in a compile pipeline.
Try adding a reference to these assemblies: C:\Program Files\Paint.NET\PaintDotNet.*.dll Then poke around the classes in those namespaces.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am creating a desktop application that adds/edits users. Both functions will use the same inputs however each function will require different click handlers to update data and to insert new data into my database however I want to utilize the same buttons
Is their a way I can assign different click handlers to the same button?
Yes you can,
//Register both handlers within constructor
button1.Click += new EventHandler(InsertOperationHandler);
button1.Click += new EventHandler(UpdateOperationHandler);
-
private void InsertOperationHandler(object sender, EventArgs e)
{
}
private void UpdateOperationHandler(object sender, EventArgs e)
{
}
The solution I came up with is as follows:
public void displayEditButtons()
{
buttonPeopleOne.Text = "Save Changes";
this.buttonPeopleOne.Click -= new System.EventHandler(this.buttonPeopleOneClear_Click);
this.buttonPeopleOne.Click += new System.EventHandler(this.buttonPeopleOneSave_Click);
buttonPeopleTwo.Text = "Delete User";
this.buttonPeopleTwo.Click -= new System.EventHandler(this.buttonPeopleTwoNext_Click);
this.buttonPeopleTwo.Click += new System.EventHandler(this.buttonPeopleTwoDelete_Click);
//change other controls to match the function context
buttonPeopleChangeFunction.Visible = true;
this.labelPeopleFunctionHeader.Text = "Edit Current User";
}
public void displayAddButtons()
{
buttonPeopleOne.Text = "Clear";
this.buttonPeopleOne.Click -= new System.EventHandler(this.buttonPeopleOneSave_Click);
this.buttonPeopleOne.Click += new System.EventHandler(this.buttonPeopleOneClear_Click);
buttonPeopleTwo.Text = "Add Contact";
this.buttonPeopleTwo.Click -= new System.EventHandler(this.buttonPeopleTwoDelete_Click);
this.buttonPeopleTwo.Click += new System.EventHandler(this.buttonPeopleTwoNext_Click);
//change other controls to match the function context
buttonPeopleChangeFunction.Visible = false;
this.labelPeopleFunctionHeader.Text = "Add New User";
}
I simply call the different methods when I need to change the buttons
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
class Puzzle
{
public int PUZZLESIZE = 3;
public int numSteps = 0;
public Button[,] buttons;
public Puzzle(Form1 form1)
{
buttons = new Button[3, 3] { { form1.button1, form1.button2, form1.button3 },
{ form1.button4, form1.button5, form1.button6 },
{ form1.button7, form1.button8, form1.button9 } };
}
public Puzzle Clone()
{
return (Puzzle)this.MemberwiseClone();
}
public void reset()
{
//reset all 9 buttons color
}
public void flipcells()
{
//flip cells in the puzzle with color change
}
class Undo
{
Puzzle newPuzzle; //null value here. Why???
public Undo(Puzzle oldPuzzle)
{
Puzzle newPuzzle = oldPuzzle.Clone();
}
public void undo()
{
//back to previous step, ie the color of the buttons go back to previous color
}
I'm asking to do the undo function for back to previous stages for max four times. "The easiest way to do this is to create a copy of the puzzle and store it in an array of Puzzle. To do this I implemented a Clone method for puzzle. This returns a brand new puzzle set to exactly the same settings as the puzzle that I called Clone on." This is what our instructor says, but i still have no idea of how to implement this.
The easiest way to implement your "Undo" function would probably be with a stack. For each action you take on the puzzle, push an instance onto the stack. When the user opts to undo a move, pop the instance off of the stack.
See this article on Wikipedia for more information on stacks, and this MSDN article on the .NET Stack generic class.
Here's a little example of how to implement the stack mentioned by DBM. In stead of cloning the whole Puzzle class, I would recommend to rather just clone the array of Buttons (that should be enough for your Undo function):
Edit: If you need to keep track of the colors of the buttons in stead of the position of the buttons, you could probably rather just put an array of the buttons' current colors on the stack.
Stack<Button[,]> stack = new Stack<Button[,]>();
private void button4_Click(object sender, EventArgs e)
{
Button[,] buttons = new Button[2, 2] { { button1, button2 }, { button3, button4 } };
stack.Push((Button[,])buttons.Clone());
buttons[0, 0] = button2;
buttons[0, 1] = button1;
stack.Push((Button[,])buttons.Clone());
buttons[1, 0] = button4;
buttons[1, 1] = button3;
stack.Push((Button[,])buttons.Clone());
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (stack.Count > 0)
{
Button[,] buttons = stack.Pop();
txtButtonOrder.Text = buttons[0, 0].Text + buttons[0, 1].Text + buttons[1, 0].Text + buttons[1, 1].Text;
}
else
{
timer1.Enabled = false;
}
}
When you declare a variable inside a method, it will hide any variables in the class which have the same name. Instead of redeclaring the variable, you should just reference it:
int myVariable = 0; // Will always be 0
public void SetMyVariable()
{
int myVariable = 5; // Isn't the same as above
}
If you want to be explicit, you can add 'this.' in front of the variable name to always refer to the class variable:
this.myVariable = 5;