i have 2 project in 1 solution and each project have 1 form. In form 1 i have label and button, and i want when i click on button it will show in form 2 labeltext in form 1.
my code in form 1:
WindowsFormsApplication1.Form1 layarForm1 = new WindowsFormsApplication1.Form1();
layarForm1.Show();
layarForm1.LabelText = no_antrian.Text;
//layarForm1.LabelText = this.Refresh();
form2(in other project but in same solutions, and i have reference to form 1):
public string LabelText
{
get
{
return this.ruang_1.Text;
}
set
{
this.ruang_1.Text = value;
}
}
my code work for first time but when i click button again it will show new form. is there any ways that label text in form 2 will refresh after button click. And i dont want to use dispose form because in form 2 i have video played that will start again if i use dispose() and show()
Two projects = 2 executable you need to use shared memory or named pipes to send the data across from one exe to another, or use winapi such as SendMessage once you get the window handle of the other window and the find the child window handle via EnumChildWindows.
Why not use the two forms in the same project?
The problem is every time the button is clicked, an instance of the form is created. You need to check whether the form is already open and if not create an instance, show the form and update the label.
If the form is already open, all you need to do is to update the label and set focus on the form.
You can use Application.OpenForms to iterate through all one forms and check whether the form is in that collection.
Something like
foreach (Form frm in WindowsFormsApplication1.OpenForms)
{
if (frm.Name == "MY_FORM_NAME") then
frm.LabelText = no_antrian.Text;
else
{
WindowsFormsApplication1.Form1 layarForm1 = new
WindowsFormsApplication1.Form1();
layarForm1.Show();
layarForm1.LabelText = no_antrian.Text;
}
}
Related
I have two forms in my project (a simple game for kids). The first one is the start menu and the second one is the game. Now, when I click on New Game, I want the second form to open within the first one. I did that by using the following code:
private Form activeForm = null;
private void openChildForm(Form childForm1)
{
if (activeForm!=null)
{
activeForm.Close();
}
activeForm = childForm1;
childForm1.TopLevel = false;
childForm1.FormBorderStyle = FormBorderStyle.None;
childForm1.Dock = DockStyle.Fill;
panel1.Controls.Add(childForm1);
panel1.Tag = childForm1;
childForm1.BringToFront();
childForm1.Show();
}
Now the second form opens within the first one, but it doesn't work properly. In my second form I have a picturebox, which is supposed to move when the user presses on one of the arrow keys. But it won't move.
Any suggestions what should I do?
P.S.
I'm a beginner and this is a school project. My teacher showed us only one way of opening a form:
Form2 objForm2 = new Form2();
objForm2.Show();
but since it is a very ugly method of getting the job done, I wanted to do it better.
I'm using Visual Studio 2019
I would suggest you to use a so called UserControl it's basically that what the name says: It's a custom windows forms control, witch has it's own child controls -> Just Like a form.
I am developing a very basic application Windows Form Application in c# that inserts values into a SQL database.
I have four separate forms
one for inputting customer details
one for inputting any transaction details
one for searching customers/transactions respectively.
What is the best way link all four forms together? I'm only just getting into C# so the most basic way possible would be ideal.
The way I see it working in my head is that you run the program and end up on one screen which shows four buttons for the four corresponding forms. When you press the button a separate window opens showing the insert forms. You can then close the form to return to the Main Start Screen
What would be the basic code for this in C#? For examples sake lets say the 5 different layouts are
Main (Containing the buttons)
TransactionEntry
AddressEntry
TransactionSearch
AddressSearch
Okay, here is an example of how I would do it. On the main form on button click event:
frmSecondForm secondForm = new frmSecondForm(this); //"this" is passing the main form to the second form to be able to control the main form...
secondForm.Show();
this.Hide();
One your Second Forms constructor code:
Form frmHome;
frmSecondForm(Form callingForm) //requires a calling form to control from this form
{
Initialize(); //Already here
frmHome = callingForm as frmMain;
}
Then on the second form's closing unhide the main form:
frmSecondForm_FormClosing()
{
frmHome.Show();
}
So all in all, if you needed to pass data between the forms just add it as a parameter on the second form.
Again, I would consider placing your data collection into a repository package (folder) and class, then create a User.cs class that will hold all of the information you keep in the database.
Hint: Right click your top level item in solution explorer and go to New -> Folder. Name it Repo.
Right click that folder and go to New -> Class and name it UserRepo.
In here build functions to collect the data from the databases.
On your main form's constructor area, call the class (repo).
private Repo.UserRepo userRepo;
frmMain_FormLoad()
{
userRepo = new Repo.UserRepo();
}
Then on log in button click:
private button1_ClickEvent()
{
if(userRepo.isValidLogin(userNameText, passwordText))
{
//Do stuff
}
}
for userRepo.isValidLogin()
public bool isValidLogin(String username, String password)
{
bool isValid = false;
//Write up data code
return isValid;
}
From the Main form use eg:
TransactionEntry trans = new TransactionEntry();
trans.ShowDialog();
.ShowDialog() will show the new form, but will halt any code executing on the Main form until you close it
(This assumes your forms are all in the same solution)
You could try the MDI (Multiple Document Interface) way, here is a nice tutorial: http://www.dreamincode.net/forums/topic/57601-using-mdi-in-c%23/
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
I once read a tutorial about how to create a new form and make it above all other windows so you can click only it - like in internet explorer for example when you click on browse for a file you can't click the main window until you finish using the browse window.
Also what is the best way to get values from a form back, for exmpale on my second form I have a listbox and when a user clicks on one of the values the first form (mainform) should get the event - is this possible?
It sounds like you want:
using (MyCustomForm form = new MyCustomForm(...))
{
if (form.ShowDialog() == DialogResult.OK) {
// Now use the values in form
// (e.g. through properties of the form)
}
}
Just use a modal form, which is done by calling .ShowDialog() after you've made an instance of it.
To get the values back, just store them in properties of the form, and then read those properties from the parent window/code before it goes out of scope. You'd handle the SelectionChanged event in the code-behind of your new form and set a property with the value.
What you are looking for is to show the form as a modal dialog box. Form.ShowDialog() Here you can read more about this topic.
You can access parent form (back form) in couple ways:
Make modal form constructor to accept parent from as parameter
Put parent form reference into some global variable that can be accessed from modal form
...
To get an event from child form you can do something like this:
form.myListBox.SelectedIndexChanged += new System.EventHandler(this.myListBox_SelectedIndexChanged);
form.ShowDialog();
You need to make myListBox control public in order to access it from parent (caller) form.
try this:
Create your form and
In your calling code do the following:
MyForm form = new MyForm();
form.ShowDialog();
To get the values back, simply create properties on your form that map to the values of your controls (make sure you don't dispose your form before you access the properties!):
public class MyForm
{
//...
public string FirstName
{
get
{
return firstNameTextBox.Text;
}
}
}
Then call the properties from your calling code after the dialog is done:
MyForm form = new MyForm();
if(form.ShowDialog() == DialogResult.OK)
{
string myFirstName = form.FirstName;
// etc
}
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.