How to Link Different Form? - c#

I got great help in my first question n hopefully someone will tell me or refer me to an earlier question about this topic.
I want to link different forms like I click on a button on first one and it opens the second one.Basically I'm going to make a Menu for cellphone functions like SMS,CALL etc. so I want that If I click on call a new form opens asking for Number to call etc.

void SomeInitializationFunction() {
button.Click += new System.EventHandler(buttonClick);
}
private void buttonClick(object sender, System.EventArgs e)
{
using(GetNumberForm getNumberForm = new GetNumberForm())
{
if(DialogResult.OK == getNumberForm.ShowDialog())
{
string phoneNumber = getNumberForm.PhoneNumber;
// do something with the user input.
}
}
}

var otherForm = new Form2();
otherForm.ShowDialog(); // To show a modal dialog, or...
otherForm.Show(); // To show it as a non-modal window

Related

Show panel on click

please for a little help in displaying the panel at the click of a button, namely in my application I have a FormMenu in which the panelCentralForm is displayed when loading.
When I click on btnListOfActiveUser_Click I manage to open FormListStaff over panelCentralForm using this code:
private void btnListOfActiveUser_Click(object sender, EventArgs e)
{
FormListStaff formListStaff = new FormListStaff();
AddFormToPanel(formListStaff);
}
private void AddFormToPanel(object form)
{
if (this.panelCentralForm.Controls.Count > 0)
this.panelCentralForm.Controls.RemoveAt(0);
Form fh = form as Form;
fh.TopLevel = false;
fh.FormBorderStyle = FormBorderStyle.None;
fh.Dock = DockStyle.Fill;
this.panelCentralForm.Controls.Add(fh);
this.panelCentralForm.Tag = fh;
fh.Show();
}
my question is how can I in a similar way that now when I click on buttonDashboard to return to me the first view, ie the view from the Central Form panel (panelCentralForm).
private void buttonDashboard_Click(object sender, EventArgs e)
{
panelCentralForm.Visible = true; //something like this, p.s. this code doesn't just work as an example I wrote
}
the whole view belongs from FormMenu.cs
Thanks a lot to everyone for the help
my question is how can I in a similar way that now when I click on
buttonDashboard to return to me the first view
Just Clear() the panel, then add the label back in?
this.panelCentralForm.Controls.Clear();
this.panelCentralForm.Controls.Add(label1);
The label should still be accessible even though it was previously removed from the panel since it is declared at form level in your designer file.

Creating new pages in Visual Studio using C# Windows Forms

I am trying to write a Windows Forms application using C#, however I have never done this before. I have successfully created a login page which takes the user to a home page where 4 buttons are displayed. I am trying to find code to put inside each button that will take the user to a different page.
In windows forms application, you have to call Show method on the Form which you want to show.
Let's say on Button1 click you want to show Form2, then below code will bring Form 2 up on the screen.
private void button1_Click_1(object sender, EventArgs e)
{
Form2 obj = new Form2();
if (obj == null)
{
obj.Parent = this;
}
obj.Show();
this.Hide();
}
You need to create, initialize and show the other forms.
frmSecond frm = new frmSecond();//You should call any other constructor, may be with some parameters
frm.Text ="I wanted to change the title";//Optional: You can change any property value if you need
frm.Show();
If you want to show the form, collect some information from user and return some values then you can use ShowDialog method instead of Show method
if(frm.ShowDialog(this) == DialogResult.OK){
var myVar = frm.ReturnObject;
...
}
You can use below approach for one parent window and new child window.
When you press the button new form opens:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
{
frm2.ShowDialog();
}
}

Sending data from Form2 to Form1 once the user inputs the required things in some text boxes in C#

I am making a program for a friend and I need it to be were when she types in a number corresponding to the form that opens when she hits 'go' that form will open. I have that part done. The issue I have is when the new form opens I have a series of text boxes that she needs to input data into like '1 can of fresh beans' and some other stuff in some other text boxes on that form. Now when she is done typing in all the required things she will hit a submit button that will then format the code accordingly to the way I have it set to like this
richTextBox1.Text += "This is some text that I type before" + AmntItemsTxtBox + " and this is some other stuff";
So that is what I kind of want to happen. Now I know I may have confused some people but what I ran into as an issue is how do I take what she typed in form 2 and send it to the richtextbox in form 1 when she hits the button so she can copy and paste it into something else later on. I know my code seems a bit "new" but I am just starting out with C# and wanting to learn more. Any help is appreciated.
I have done it in my sample project. It may help you.
Form 1:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.txtmessagechanged += new EventHandler(txt_messagechanged);
frm2.ShowDialog(this);
}
private void txt_messagechanged(object sender, EventArgs e)
{
txtMessage.Text = (string)sender;
}
Form 2 :
public EventHandler txtmessagechanged { get; set; }
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (textBox1.Text != string.Empty)
{
string a = textBox1.Text;
if (txtmessagechanged != null)
txtmessagechanged(a, null);
}
else
{
MessageBox.Show("Fill some data in textbox");
e.Cancel = true;
}
Image of form 1 (on button click event it open form 2):
Input in form 2 (put some value in text box) :
Get text in form 1 on form 2 close event.
You can modify it according to your needs.
Form 1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(textBox1.Text);
frm2.Show();
}
From 2
public Form2(string msg)
{
InitializeComponent();
textBox1.Text = msg;
}
That is how you pass data (or at least one way of doing it). Then you can do whatever you want with it and format it however you like.

Closing and Opening Forms

What is the best way to close a form and get to another form.
At present I have a Main Form and then two forms:
I want to know how to close one form and open another form efficiently.The two times I did it had a slight different outcome:
The main form in my application is just the introduction which will load some Company Logo and show a progress bar which will then take it to a log-in form, where in I have a log-in button, from where the actual application will open.
I have some questions.In the Main Form I have added this code:
private void Form1_Load(object sender, EventArgs e)
{
int i;
for (i = 0; i <= 100; i++)
{
progressBar.Value = i;
label1.Text = "Please wait while Refresh loads up...";
}
}
private void progressTimer_Tick(object sender, EventArgs e)
{
this.Close();
}
private void MainForm_Deactivate(object sender, EventArgs e)
{
Form form = new FirstForm();
form.ShowDialog(this);
}
1) This works fine, just that the new form that opens up is at the task bar and is not in the center of the screen(as I have set it's property).How do I fix this?
In the First Form I have added this code:
private void loginButton_Click(object sender, EventArgs e)
{
using( ERPQueries eq = new ERPQueries())
{
int? count = eq.CheckEmployee(userTextBox.Text,passwordTextBox.Text);
if (count == 1)
{
//testLabel.Text = "Ok";
this.Close();
Form form = new SecondForm();
form.ShowDialog(this);
}
else
{
testLabel.Text = "Invalid username or password!";
}
}
}
2) Here the next Form pops up in the center of the screen.I want to know how is it different from the first case, as I have used showDialog() in both the cases?
3) Also in the first case my Main Form Disappears, whereas in the second case the First Form is still visible in the background and disappears only after closing the SecondForm.
I'm sure I'm doing a lot of mistakes, my code is flawed.Please help.This is the first time I'm making an application with multiple forms.
Edit:
When I use Show() instead of ShowDialog() I don't see the new form.Am I missing something?
I tried this.Dispose() instead of this.Close().In the first case it works fine.In the second case, it disposes all the forms.
try:
form.Show();
not
form.ShowDialog(this);
which is what makes it modal.
ShowDialog() is blocking call, so if in one Form's deactivate event you call ShowDialog() of another, your parent form will not yet finish it deactivating.
Like a suggesion I can give you create a collection/stack of forms you want to manage and give control over it to a FormManager class, where you implement whatever logic you want and can call either ShowDialog() or Show(). In other words bring out forms Show/Hide management out forms itself.
Regards.

How to disable multi clicks in the c# application programming?

In the c sharp application program, if a button is clicked then a dialog box will be opened but if the user clicked more than one time then more dialog boxes are opened. How to overcome it?
Please give me the solution regarding this issue.
If its a desktop application use Modal Dialogs.
use ShowDialog function:
using (Form2 frm = new Form2())
{
frm.ShowDialog();
}
This will disable the current form and only make the new form usable.
Alternatively, you can disable the button to prevent it from being clicked again.
button1.Enabled = false;
But make sure you enable the button when it should be accessible again.
Furquan has a good answer. Edit: as does Fun.
If you can't or don't want your dialog to be modal, you can add extra state to see if the subdialog is already open. Here's some example pseudo-code (it probably won't compile):
class MyForm : Form
{
public void OnButtonClick()
{
if(!isSubDialogOpen)
{
isSubDialogOpen = true;
ShowSubDialog();
}
}
private void OnSubDialogClose()
{
isSubDialogOpen = false;
}
private void ShowSubDialog()
{
SubDialog subDialog = new SubDialog(this);
subDialog.OnClose += OnSubDialogClose;
subDialog.Show();
}
private bool isSubDialogOpen;
}
class SubDialog : Form
{
// ...
}

Categories

Resources