C# window Form , that run two forms - c#

Good day guys, I checked for previous question, yes i saw them, but it was not really solving my problems.
I am writing an application that has two forms, there is a mainForm and Password form.
The form is for scanning two code and match if they are equal. you scan the first code and then the second code. If they Match it pop-up the green lights to show that they match. If the code does not match it pop-up a red light and a Password form at the same time and locks the form textbox to ReadOnly for supervisor to verify what the user has scanned.
It is doing all this.
The problem is i want when the supervisor enter Password it must show the form again so that the user can continue scanning, it is doing this. But the thing is when it open a new form, the one at the back that was locked it does not disapear. i tried to reset the form, it does not work. Please help in this.
//object references of the Main Form
MainForm fm=new MainForm();
if ((txtPassword.Text =="This"
{
fm.BringToFront();
//Reset the form to a normal state
fm.lblResult.Visible = false;
fm.txtResult.Visible = false;
fm.chkMtn.Checked = false;
fm.chkVodacom.Checked = false;
//Clear the textBox of the form
fm.txtMainFormScan1.Text="";
fm.txtMainFormScan2.Text="";
set the focus on the fist Scan
fm.txtFirstScan.Focus();
}
The code above does not work. now i am using the one below.
if ((txtPassword.Text =="ThisPassword")
{
lblInstructPassword.Text = "Password correct";
txtPassword.Text = "";
fm.ControlBox = true;
//fm.Activate();
this.Hide(); //This Close the Password form
fm.Show();
fm.chkMtn.Checked = true;
}
Even when i use the //fm.ShowDialog, i cannot win. I also tried to use the Mutex to allow only one instance of an application to run. result are Zero. Thanks for your help in advance.

You're doing a lot of work that .Net should be doing for you.
All else being mutable details, your real problem is this:
You have a working form that compares two values.
If the comparison result is false, you need a modal window to appear to prompt for a password.
If the password is correct, you need the first form to be editable again and the password form to disappear.
You were on the right path using ShowDialog(), as this does disable the window that fires the command to show the dialog. You need your password logic to be a part of that dialog form, not the parent form. This will allow you to return a DialogResult enum value to the parent form (if any value at all) and get you around the needless Focus() tossing.
If you want the calling form to disappear and reappear when you do this, wrap the call to your form's ShowDialog() call in this.Hide() and this.Show()

Related

C# ChromiumWebBrowser: Prevent control from stealing focus

I'm making a program that has a Form with a ChromiumWebBrowser in it. The navigation is done automatically. When webbrowser complete it's task, I'll dispose it, create a new webbrowser, add it to form, and load a new address.
But, when the new webbrowser was created and added to form, the program jumps in front of what ever other program is in the top with focus. Example: I start my program, press the button to start its task, open notepad to type some text and my program jumps in front of it when navigating to a new site.
Even when the window is minimized, it still steals focus from other open programs.
How do I prevent it stealing focus after it is created?
As #amaitland said, this looks like a bug.
Workarounds I've used are:
1) disable the browser. This will prevent the browser from receiving mouse/keyboard input, but it won't "grey-out" the control.
Browser1 = New CefSharp.WinForms.ChromiumWebBrowser(url)
Browser1.Enabled = False
2) Pass a callback .net function for when the page loads where you simply put the focus back to winforms by focusing on a label of your choice.
Label1.Focus()
Your Form need set: this.Topmost = false;
AND just set: this.BringToFront();
Add new browser to the form just like the function as follow:
private ChromiumWebBrowser AddNewBrowser(FATabStripItem tabStrip, String url)
{
if (url == "")
{
url = OpenUrl;
txtUrl.Select();
txtUrl.Focus();
}
else
{
tabStrip.Select();
tabStrip.Focus();
}
// ...
}
Hope has help to you. Thanks !

Showing a modeless form's 'title bar' inside the parent form when it's minimized

I'm trying to implement some complement views inside my application and I would like to have a better layout control over them. I don't know how to explain in words what my desired functionality is, so I made it through with some photoshop help, hoping you could give me a hand to implement it.
My application now looks like this:
(i need reputation to post images so.. sorry for the links)
http://i59.tinypic.com/2ikv8m1.jpg
When I minimize the modeless form which is focused in the previous image, I would like to be able to see it (and handle it to maximize or close) inside my main form as I show in the image below (made it in photoshop)
http://i58.tinypic.com/1e28go.jpg
Hope someone can lead my into a solution and thanks for the support.
EDIT: I need to be able to move that form outside my main form, even to a different monitor.
If you don't want to use the MDI approach, then set TopLevel of the modeless Form to false and add it to the main Forms Controls collection before showing it:
Form frm = new Form();
frm.TopLevel = false;
this.Controls.Add(frm);
frm.Show();
*Obviously changing Form to the correct type of your modeless form.
If i understand what you are trying to do, you want to minimize a certain form but still see it within your app (im assuming like Excel or Word)
You can do something similar to what Idle_Mind said, but enclose both in a Form instead of the parent.
Form fParent = new Form();
fParent.Dock = DockMode.Fill;//i think this is the syntax. Use this if you want the form to fill to the screen
Form fChild = new Form();
fChild.TopLevel = false;
fParent.Controls.Add(fChild);
fChild.Show();
Here, it should minimize to the lower left part of the parent form. You can then size the parent to whatever you want it to be.

c# second form closing after initialisation

I am trying to create a simple c# application (my first attempt at c# so please be kind). I've created a form with a textbox to capture an "auth code", which is then validated and then a webclient fetches an xml file passing this auth code in to the request. The data sent back is parsed e.c.t.
What i want to do is once the xml comes back and ive done my checks to valid it is all fine. I want to close the first form and load up a second form where i will programmatically add the form components needed to display the xml data in a pretty format.
My problem is that im unable to get the second form to stay open (im no doubt invoking the second form in the wrong manner). Here's what i have:
// close current form
this.Close();
//open new form
xmlViewForm xmlView = new xmlViewForm();
xmlView.Show();
I'm sure you've spotted the mistake im making by now. but just to state the obvious for the sake of completeness, it closes the first form, opens the second, and then immediately the program exits (the second form flashes up for a second obviously).
I've tried a few things but none of them work (including using Application.Run(new xmlViewForm()); instead of instantiating the class and using the show() method. Obviously you know that doesn't work, and now i do too, although i dont understand c# even remotely enough to work out why.
Thanks for any help :)
The first thing that came to mind is that you are closing the form that you opened by calling Application.Run(new MyForm()) or something similar. This form has special significance; it is the "main form" of the application, and when closed, it signals to the application that the user wants to close the entire program, no matter how many other windows are open.
There are two possible fixes. First, and easiest, is simply to Hide() the form you don't want visible instead of calling Close() on it. Though invisible, it's still running, so the application doesn't close.
The second solution is to define a custom "application context" that should be run instead of the "default context" that is created by specifying a main form to watch. You do this by deriving a custom class from System.Windows.Forms.ApplicationContext. With this context specified, you can use it to control termination of the application based on something other than closure of the main form. Example code that launches two "main forms" and keeps track of whether both are still active can be found at the MSDN page for the class. You can do something similar by specifying Load and Close handlers for the main form, then passing them to the child form when the main form instantiates it, thus keeping a count of "open" forms, and closing out the full application when that number is zero. Just make sure the child form loads before closing the main form by calling childForm.Show() before this.Close().
You can not open the second form after closing the main form.
Do this:
//open new form
xmlViewForm xmlView = new xmlViewForm();
xmlView.Show();
// hide current form
this.Hide();
Main form can not be closed because it's the parent form. The child form will never show up if you close the main form.
Or change the xmlViewForm to main form by editing Program.cs file
Application.Run(new XmlViewForm());
Then you can easily call the other form first at the time of loading and close it as you please:
private void XmlViewForm_Load(o, s)
{
// hide current form, and this will remain hidden until the other form is done with it's work
this.Hide();
//open the other form
TheOtherForm _theOtherForm = new TheOtherForm();
_theOtherForm.Show();
}
private void TheOtherForm_Closed(o, s)
{
// show current form
this.Show;
}

A modal form with a parent form as background

How can I have a black form as a background and some modal forms opened one at a time whose owner is the black form? I need these two to retain their order together (when minimized and maximized) that's why I have chosen the modal form.
I have made a simple main form with black background, and opened a form in dialog (modal) form. The main form provides a black background for me and the modal form stays in front of the black background. But when opening new forms, I can't set the owner of new modal form to the aforementioned black form. I have tried passing the black form object and also registering events to no avail.
Do you know any mechanism to implement the following scenario:
A black form as a background and a series of modal forms opened one at a time in front of the black one in a way that the black form is the owner of every modal form.
Thanks
Edit
Please consider this scenario: I have 3 forms named frmBlack, Form1 and Form2. I use frmBlack as the main blacked form. After placing a button on this form I call the Form1Object.ShowDialog(this). Now suppose that I want to navigate to the third form (Form2), [this means that I must close the Form1Object] I put a button on the second form (Form1) and when this button is pressed I must close the Form1 object and navigate to the Form2Object while its owner in the ShowDialog() function must be set to frmBlack.
This is done using MDI Forms.
Your application will look like this: http://www.datadynamics.com/Help/AB3/Images/MDI%20Child%20menu.gif
This works in winform projects, not in WPF project (at least not by default).
Parent or Owner? That's a difference. Parent is only used in MDI-Applications (see Luigi's post). The owner can be set in the call to ShowDialog( owningForm ).
What is it that you want to achieve?
hth
Mario
To achieve what you want from the window with the second button post pack to the frmBlack and let it do the work.
Or more specifically set a flag within the frmBlack, since in frm1.btnShowNextForm you need to close frm1...
And also take a look at Form.Owner
so something along these lines in frm1.buttonShowNextFormClicked()
{
if ( null != Owner )
{
FrmBlack frmBlackLocal = Owner as FrmBlack;
if ( null != frmBlackLocal )
{
frmBlackLocal.NextAction = FrmBLack.NextActions.ShowForm2; //an enum
}
}
Close();
}
and in frmBlack
{
frm1.ShowDialog(this);
if ( NextAction == NextActions.ShowForm2)
{
frm2.ShowDialog(this);
}
}
Well, of course it needs some brush-up (like extracting the next handler in a function of it's own, but you should get the idea.
hth
Mario

How can I transfer information to another form without using static forms?

I have a Windows form that's generated using code (including buttons and what not). On it, amongst other things, there is a text box and a button. Clicking the button opens a new Windows form which resembles an Outlook contact list. It's basically a data grid view with a few options for filtering. The idea is that the user selects a row in this home-made contact book and hits a button. After hitting that button, the (second) form should close and the email address the user selects should be displayed in the text box on the first form.
I cannot use static forms for this purpose, so is there any way to let the first form know the user has selected something on the second firm? Can you do this with events, or is there another way? Mind that I hardly know anything about delegates and forms yet.
Please advise.
Edit 1 = SOLVED
I can return the email address from the second form to the first form now, but that brings me to another question. I am generating controls and in that process I'm also generating the MouseClick eventhandler, in which the previous procedure for selecting a contact is put.
But how do I, after returning the email address in the MouseClick eventhandler, insert that information into a generated text box? Code to illustrate:
btn.MouseClick += new MouseEventHandler(btn_MouseClick);
That line is put somewhere in the GenerateControls() method.
void btnContacts_MouseClick(object sender, MouseEventArgs e)
{
using (frmContactList f = new frmContactList())
{
if (f.ShowDialog(fPrompt) == DialogResult.Cancel)
{
var address = f.ContactItem;
MessageBox.Show(address.Email1Address.ToString());
}
}
}
That appears separately in the class. So how do I put the email address into a text box I previously generated?
Forms in .Net are normal classes that inherit from a Form class.
You should add a property to the second (popup) form that gets the selected email address.
You should show the popup by calling ShowDialog.
This is a blocking call that will show the second form as a modal dialog.
The call only finishes after the second form closes, so the next line of code will run after the user closes the popup.
You can then check the property in the second form to find out what the user selected.
For example: (In the first form)
using(ContactSelector popup = new ContactSelector(...)) {
if (popup.ShowDialog(this) == DialogResult.Cancel)
return;
var selectedAddress = popup.SelectedAddress;
//Do something
}
In response to my first edit, this is how I solved it. If anyone knows how to make it more elegant, please let me know.
void btnContacts_MouseClick(object sender, MouseEventArgs e)
{
using (frmContactList f = new frmContactList())
{
if (f.ShowDialog(fPrompt) == DialogResult.Cancel)
{
var contact = f.ContactItem;
TextBox tbx = ((Button)sender).Parent.Controls[0] as TextBox;
tbx.Text = contact.Email1Address;
}
}
}
You should keep a reference to your generated TextBox in a variable (private field in your class) and use this instead of looking it up in the Controls array. This way your code would still work even if you some time in the future change the location it has in the array, and you would get a compiler message if you removed that field, but forgot to remove the code that used it.
If the second form is modal, I would recommend that rather than having the first form create an instance of the second form and use ShowModal on it, you should have a Shared/static function in the second form's class which will create an instance of the second form, ShowModal it, copy the appropriate data somewhere, dispose the form, and finally return the appropriate data. If you use that approach, make the second form's constructor Protected, since the form should only be created using the shared function.

Categories