Dynamically show forms - c#

Please I need help with this issue. I have a dynamic menu with buttons. Each button has name of form(VP,VZPAL,PAL,...).and when I click on some button, I want to activate form with the same name that the button has. I try do it with this code, but its escape with exception:
Value cant be null.
string Formname = "FISpanel.Form" + b.Name.ToUpper().ToString();
Form frm = (Form)Activator.CreateInstance(Type.GetType(Formname));
frm.Show();
Value of Formname is right(FISpanel.FormVP).Any ideas?

This is how I've achieved the same thing in the past. In my application I have 'links' that are set inside of .ini files, but these can be different for each user, so (like you), I needed a solution that would launch forms depending on what was clicked:
LinkLabel lnk = (LinkLabel)sender;
Type type = Type.GetType("Valhalla." + lnk.Tag.ToString());
Form thisFrm = (Form)Activator.CreateInstance(type);
// DISPLAY THE FORM //
thisFrm.ShowDialog();
Also, as #King King has mentioned. The dot is very important. Are your forms actually named: "FormVP" Or just "VP" (etc...) If the later then you will have to call them as: Form.VP and not FormVP.

Related

Visual Studio C# multiple Button array

So in my Windows forms application (C#) i have a grid of button elements (btn0, btn1, ... , btn200) . I've been looking around for some time now but I couldn't find the answer I was looking for.
The question is how can I change the property of all the button at the same time.
At first I tried formatting a string like this: "btn" + id, (the id being the button number) so that I would have every button name in a string. But then I had a problem changing from string to button (type). Is there a way to do that ?
The other thing I tried to do was to create an array of buttons, but haven't had success with that either.
Is one of those two ways possible and how?
Assume that the buttons are all in a control named parent:
foreach(Button btn in parent.Controls.OfType<Button>())
{
// do something with btn
// eg btn.Text = "New Text";
}
If the buttons are directly in the form, use this instead of parent. If the buttons are in some kind of a panel, use panelName instead of parent.
The above works because as you stated, all the buttons share the same parent. However, if in some other scenario you have buttons in different parents (say panels), you would then need to do some recursion or specify multiple specific parents to loop on).
You can find it by
Controls.Find("btn1", true);
Where btn1 is the name of the button (the string you generated)
FIrst of all why don't you use an array or List of buttons in place of button1,button2.
Button[] buttons=new Button[size];
....................
button[i].Property="value";
If you don't want to change then other option would be to use a linq query on Form.Controls like this,
var buttons = from button in Controls.OfType<Button>() where
button.Name.StartsWith("btn") select button ;
This will give you list of all buttons that matches your pattern.
or use "button.Name=="btn" + id" to get the exact button you are looking for.
var buttons = from button in Controls.OfType<Button>() where
button.Name=="btn"+id select button ;

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

Can not open any folderBrowserDialogs in my second form

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.

how to display textBox control in MessageBox?

Any idea how to display textBox control in MessageBox.
I'm working on winforms projcet c#.
Thank you in advance.
You can't. MessageBox is a special container designed to only show a message and buttons. Instead, you can create your own Form with whatever controls you want, and use .ShowDialog() on it.
You can simply add an Input box from VB.NET into your C# project.
First add Microsoft.VisualBasic to your project References, then use the following code:
string UserAnswer = Microsoft.VisualBasic.Interaction.InputBox("Your Message ", "Title", "Default Response");
And that should work properly.
It will be better to add a new Form in you application which you can customize the way you want.
and just call it from where ever required.
you could create a classic win form that looks like a message box and opening it as a modal form
perhaps using Form.ShowDialog
more info at
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
You cannot customise the MessageBox, its better you use a popup designed using Windows Form separately and use its instance to invoke.
customPopup popup = new customPopup();
popup.ShowDialog();
Place your controls on the popup form and set their access modifiers to public if you want to access the textboxes or labels etc in your previous form.
customPopup popup = new customPopup();
popup.msgLabel.Text= "Your message";
popup.ShowDialog();
As I know there is no way to do that.
You can create a winform change it's style to look like a MessageBox and add your own controls.
Yes, as krillgar mentioned,you should create your own form. And
1. Encapsulate the form in a static class or function, so you may just call MyMessageBox.Show().
2. The textbox should have readonly=true, so the end users won't be able to change the text displayed, while they could select text and copy to clipboard.
Regarding to item 2, I believe many Windows build applications and MS Office use such approach.
using Microsoft.VisualBasic;//add reference
var Password = Interaction.InputBox("Message", "Title" ,"information in textbox", -1,-1);
In the variable "password" it receives the information that is entered from the text box.
Remember to add the reference "Microsoft.VisualBasic" in the solution explorer
Solution in here, you can create windows form and design it, set form is dialog, when you call form, it is auto show. In form you design, you set value some parameter static where other class in project, but you should set when you close form design that, OK, come back form init call show dialog, you create interval call when have == null return call, when != null you stop call back and using parameter in class static it !

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