I have a unique c# source file named source.cs that i compile using CSharpCodeProvider from a builder to get an executable.
I would put an option on the builder whether to display the About form on application startup or not.
How can i create a form with title as About Us then add controls within (Labels, RichTextEdit etc..)
Something like
if (display_about_dialog) {
// code to display the form }
Any help would be highly appreciated
Try something like this:
using (Form form = new Form())
{
form.Text = "About Us";
// form.Controls.Add(...);
form.ShowDialog();
}
Here's the documentation page for the System.Windows.Forms.Form class.
if you have a class MyForm : System.Windows.Forms.Form (that you create using windows form builder)
You can do
MyForm form = new MyForm();
form.Show();
To launch an instance of MyForm.
Though if you want to create a simple confirmation or message dialog, check out the many uses of MessageBox
MessageBox.Show("text");
MessageBox.Show("text", "title", MessageBoxButtons.OKCancel);
Form aForm = new Form();
aForm.Text = #"About Us";
aForm.Controls.Add(new Label() {Text = "Version 5.0"});
aForm.ShowDialog(); // Or just use Show(); if you don't want it to be modal.
Form is a class which you can instantiate like any other, set it's properties, call it's methods.
Related
I 'am making a WindowsForms application. I have 2 Forms :
On the first form (Form1), There are many fields (Textboxs) that should be filled by the user, then click to a button (Transfer).
This button should show all the input datas of Form1 in a new Form (Form2).
I'm not sure how to begin to move data from one form to another. Can somebody guide me how to do it?
If Form1 creates Form2 specifically to show the data, then you can use a non-default constructor to pass the information as you create the form.
First, let's consider an example of the information you want to transfer, and name it Form2Info:
// This class is an example of the information you want to transfer
public class Form2Info
{
public string text1;
public int number1;
}
Then, you modify Form2's constructor to take the information:
public partial class Form2 : Form
{
private Form2Info info;
public Form2(Form2Info information)
{
InitializeComponent();
info = information;
// Do something with this information, such as populate a TextBox or Label on the form.
}
}
Finally, you want to create a Form2 instance from your Form1:
// Create the information you want to pass; we fill it with some placeholder data here.
Form2Info info = new Form2Info();
info.text1 = "Hello"
info.number1 = 5;
// Now create the form and pass the data
Form2 form2 = new Form2(info);
form2.ShowDialog(); // Show modal dialog.
Each Textbox has a value (Textbox.Text property). You will have to transfer the content of these different properties to the new controls in your second form.
The easiest way will be through a custom Form constructor for the second form.
public Form2(string textBox1Value, string textBox2Value)
// etc... add as many as you like or use an object that holds all values in properties
{
InitializeComponent(); // Required by WinForms
this.TextBox1.Text = textBox1Value;
this.TextBox2.Text = textBox2Value;
}
Make sure you match the text boxes you want using the names. Finally when creating the form on the Transfer button code, call this constructor instead.
I am using Metro ui for windows application
public partial class Distributor_Closing : MetroFramework.Forms.MetroForm
{
private object BtnClick(Button button, int index)
{
MetroFramework.Forms.MetroForm childForm = new Distributer_Closing_Info(sub_cat[index], Str, id, Convert.ToInt32(Mtddlstocks.SelectedValue));
childForm.ShowDialog(this);
}
}
I am getting above error when i close Distributer_Closing_Info child form;
public partial class Distributer_Closing_Info : MetroFramework.Forms.MetroForm
{
public Distributer_Closing_Info(int sub_cat,String dte,int stk_mas,int stkid)
{
InitializeComponent();
sub_catid = sub_cat;
StockDate = dte;
Stkmasid = Convert.ToInt32(stk_mas);
stk_id = stkid;
LoadGrid();
}
}
I think it is an issue with the using MetroForm from the MetroFramework;
Your program would work fine if you were using System.Windows.Forms.Form
The workaround as mentioned
childForm.ShowDialog(null);
or this one
childForm.ShowDialog();
In my case the following line triggered this exception (WinForms):
this.splitContainer1.Panel1.Controls.Add(this.splitContainer1);
(The SplitContainer added itself in one of it's own panels.)
In my case, I no longer could open a Form in the WinForms designer within Visual Studio. I received the "A circular control reference has been made" error.
It turns out that I had set the .Name property of the Form to the same name as one of the Controls on the Form (I might have done this by editing the .Designer.cs code indirectly using a ReSharper rename operation...) in the .Designer.cs class module using the same name (but different casing) as a control already declared as private.
For example, the designer code had this snippet for setting Form properties:
//
// MyForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(850, 613);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MinimizeBox = false;
this.Name = "MyCircularName";
While further down in the .Designer.cs, control names are declared:
private MyUserControl myCircularName;
Notice that "MyCircularName" is now used by both Form and MyUserControl instances (the circular-check is not case sensitive). When I try to view the Form in the designer, the designer detects that my Form Name property is the same as a declared-control's Name property.
The solution was to either rename the Form (change "MyCircularName" to, say, "MyCircularNameForm") or to change the declared control name from "myCircularName" to, say, "myCircularNameControl".
I'm new to Visual Studio 2010 and I'm planning to create a Timekeeping system. I'm just want to ask how could I create a form that compose 2 forms in it. For example, if I will click a button it will open a new form inside a form. Please help. Thanks
Form formA = new Form();
formA.IsMdiContainer = true;
Form formB = new Form();
formB.MdiParent = formA;
formB.Show();
You have to work with MDI (Multiple Document Interface), have alook at this article that might help.
You could create custom form, remove all borders, and toolbars to make it look as closely to a panel as possible. Then make that new custom form a MdiContainer / MDI-panel and show forms in that panel, something like the code below will do the job
Mdi-Panel definiton:
public class MdiClientPanel : Panel
{
private Form mdiForm;
private MdiClient ctlClient = new MdiClient();
public MdiClientPanel()
{
base.Controls.Add(this.ctlClient);
}
public Form MdiForm
{
get
{
if (this.mdiForm == null)
{
this.mdiForm = new Form();
/// set the hidden ctlClient field which is used to determine if the form is an MDI form
System.Reflection.FieldInfo field = typeof(Form).GetField("ctlClient", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
field.SetValue(this.mdiForm, this.ctlClient);
}
return this.mdiForm;
}
}
}
Usage:
/// mdiChildForm is the form that should be showed in the panel
/// mdiClientPanel is an instance of the MdiClientPanel
myMdiChildForm.MdiParent = mdiClientPanel1.MdiForm;
I think, this is a very easy way:
Form1 form= new Form1 ();
form.TopLevel = false;
this.Controls.Add(form);
form.Show();
Maybe MDI interface will do what you want..
Here's a tutorial to do that.
I am making an application in C#, and it has a menu, having forms linked with it, i want
that there should be a parent form, having a panel or window, when we click on any menu link, its .cs form should be loaded in the window, and so we can click on other windows, and their forms should replace the current one. Just like a common windows software.
Regards
Touseef Khan
Your Form(Window) must be MDI
YourForm.IsMdiContainer = True
NewForm.MdiParent = YourForm;
NewForm.Show();
Try this:
var f2 = new Form2();
f2.TopLevel = false;
f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f2.Size = this.Size;
f2.BringToFront();
f2.Visible = true;
this.Controls.Add(f2);
I am trying to write a program that opens an arbitrary number of forms ( each one containing a label) when the user clicks on a button, and i put them on a list:
List<Form> formlist = new List<Form>();
...
public void showFrame()
{
Form f = new Form();
// I add the components i need ...
formlist.Add(f)
}
What i need now is, given the i index of the form in formlist, to change the label.Text of that form.
Is possible to do it, withous using a different name for each lavel?
Give the control(s) you add to the form a Name. Then it is
formlist[i].Controls["somename"].Text = "mumble";