Close Current form open new one of that form closed - c#

i have 2 form
in form 1 i have a button when i click on button form 2 will be show and my data are showing on form 2 so far its OK
but i want when one of form 2 its open if user click on button in form 1 then close this form 2 and open a new one! not open an other form 2
i hope u understand my question :D sorry for bad English
i tried form instance function but its not working its just hold this form 2 and will not allow to open new one!
with these code:
public static Form2 Instance
{
get
{
if (_form2 == null)
{
_form2 = new Form2();
}
return _form2;
}
}
and here is button code :
private void btnSave_Click(object sender, EventArgs e)
{
if (RadioMale.Checked == true)
{
jensiyat = "مرد";
}
else { jensiyat = "زن"; }
if (RadioMarried.Checked == true)
tahol = "متاهل";
else tahol = "مجرد";
Class1.txt +=
"________________________\n\n" + "مشخصات مربوط به خانم/آقای "
+ tbFamily.Text + "\n________________________" +
"\nنام و نام خانوادگی: " + tbName.Text + " " +
tbFamily.Text + "\n" + "ایمیل: " + tbEmail.Text + "\n" + "شماره ملی: " +
tbCodmeli.Text + "\n" + "سریال شناسنامه: " +
tbSerialShenasname.Text
+ "\nشهر محل زندگی: "+ shahr + " - " + TreeShahr.SelectedNode.Text
+ "\nآدرس: " + tbAddress.Text + "\n"
+ " تحصیلات : " + ComboTahsilat.SelectedItem
+ "\nجنسیت : " +jensiyat
+ "\nوضعیت تاهل: " + tahol
+ "\nتاریخ تولد: " + BirthTimePicker.Value.ToPeString()
+ "\n__________________________________________________";
Form frm2 = new Form2();
frm2.Show();
}

You can use Application.OpenForms. Gets a collection of open forms owned by the application.
List<Form> forms = new List<Form>();
// All opened myForm instances
foreach(Form f in Application.OpenForms){
if (f.Name == "Form2"){
f.Close();
break;
}
}
You can Show form like
Form2 ff = new Form2();
ff.Show();

Declare the Variable for the Form2 as class variable outside of the scope of the btnSave_Click method. This way you will be able to access it again when the button is clicked a second time.
Form frm2 = new Form2();
private void btnSave_Click(object sender, EventArgs e)
{
if(frm.Visible) // check whether the form is already showing
{
frm.Close(); // if yes close it first
}
frm2 = new Form2(); // then make a new form and show it
frm2.Show();

Related

I want to make a redirection webform2 that shows a message based on the gender chosen in radiobutton in webform1

This is the webform1 code and I made a string called pass to store the text in. What should I do in webform2 to display the message based on the conditions?
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("two.aspx", true);
if (Request.Form["TextBox1"] != null && Request.Form["TextBox2"] != null)
{
if (RadioButton2.Checked)
{
//pass = Response.ToString().Insert(Response.ToString().Count(), " " + "Welcome, " + Server.HtmlEncode(Request.QueryString["TextBox1"]) + ". <br/> The url is " + Server.HtmlEncode(Request.Url.ToString()));
pass = "Thank you Ms. " + TextBox1.Text + "Your Registration has been successfully completed" ;
}
else if (RadioButton1.Checked)
{
pass = "Thank you Mr. " + TextBox1.Text + "Your Registration has been successfully completed";
}
}
}
Based on whatever i understand, i am providing some solution:
in webform1.aspx:->place below code last line before closing the function
Application["pass_value"] = pass;
Response.Redirect("~/webform2");
Response.Redirect("two.aspx", true);->Not required
in webform2.aspx:
Label1.Text = Application["pass_value"].ToString();
in webform2.aspx.cs design page:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

in C#, Append selection multiple times from 2 ComboBoxes into a TextBox using a button

I am trying to append selections from 2 ComboBoxes into a TextBox with a click of a button and I am not sure how to do this. I can do it once with code like this:
private void BTN_APPEND_Click(object sender, EventArgs e)
{
TB_CONNECTORS.Text = CB_PORT_NUMBER.Text + " " + CB_CONNECTOR.Text;
}
And this will result in
Append with the click of a button
but the question is, how can I append to this one more time?
Did you try this :
if(!TB_CONNECTORS.Text == "")
{
TB_CONNECTORS.Text = TB_CONNECTORS.Text + " & " + CB_PORT_NUMBER.Text + " " + CB_CONNECTOR.Text;
}
else
{
TB_CONNECTORS.Text = CB_PORT_NUMBER.Text + " " + CB_CONNECTOR.Text;
}
This will result in the new value achieved from the 2 comboboxes keeping the existing text of the textbox

dynamically create a button

In Visual Studio C# windows form how do I dynamically create a button each time an item is inserted into a listbox ?
When the created button is clicked it has to remove the inserted item from the listbox.
I want to add the button th this:
if (comboBox4.Text != "" && listBox1.Text != "" && comboBox3.Text != "")
{
string ha = listBox1.SelectedItem.ToString();
Clipboard.SetText(comboBox4.Text + "stk " + ha + " i farve " + comboBox3.Text);
listBox2.Items.Add(comboBox4.Text + "stk " + listBox1.SelectedItem.ToString() +
" " + comboBox3.Text);
}
Create a button and add to the form.
Pseudo-code (not the complete solution, only the main login behind it):
public class MyEventArgs: EventArgs
{
public ListBoxValue lbv;
}
...
listBox2.Items.Add(comboBox4.Text + "stk " + listBox1.SelectedItem.ToString() + " " + comboBox3.Text);
Button x = new Button();
x.Text = "whatever";
x.Top = some coordinate;
x.Left = some coordinate;
MyEventArgs me = new MyEventArgs();
me.lbv = inserted lisbox item reference;
x.Click += new ClickHandler(this, me);
myForm.Controls.Add(x);
...
private void ClickHandler(object sender, MyEventArgs e)
{
listbox.Items.Remove(e.lbv);
}

How do I pass variables stored in Properties into another windows form?

So I need to be able to pass a property(name) into another windows form.
In the first form, the user is prompted to key in their name, while in the second one, their name is shown.
My problem is that although the value keyed in in the first form is saved (I have a Message box to show me) when the new form runs, the value of the property is reset to the placeholder name from the constructor class. Here are the codes(Form1 being the second form)
Both of them have initialised the reference to the contructor class at the start.
else if (select > 0 || txtName.Text != "")
{
p.Name = txtName.Text; // Save Name as property
MessageBox.Show("" + p.Name);
this.Hide();
Form1 form = new Form1();
form.ShowDialog();
}
For Form1:
private void Form1_Load(object sender, EventArgs e)
{
setName();
MessageBox.Show("" + p.Name);
timer1.Start();
label3.Text = "Player: " + p.Name;
}
Create a property in Form1 to accept the name:
public class Form1 : Form
{
//other stuff
public string Name {get;set;}
}
Then set that property when creating the form:
else if (select > 0 || txtName.Text != "")
{
this.Hide();
Form1 form = new Form1();
form.Name = txtName.Text;
form.ShowDialog();
}

Button click Open richTextBox and display the readfile

I have 2 buttons and I read different files when I click on these buttons. I used the to display the readfile using MsgBox since the files are big, so i want to display it in a richTextBox.
How can I open a richTextBox and display the read file when I click on any one of these buttons???
private void button1_Click(object sender, EventArgs e)
{
DisplayFile(FileSelected);//DisplayFile is the path of the file
var ReadFile = XDocument.Load(FileSelected); //Read the selected file to display
MessageBox.Show("The Selected" + " " + FileSelected + " " + "File Contains :" + "\n " + "\n " + ReadFile);
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo)comboBox2.SelectedItem;
StreamReader FileRead = new StreamReader(file.FullName);
string FileBuffer = FileRead.ReadToEnd(); //Read the selected file to display
//MessageBox.Show("The Selected" + " " + file + " " +"File Contains :" + "\n " + "\n " + FileBuffer);
// richTextBox1.AppendText("The Selected" + " " + file + " " + "File Contains :" + "\n " + "\n " + FileBuffer);
//richTextBox1.Text = FileBuffer;
}
Is there any other way to do it?
Here is a simple example (code based form design). It's better if you create the form via the GUI designer:
private void button1_Click(object sender, EventArgs e)
{
//test call of the rtBox
ShowRichMessageBox("Test", File.ReadAllText("test.txt"));
}
/// <summary>
/// Shows a Rich Text Message Box
/// </summary>
/// <param name="title">Title of the box</param>
/// <param name="message">Message of the box</param>
private void ShowRichMessageBox(string title, string message)
{
RichTextBox rtbMessage = new RichTextBox();
rtbMessage.Text = message;
rtbMessage.Dock = DockStyle.Fill;
rtbMessage.ReadOnly = true;
rtbMessage.BorderStyle = BorderStyle.None;
Form RichMessageBox = new Form();
RichMessageBox.Text = title;
RichMessageBox.StartPosition = FormStartPosition.CenterScreen;
RichMessageBox.Controls.Add(rtbMessage);
RichMessageBox.ShowDialog();
}

Categories