Can not open any folderBrowserDialogs in my second form - c#

I am using visual studio 2010 in C#. Basically, I have my first form with my main code, then I have a second form set up where the user is prompted to enter multiple paths. However, only on form1 do any folderBrowserDialogs open. On form2, regardless of what I try so far, the button simply clicks and nothing changes. I have not altered anything but variables really from the one I use on form1 and it works just fine.
Here's a bulk of my code, it contains one of the folderBrowserDialog's that will not work. This is all form form2:
string mexPath;
string ausPath;
string canPath;
string chilPath;
public CultureInfo targetCulture1 = new CultureInfo("es-CL"); //Chili
public CultureInfo targetCulture2 = new CultureInfo("de-AT"); //Austria
public CultureInfo targetCulture3 = new CultureInfo("es-MX"); //Mexico
public CultureInfo targetCulture4 = new CultureInfo("fr-CA"); //Canada
PassoloU.PassoloApp app = new PassoloU.PassoloApp();
//Respective Language Paths
private void spanPath_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
mexPath = folderBrowserDialog1.SelectedPath;
}
}
I have established 4 different folderBrowserDialog's in the design and I am not sure where to go from here.
EDIT: Thank you for the help. For some reason, adding "_1" to the end of each click events name allowed the dialog boxes to open. So now each reads "spanPath_Click_1", "germPath_Click_1", etc. I have no idea why this is an issue though, but it seemed to have solved my problem.

Open your Form2 in Designer mode, select the button, go to properties and ensure the click event is attached to the button as MichaC mentioned.
I just tried to put together an example with two forms, Form1 with a label, FolderBrowserDialog, and a button. Form1 displays the browser dialog, and then the selected folder path is displayed in the label. If you click a button it displays Form2 with similar controls. Both the FolderBrowserDialogs worked just fine. Let me know if you need me to post it.

Related

refresh label text in another form after button click C#

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;
}
}

Updating Form1's widgets by clicking Form2's button in Visual C# Windows Forms

I'm fairly new to Visual C# and I'm writing an GUI app with multiple forms. One form is main window, and the rest are some kind of option windows. When showing an option window, I need to load some data to it (for example a string to window's editbox), then edit it and return back to main window when closing option window. Is there any simple way I can achieve it?
I've found some solutions like, or c# event handling between two forms, but I can't really conform it to my needs. I was thinking about passing data in constructor, but how to get it back? I've found something about ShowDialog, but as I said I'm new to C# (started yesterday ^^) and don't know if I can use it.
Any ideas, please?
I found the following previous answer which outlines sending specific properties from the one form to another:
Send values from one form to another form
The using keyword will also ensure that the form is cleaned-up properly, here's a link to it's usage (pardon the pun...) : http://msdn.microsoft.com/en-us/library/vstudio/yh598w02.aspx
I've run into the same issue to be honest, and I have to say that prior to this discussion I would just pass the parent form itself to the child and alter it in that way. Such as:
ChildForm child = new ChildForm(this); //from the parent
and
public ChildForm(ParentForm parent)
{
this.parent = parent;
}
Probably not the best convention though, as you probably don't need to access that much from the parent as the child.
Thanks guys, I think I finally get it. Idle_Mind, your idea was the easiest in my point of view, so I decided to use it. If someone else has a problem like this, here's what I've coded:
In main window form: when button is clicked, a new form appears; after closing it, label1 shows the text typed in that form
private void Button1_Click(object sender, EventArgs e)
{
LoadDataForm loaddata = new LoadDataForm("initial value");
if (loaddata.ShowDialog() == DialogResult.OK)
{
label1.Text = loaddata.textBox1.Text;
}
}
In load data form: argument passed in form's constructor appears in textBox1; textBox1's Modifiers property has to be modified to "public"
public LoadDataForm(string initvalue)
{
InitializeComponent();
textBox1.Text = initvalue;
}
private void ApplyButton_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
Regards,
mopsiok

Creating Multiple Form GUI

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/

Creating a modal window with a Date Picker in C#

I have a C# application in which a function is performed which requires a date. The original specification said that the current date could always be used, but now I have to allow for the user to be able to change the date on demand.
The problem is, this operation executes completely in the background. There is only a menu item which the user clicks to start the process. So I wanted to add, when the user clicks the menu item, a modal window which asks the user for the date to be used, and returns the date entered. I have not been able to find a way to do this.
Do I need to create a form which has only one item on it - a DatePicker - and design it to look like a modal window? Or is there an existing modal window class which does exactly this?
I don't think there is a modal window built into the framework.
You will have to create a new windows form. Put a datepicker on it. Make its modifier public and do something like this:
private void MenuItemClick(objec sender, EventArgs e)
{
var userEnteredDate = DateTime.MinValue;
FormWithDate fmNewFormWithDateOnIt = new FormWithDate();
fmNewFormWithDateOnIt.ShowDialog();
userEnteredDate = fmNewFormWithDateOnIt.dtTimePickerOnForm.Value;
fmNewFormWithDateOnIt.Dispose()
//Do your thing
}
UPDATE: You could put a button on the form and set its DialogResult = OK and then do the following:
if (fmNewFormWithDateOnIt.ShowDialog() == DialogResult.OK) //Not clicked the red close button on the form
{
userEnteredDate = fmNewFormWithDateOnIt.dtTimePickerOnForm.Value;
}

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