Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I make some application then i need function for close all active window in c#
but i need to open new one after that
so my code is..
for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
App.Current.Windows[intCounter].Close();
Dashboard mf2 = new Dashboard();
mf2.mastersupplier_clicked(sender, new RoutedEventArgs());
mf2.ShowDialog();
but my application is exit and never show the dashboard dialog back.... any idea about this? im stuck in this function,.
i make this with wpf c#
Set the ShutdownMode of the App to ShutdownMode.OnExplicitShutdown before you close the windows:
App.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
App.Current.Windows[intCounter].Close();
Dashboard mf2 = new Dashboard();
mf2.mastersupplier_clicked(sender, new RoutedEventArgs());
//shut down the app explicitly when the dashboard dialog is closed:
mf2.Closed += (ss, ee) => App.Current.Shutdown();
mf2.ShowDialog();
test this code
Dashboard mf2 = new Dashboard();
mf2.mastersupplier_clicked(sender, new RoutedEventArgs());
mf2.Show();
for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
{
var wnd = App.Current.Windows[intCounter] as Window;
if(wnd != mf2)
{
wnd.Owner = mf2;
wnd.Close();
}
}
Related
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
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
When I try to use my method in created thread, thread skips 'if' statement in a loop. The problem is label4 is always shows else state. What should I do for the right operation? Here is my code;
This is where I create threads;
for(int i = 0; i <threads; i++)
{
var tempThread = new Thread(new ThreadStart(DoWork));
tempThread.Name = i.ToString();
tempThread.Start();
}
CheckForIllegalCrossThreadCalls = false;
And this is what my thread method do;
public void DoWork()
{
for(int i=2; i < variable/ 2; i++)
{
if (variable% i == 0) label4.Text ="yes not";
else label4.Text = "yes";
}
}
I'm guessing this is what you want to do, but since you haven't told us your goals we can only guess
public void DoWork()
{
for(int i=2; i < variable/ 2; i++)
{
if (variable% i == 0) {
label4.Text ="yes not";
return;
}
}
label4.Text = "yes";
}
I don't use C# but if DoWork is how you create a Thread then I think it's a bit much for something that should take almost no time at all to finish.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want the timer to stop when the ImageNumber is equal to zero.
private void Health_Regen_Tick(object sender, EventArgs e)
{
if (ImageNumber1 == 0)
Health_Regen.Enabled = false;
if (ImageNumber1 < 20)
{
ImageNumber1++
HealthBar.Image = Image.FromFile(path + ImageNumber1.ToString() + ".png");
}
}
If I add a return statement after the first if statement the second if statement is disabled.
I want the timer to stop when the ImageNumber is equal to zero.
You appear to already know how to do this, assuming Health_Regen is a Timer then:
Health_Regen.Enabled = false;
Will disable your Timer.
If I add a return statement after the first if statement the second if statement is disabled.
That is to be expected as you are using the return keyword which will prevent anything after it within the same code block from being executed.
Your question does not make exactly what you are asking clear, however, I am assuming from your comment that the second if statement is not executed that you want to update the HealthBar.Image value even if the Timer is disabled? If so then something like below should work for you
private void Health_Regen_Tick(object sender, EventArgs e)
{
if (ImageNumber1 == 0)
{
Health_Regen.Enabled = false;
}
else if (ImageNumber1 <= 20)
{
ImageNumber1 += 1;
}
HealthBar.Image = Image.FromFile(path + ImageNumber1.ToString() + ".png";
}
You should just be able to call the Stop method on the timer instance.
if (ImageNumber == 0)
{
Health_Regen.Stop();
}
This is just a guess!.
Replace
if (ImageNumber1 == 0)
with
if (ImageNumber1 >= 20)
Your timer will already stop when ImageNumber1 equals 0, but it just never counts down to 0.
Also if you change ImageNumber1 to 0. It may already be running the timer which will increment it by one and skip the stopper, so pretty much the way you have it coded it's based on luck right now.
The luck happens if you change ImageNumber1 while timer is already running.
Try this:
if (ImageNumber1 == 0)
Health_Regen.Enabled = false;
else if (ImageNumber1 < 20)
{
ImageNumber1++;
HealthBar.Image = Image.FromFile(path + ImageNumber1.ToString() + ".png");
}
Still the luck may still happen best you can do is stop the timer outside the timer when you make it ImageNumber1 = 0;
Best Fix:
Also a better solution is to fix your program to start at ImageNumber1 = 20 and just keep going down to 0.. when it hits 0 stop the timer.. you shouldn't make it this confusing all you will have to do is rename a few png files in a new order! instead of
1.png now it will be 20.png
2.png now it will be 19.png
and so on and so on..
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm encountering an extremely strange behaviour with this code fragment.
Normally the for Statement doesnt enter when counted to 16 but leaves.
In my case it enters with i=16 and executes the the Zero-Case which ends in an ArrayOutOfBoundsException.
This behavior is repeated every time I call foo()
Is there any explanation for this?
void foo()
{
Thread.Sleep(2000);
try
{
for (int i = 0; i < 16; i++)
{
#region Switch
(switch on 0 to 15 and do something unrelated)
#endregion
}
}
catch (Exception ex)
{
MessageBox.Show("Exception occured: " + ex);
}
}
Why not loop upto your array length?
for (int i = 0; i < array.Length; i++)
{
//Your secret code here
}
Also make sure you're not changing the value of i inside the loop anywhere.
You'll never get the exception with this code(unless you modify i inside the loop) irrespective of array Length.
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