how to make best player screen? [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
i am using visual studio 2008 c# winform. . i've make sudoku game which is working well . . i want to make best player screen for it and score depend on how much time the player take to complete game . .
i am using another form to take player name when he meets the condition for best player and give the name to label on main form but its not working.here is my code:
private void button1_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
main.lbBEN.Text = textBox1.Text;
this.Close();
}
and this on another form:
if (emint<bmint)
{
best b = new best();
b.ShowDialog();
}
please guide me. . .THANK you

Add a public property to the second form and just below the ShowDialog(), sets the form1 label.Text to that property containing the name of the user.
public partial class Form2 : Form
{
string _highestScoreUser = string.Empty;
public Form2()
{
}
public string HighestScoreUser
{
get{ return _highestScoreUser; }
set{ _highestScoreUser = value; }
}
}
In Form1 code after ShowDialog is called like
{
Form2 form = new Form2();
form.ShowDialog();
form1.label.Text = form.HighestScoreUser;
}
Hope this help

You've created a brand new Form1 object unrelated to the Form1 that is already on the screen. You need to somehow pass a reference to the real Form1 the the secondary form.

Related

Minimize all running windows when application runs [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am working on a C# windows form application. How can i edit my code in a way that when more than 2 faces is being detected by my webcam.
More information:
When "FaceDetectedLabel.Text = "Faces Detected : " + cam.facesdetected.ToString();" becomes Face Detected: 2 or more...
How can i do the following:
Minimize all program running except my application.
Here is my code:
namespace PBD
{
public partial class MainPage : Form
{
//declaring global variables
private Capture capture; //takes images from camera as image frames
public MainPage()
{
InitializeComponent();
}
private void ProcessFrame(object sender, EventArgs arg)
{
Wrapper cam = new Wrapper();
//show the image in the EmguCV ImageBox
WebcamPictureBox.Image = cam.start_cam(capture).Resize(390, 243, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC).ToBitmap();
FaceDetectedLabel.Text = "Faces Detected : " + cam.facesdetected.ToString();
}
private void MainPage_Load(object sender, EventArgs e)
{
#region if capture is not created, create it now
if (capture == null)
{
try
{
capture = new Capture();
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
#endregion
Application.Idle += ProcessFrame;
}
check this post:
How to programmatically minimize opened window folders
You'll have to enumerate all processes and exclude the current one (yours) instead of getting the "explorer" one. I'd suggest also putting exception handling and some checks in place since not all processes have windows to be minimized
Also this post for the log off part:
Log off user from Win XP programmatically in C#

How to move PictureBox with Thread? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm learning threads in C# so my first program will be 2 images that will be moving. But the problem is that I get an error when I try to do a new point in a thread:
Here's my code:
namespace TADP___11___EjercicioHilosDatos
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int x = 0;
int y = 0;
private void Form1_Load(object sender, EventArgs e)
{
Thread Proceso1 = new Thread(new ThreadStart(Hilo1));
Proceso1.Start();
}
public void Hilo1()
{
while (true)
{
x = pictureBox1.Location.X - 1;
y = pictureBox1.Location.Y;
pictureBox1.Location = new Point(x, y);
}
}
}
}
You can only update a control from the thread that control was created on. Controls do have an Invoke method that you can call from another thread. This method takes a delegate that specifies the work you would like to do on the control's thread:
var updateAction = new Action(() => { pictureBox1.Location = new Point(x,y); });
pictureBox1.Invoke(updateAction);
You have to Invoke it. For [obvious] reasons, you can't access controls created by a different thread so you have to use a delegate. Several similar SO questions:
How to update the GUI from another thread in C#? (111 upvotes)
Writing to a textBox using two threads
How to update textbox on GUI from another thread in C#
Writing to a TextBox from another thread?
If you check out the first link, Ian's great answer will demonstrate how you should do this in .Net 2.0 and 3.0. Or you can scroll down to the next answer, Marc's, which will show you how to do it in the simplest way.
Code:
//worker thread
Point newPoint = new Point(someX, someY);
this.Invoke((MethodInvoker)delegate {
pictureBox1.Location = newPoint;
// runs on UI thread
});

Sending Array List Between Forms [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Currently I am sending one Array List from form2 to form 1 and it works fine.
Form1 form2 = new Form1(this, SampleArrayList); //pass form reference and an arraylist
form2.Show();
this.Hide();
And on form1 I associate SampleArrayList with local Array List.
Form2 formParent;
ArrayList SampleArrayList;
public MainForm(Form2 par, ArrayList _SampleArrayList)
{
InitializeComponent();
this.formParent = par;
this.SampleArrayList = _SampleArrayList;
}
However I want to avoid creating new instance of Form1
form2 = new Form1(this, SampleArrayList);
I want to send Array List to currently running instance of Form1. What would be best way to do this. Thank you
Quote of OP in the comments above:
Form1 is created first, clicking add button creates Form2. Clicking
submit button hides Form2 and creates new instance of Form1 along with
old instance which I didn't want to dispose of. I want to click submit
button and go back to running instance of Form1.
That's really a larger problem then. Here's a nice solution that I like.
public partial class Form1 : Form
{
private string dataFromThisForm; //can be whatever
private void button1_Click(object sender, EventArgs e)
{
Form2 otherForm = new Form2();
//pass some data to other form
otherForm.SomeData = dataFromThisForm;
this.Hide();
otherForm.Show();
//when the other form is closed
otherForm.FormClosed += (sender2, e2) =>
{
this.Show();
string newData = otherForm.NewData;
};
//when the other form is hidden.
otherForm.VisibleChanged += (sender2, e2) =>
{
this.Show();
string newData = otherForm.NewData;
};
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//Use SomeData to populate controls.
}
public string SomeData { get; set; } //data passed in from other form
public string NewData { get; private set; } //data to expose to other form
private void button1_Click(object sender, EventArgs e)
{
NewData = "SomeDataToPassToForm1";
//this.Close();
this.Hide();
}
}
A few notes:
You're passing around an ArrayList, rather than strings as I did in
this example. An ArrayList is a mutable reference type, whereas
string is immutable. This means that you can just modify the
ArrayList passed to Form2, and those changes will be reflected in
the variable in Form1 since they both point to the same underlying
ArrayList. I left this code in here as it covers the general case
though.
You say that you hide Form2 when you click the submit button.
Normally in this design you would close it since it won't be needed
anymore. If you really don't plan to use it again I suggest closing
it. If you really do plan to show that form again then just hiding
it is fine.
If you close Form2 on submit it will fire the FormClosing event, if
you just hide it it will fire the Visible event. You should probably
remove one of those two event handlers in my code depending on
whether you actually close it or hide it. If you sometimes do one and
sometimes the other then feel free to leave both. You won't actually
harm anything (other than confusing people) if you leave both in even
if you only use one.
A static class that would act as a communications platform within your application could solve the problem or both forms could be owned by the same object.
I would add a method to Form1 that accepts the Array list.
Something like:
public void Setup(ArrayList SampleArrayList)
{
// Do what you need here...
}
Then you could call this method from inside your constructor (to keep the constructor the same), and call Setup when you need to change the list the Form is using.
Your code above would be like this:
// If you needed a new instance of Form1, it wouldn't change
Form1 form2 = new Form1(this, SampleArrayList); //pass form reference and an arraylist
form2.Show();
this.Hide();
// If you already had an instance in the form2 variable
form2.Setup(SampleArrayList);
No matter what, if you are using .NET 2.0 or greater, I would recommend using a generic List to store list data instead of ArrayList.
You can write a static property in the Form1 class if you don't need to create a instance of it. For example:
private static ArrayList _SampleArrayList;
public static ArrayList SampleArrayList
{
get { return _SampleArrayList; }
set { _SampleArrayList = value; }
}
and when you want update the ArrayList write simply:
Form1.SampleArrayList = SampleArrayList;
PS: I reccomend you use a List<> instead of an ArrayList.
If you don't know the type instead of ArrayList use List<object> else List<T>.
EDIT:
If form1 and form2 are both an istance of the same class you only have to make the ArrayList static.

There is an error in my C# program that occur a stack overflow Exception [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
i have 2 forms. one of them is a form that User select a school class so click OK. I use
public int[] grad_class=new int[2];
for save which School class has selected. (list of classes is COMBO BOX)
after that i open another new form to edit information of that class' students.
i use Switch like this:
switch(grad_class)
{
case "11": something to do..., break;
case "12": something to do..., break;
}
But My problem :
I define "grad_class" in form No.1 . so when i want to use that i have to do this:
form1 f1 = new form1();
f1.grade_class;
and in form No.2 i call this code.
in form No.1 i call:
form2 f2 = new form2();
OCCUR STACK OVERFLOW EXCEPTION !!!
You state:
form1 f1 = new form1();
f1.grade_class;
and in form No.2 i call this code. in form No.1 i call:
form2 f2 = new form2();
Well, there's your issue right there. You state that in form1 you call form2.ctor and in form2 you call form1.ctor. So your code looks like this:
public form1() {
var f2 = new form2();
}
public form2() {
var f1 = new form1();
}
Well, of course this results in a stack overflow. These are mutally recursive functions. You have no return condition, so you exhaust your stack space. Quite simply, what is happening is that the machine has to store state about where to go next when a method is finished. It typically stores this information in space called the stack. The stack is bounded though, so if you push too many "this is where to go next" blocks onto the stack by repeatedly invoking methods, you will exhaust this stack space and hit the infamous stack overflow exception.
Edit: You said:
namespace TeacherBook {
public partial class ChooseGrade : Form {
public int[] grad_class=new int[2]; //int array for grade & class (first for Grade,second for class)
EditStudent StdForm = new EditStudent();
}
namespace TeacherBook {
public partial class EditStudent : Form {
ChooseGrade ChGrade = new ChooseGrade(); // define form
}
}
Effectively what I suspected. You have a field initializer in ChooseGrade that instantiates a new instance of EditStudent. But EditStudent has a field initializer that instantiates a new instance of ChooseGrade. So when you instantiate a new instance of ChooseGrade, this causes the constructor for EditStudent to be invoked, which causes the constructor for ChooseGrade to be invoked, which causes the constructor for EditStudent to be invoked, and on and on it goes until you overflow your stack.

C# Windows Forms - outputting info to a textbox without crashing [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
In a Windows Forms app, I am using Quartz.NET to run some tasks every few minutes. Previously, this application was a console application that was invoked based on a schedule but for various reasons this wasn't ideal - back then all debug info was outputted to the console.
In this version, I need a way to show the debug information for a job on the user's screen. My initial idea was a new form that is shown when a job is run, and all debug information is appended to a multiline textbox on that form. However, this doesn't work as most of the app seems to crash when I do this.
Any other ideas?
EDIT: Sorry for any confusion. This is what's called when a job executes:
public virtual void Execute(JobExecutionContext context)
{
RunJob jobForm = new RunJob();
jobForm.Show();
jobForm.JobLabel = context.JobDetail.JobDataMap.GetString("Name");
for (int i = 0; i < 100; i++)
{
jobForm.WriteLine(i.ToString());
}
jobForm.Hide();
}
And this is the contents of the 'RunJob' form:
public partial class RunJob : Form
{
public string JobLabel
{
get
{
return lblJobName.Text;
}
set
{
lblJobName.Text = value;
}
}
public void WriteLine(string text)
{
textBox1.AppendText(text + Environment.NewLine);
}
public RunJob()
{
InitializeComponent();
}
}
Basically, the RunJob window freezes when the text is being appended, when ideally it'd just add the text smoothly. I understand 'crash' was a very poor choice of word! My excuse is that it's early in the morning, ahem
i dont see anything wrong in above code that could lead it to deadlock or whatever it is, except this line: jobForm.JobLabel = context.JobDetail.JobDataMap.GetString("Name");
what does this line does, there must be something wrong in this line,
try putting some hard coded string instead of context.JobDetail.JobDataMap.GetString("Name"); and tell me if it still doesn't work.
Job and UI form should obviously be at separate threads. Job thread should write to the form and then close it when unneeded.
Presuming you have an Action action that is invoked on UI thread:
public virtual void Execute(JobExecutionContext context)
{
RunJob jobForm;
var name = context.JobDetail.JobDataMap.GetString("Name");
action(()=> { jobForm = new RunJob(); jobForm.Show(); jobForm.JobLabel = name;});
for (int i = 0; i < 100; i++)
{
var txt = i.ToString();
action(()=>{ jobForm.WriteLine(txt); });
}
action(()=> { jobForm.Hide(); });
}

Categories