Move one form to another winforms - C# - winforms

I have 2 winforms Form 1 and Form 2. I have button1 in form1, when i click on button1 from form1 i display form2.
Form2 ins = new Form2();
ins.MdiParent = this.MdiParent;
this.Hide();
ins.ShowDialog();
I hide the form1 to display form2 when button1 is clicked. This creates a flicking effect and i need to remove this flicking. How do i open/redirect to another form (i am supposed to show only one form at a time and am not supposed to show any top menu like (if i use MDIParent form). Just one active form.
Thanks,
Karthick

It sounds a bit like you're trying to create a web-style UI where the user steps from one "page" (represented by a Form) to another.
Rather than implementing a UI like this with separate forms, you're better off doing it with UserControls hosted on a single parent form.
Have a read of this MSDN article, which includes a download with sample code. It's a great walkthrough for designing that kind of user interface:
IUIs and Web-Style Navigation in Windows Forms, Part 1
IUIs and Web-Style Navigation in Windows Forms, Part 2
Edit
If you're intent on showing two separate forms, is there any reason you need to show the second one modally? Can you not simply show it and then hide the original?
form2.Show();
form1.Hide();
... or do you have yet another form that both form1 and form2 are "modal" to?

To transfer from one page (form1) to another (form2)
suppose form1 contain a button named "SAVE"
we have to write the following code in click event of the "SAVE" button
form2 f2=new form2();
f2.Show();

I think there is a property on winforms if you would like to show it on the task bar or not.

I can clarify your doubt about how to redirect from one form1 to form2
for example:
place a link in form1 and then write following code in it
form2 ins=new form2();
ins.show();

Instead of hide use close option.
Form1 formObject = new Form1();
formObject.Close();
or simply
this.Close();

Related

Load an ExtJS form

I have a Simple Extjs form1 with several textfields and a button.
When i click on the button on form1,a form2 is Popped open.
The problem i am facing is, i cannot get the values from the form2 into form1, unless form2 is instantiated.
Is there anyway i can load/instantiate the form2 before form1 so that i can get the values from the form2,using Ext.getcmp('id') into form1?
Being a novice in Extjs, i hope my question is Not vague.
You can pass the inputs from form 1 into form 2 as a config property. Then this data will be available during the initComponent method call in form 2, when the form is being created. During initComponent, simply make the fields visible/hidden based on the data that was passed in.

How to open a form from a Dialog (modal form) and make it accesible even if didn't close the dialog

I have a form (form1) that I open as modal. In that form there is a button where, when the user presses it, it opens a new form (form2).
I have the problem that form1 is modal and form2 is in the background, and I cannot do anything until I close form1. How can I do stuff in form2 without closing form1?
I need form1 to be modal, I cannot change that. I tried to set the parent of form2 when calling it:
form2.Parent = form1
form2.Show()
But it gave me an error:
"Form cannot be added to the Controls collection that has a valid MDI
parent. Parameter name: value"
So I tried this:
form1.IsMdiContainer = true
But this just puts form2 inside form1. I also tried hiding form1 and then showing it again when form2 is closed, but it fails.
Platform: Windows Forms using VB.NET.
The problem I was having was that it was setting the parent of form2 as the mainform, so I deleted that part and now form2 opens on top of form1.
Another solution that also works is to open form2 as modal. Of course it will act different, but that depends on what you want.

C#: components cannot show on Windows Form

I have created an empty project. after that, I added new items is Windows Form and add some component such as button or textbox in Design View.
In main file, I use this code:
SimpleForm form = new SimpleForm(); // SimpleForm is my class
form.Show();
But, when I run, C# generate a form but I cannot see textbox nor button. I just see blank area where I put those components. As picture below, the white area is my button, but I don't know why this button cannot show. I think maybe some problem, and those components cannot draw on the form.
Please help me this error.
Thanks :)
If by "the main file" you mean your main method (the entry point) - try this code instead:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new SimpleForm());
If not you really need to post more of your code to get a reasonable answer.

A way to refresh a form page from another form page?

I have a Win form application (VS 2010 / C#) and I'm trying to figure out how to refresh pages without a refresh button. Currently I can refresh a page (basically to reset the data bindings) with a refresh button containing code something like this (this.refresh() does not seem to work for some reason):
this.Hide();
AccountSettings AS = new AccountSettings();
AS.ShowDialog();
An example I have is a page with numerous settings including data grids with CellClick events. When I click a cell I can make changes to a database. I hit close to go back to the Settings page but the only way for me to see the changes are to refresh() the page via the button.
So the short of it is, is there any way to refresh a form page from another form page?
For instance, when I click the Save button or close the child window.
Maybe pass the original form as an argument to the second form:
Form2 frm2 = new Form2(this);
And in Form2:
Form1 frm1;
public Form2(Form1 frm1)
{
InitializeComponent();
this.frm1 = frm1;
}
And then have in Form2:
frm1.Update();
Refresh on winform controls repaints the control itself. I find it useful to create a method that just loads my controls with the proper data, and then call it as necessary. (Including Form load)
private void ResetData()
{
//code to update settings
}
If you are showing the form that is closing as a dialog also you can take advantage of that, and check the status of the dialog instead of just opening it.
Form2 dlg = new Form2();
if (dlg.ShowDialog == System.Windows.Forms.DialogResult.OK) {
//code that updates your data
ResetData();
}
If its not a dialog there are a few things you could do and how your application works would make one method better than others. Here is just one example.
If your changes are something you don't need access to data from the other window to update you can handle the closed event of the form you create.
Create a class level variable to hold the form that is opened, so that you can also remove the event handlers you create:
private Form2 frm;
To create an instance of the form, and add the close event handler:
frm = new Form2();
frm.FormClosed += OnForm2Closed;
The event handler method:
private void OnForm2Closed(object sender, FormClosedEventArgs e)
{
ResetData();
frm.FormClosed -= OnForm2Closed;
}

WPF trigger code behind button to display second form

I am a WPF novice.
I have created a form containing a combo box with which to choose a multi-field key value(populated from an XML data file).
I have also created a second WPF form which is available to display all field values from the record associated with the multi-field key value chosen from the first form.
I need to be able to click a button which will cause the second form to be displayed, with all fields filled in which are associated with the chosen key field values.
How do I go about writing such an event trigger using C#?
couple of steps (this is not really MVVM, BTW) ...
first, add a click handler to your button
second, in the click handler code, instantiate your new form
third, set the data context, etc for the new form
forth, show the new form by calling .Show()
in your xaml add a click handler to the button in question....
<Button Click="myClickHandler"/>
in visual studio, you can right click the text in the click="" and choose to navigate to the handler and visual studio will generate the code for it for you.
in your click handler, in code behind, do something like this....
public void myClickHandler(object sender,EventArgs)
{
MySecondForm form = new MySecondForm();
form.DataContext = theDataContextIWantToSet;
form.Show();
}

Resources