How to make this open a webbrowser inside a new form - c#

private void button5_Click(object sender, EventArgs e)
{
if (domainUpDown2.Text == "Battlefield: Bad Company 2")
{
Form2 form2 = new Form2();
form2.ShowDialog();
}
}
All that does is open a new blank form, but i need it to open a new form with a webbrowser in it so i can set it url dependent on the if statement..

Assuming your form2 has a WebBrowser control, and that it has a property you can set like this:
public Uri WebLocation
{
set { webBrowser1.Url = value; }
}
Then modify your code as follows:
private void button5_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
if (domainUpDown2.Text == "Battlefield: Bad Company 2")
form2.WebLocation = new Uri("http://badcompany2.yoursite.com");
if (domainUpDown2.Text == "Some Other Item")
form2.WebLocation = new Uri("http://someotheritem.yoursite.com");
form2.ShowDialog();
}

Related

How to change label after close form

When I start app form1 labelStatus = "Can't Edit" and after I click btnedit form2 is open
and
labelStatus = "Editable "; but after I close form2 by button close on windows it don't change
back labelStatus to "Can't Edit" again
Form1
public static string txt;
private void btnEdit_Click(object sender, EventArgs e)
{
editForm edit= new editForm();
editForm.Show();
labelStatus.Text = "";
}
In form1
Where I should put labelStatus.Text=txt;
Form2
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1.txt = "Can't Edit";
}
Find Form1 by Form1 name from OpenForms in Appliction
use following code
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
var form1 = Application.OpenForms.OfType<Form1>().Where(x => x.Name == "formName").FirstOrDefault();
form1.labelStatus.Text = "Can't Edit";
}

How do I show data in DataGridView of another form?

I want to show data in DataGridView which resides in form1 and data reside in form2. Form2 has a button named “ADD” which adds all the data in form1’s grid. Following code is working properly for the same form, how do I edit this code in order to show data from another form.
private void btn_Add_Click(object sender, EventArgs e)
{
InputUserInfo frm1 = new InputUserInfo();
frm1.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
frm1.dataGridView1.RowTemplate.Height = 120;
frm1.dataGridView1.AllowUserToAddRows = false;
int numberOfRows = frm1.dataGridView1.Rows.Count;
if (numberOfRows < 5)
{
MemoryStream ms = new MemoryStream();
thumb_pic.Image.Save(ms, thumb_pic.Image.RawFormat);
byte[] img = ms.ToArray();
frm1.dataGridView1.Rows.Add(lbl_CP_UserID.Text, lbl_CP_Name.Text, img);
}
else
{
MessageBox.Show("Please insert Only 5 Images");
}
this.Hide();
frm1.Show();
}
Maybe you have found a solution already but this might help someone in future.
The form (see pic Form1) with grid
Code behind this Form:
public Form1()
{
InitializeComponent();
}
public DataGridView DataGridView1
{
get
{
return dataGridView1;
}
}
private void btnAddItems_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.TopMost = true;
frm.ShowDialog();
}
Form (See pic Form2) to add data into Form1 dataGridView
Code behind this Form:
public Form1 frm1;
public Form2(Form1 gridForm)
{
InitializeComponent();
frm1 = gridForm;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (frm1.DataGridView1.Rows.Count < 5)
{
frm1.DataGridView1.Rows.Add(txtOrderNo.Text, txtDesc.Text);
frm1.DataGridView1.Refresh();
txtOrderNo.Text = txtDesc.Text = "";
if (frm1.DataGridView1.Rows.Count == 5) this.Close();
}
else
{
MessageBox.Show("You can only add up to 5 items.");
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}

How to open form 2nd time when it's closed

i doing window app using c#.net.
i have a form name form_1 with menu-strip.
from the menu-strip of form_1, i am opening same form form_1 and closing the same form_1 after using it but if i click that for the second time it is not showing,if i click that for third time it is showing.
edit:
mainform
form fm;
bool frm= false;
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
if (frm== false)
{
fm= new form();
fm.MdiParent = this;
fm.Show();
frm= true;
}
else
{
if (fm.IsDisposed)
{
frm= false;
}
}
}
form
form fm = new form();
fm.MdiParent = this;
fm.Show();
this.Close();
If you expect your function addToolStripMenuItem_Click to always open fm (assuming it is disposed), then you'll need fm.show() in the else condition as well. You could try something like this instead...
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!frm || fm.IsDisposed)
{
if (fm != null && fm.IsDisposed) { frm = false; }
fm = new form();
fm.MdiParent = this;
fm.Show();
frm = true;
}
}
This probably makes your bool frm obsolete, but I left it in in case you're using it for something else.

how can i hide my second form dialog whilst not closing my first form?

I am using Visual studio 2012. I created two forms, form1 with a button to open form2 and the form2 has a button "exit" which will take me back to form1.
this is my code from form 1:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
this.Hide();
}
and from form 2:
private void btnExit_Click(object sender, EventArgs e)
{
this.Hide();
}
I know I can use frm2.Show(); this.Hide(); instead of frm2.showdialog();. But, I need my form 1's state to be unaltered. My form1 contains a login form, which only enables the buttons (such as the new form button) if the login is correct. So if I hide form1 and just show it again, the login resets;
On Form2 class add a property to store the reference to the parent form:
public Form ParentForm { get; set; }
Then on Form1, you can show Form2 this way, hiding Form1 at the same time:
private void btnRecords_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ParentForm = this;
this.Hide();
frm2.ShowDialog();
}
When closing Form2, you can show Form1 again:
private void btnExit_Click(object sender, EventArgs e)
{
this.ParentForm.Show();
this.Close();
}
Or even better, close Form2 this way:
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
if (this.ParentForm != null)
this.ParentForm.Show();
}
This will also show Form1 back if we the user closes Form2 using the cross button in the title bar.
Why don't you handle the login in the form_load event of the main form. The form_load does not run every time the form regains focus. Is it appropriate to close the form on them if they do not login on load? In my case, I send an email with the username of windows user to Domain administrators and close the program. They have to reopen the program to give it another go.
private void frmMain_Load(object sender, EventArgs e)
{
//Check login
Form frmLogin = new Form();
frmLogin.ShowDialog();
if (frmLogin.LoginSucessful == true)
{
btnRecords.Enabled = true;
lblWarning.Visible = false;
}
else
{
btnRecords.Enabled = false;
lblWarning.Visible = true;
lblWarning.Text = "You must first Login";
}
//other setup code here
}

How to access a Textbox in Form3 from Form1?

In (Form1) i have a setting button, when i click on it a new form ( Form2 ) is shown, using these lines of code :
private void b7_Click(object sender, EventArgs e)
{
Form3 frm = new Form3();
frm.Show();
}
In form3, i have 6 text boxes, and two button, Save and Cancel.
What i'm trying to do is to provide this form to the user so he types the neccessary data into the form, then he click the Save Settings button. In Form1, i want to access to these text boxes to get their current values ( when user clicked save settings ). I tried to add a Form4 and named it ( MiddleForm), i added 6 text boxes to it, and in Form3 (The form in the image above) i wrote these line :
private void button2_Click(object sender, EventArgs e)
{
MiddleForm mf = new MiddleForm();
mf.textBox1.Text = keywrd1.Text;
mf.textBox2.Text = keywrd2.Text;
mf.textBox3.Text = keywrd3.Text;
mf.textBox4.Text = keywrd4.Text;
mf.textBox5.Text = keywrd5.Text;
mf.textBox1.Text = thelink.Text;
Close();
}
then i tried to access the values passed to the MiddleForm from Form1 (The form where i need to use the textboxes values), in Form1, i wrote these lines (for debug purposes only)
MiddleForm mf = new MiddleForm();
MessageBox.Show(mf.textBox1.Text); // to see whether there is something
Unfortunately, it seems that nothing is passed to mf.TextBox1
How can i call the current values (Saved using save settings button) of Form3 From Form1 in order to use them in the rest of code.
Any help please on getting this to work ?
Make 6 public properties in your Form3 like that:
public partial class Form3 : Form
{
public string Value1
{
get { return this.keywrd1.Text; }
}
public string Value2
{
get { return this.keywrd2.Text; }
}
...
}
After your Form3 is closed (but before disposed) you can access text values via properties. As pointed in another answer, use ShowDialog instead of Show and close Form3 inside it's own code.
private void b7_Click(object sender, EventArgs e)
{
Form3 frm = new Form3();
frm.ShowDialog();
string value1 = frm.Value1;
...
}
Make public properties in Form3 like this
public string[] Keys
{
get
{
return new string[] { tbKey1.Text, tbKey2.Text, tbKey3.Text,
tbKey4.Text, tbKey5.Text};
}
}
public string Link { get { return tbLink.Text; } }
From Form1 you can access these properties like this
Form3 frm = new Form3();
if (frm.ShowDialog() == DialogResult.OK) {
string[] keys = frm.Keys;
string link = frm.Link;
}
Note: It is important that you use ShowDialog and not Show, since Show does not wait for the other form to close. Also, when "Save settings" is clicked in Form3, set the dialog result
DialogResult = DialogResult.OK;
Close();
so that you can check it in Form1 as shown above.
You need to do this:
var form = Form.ActiveForm as Form3;
String myText = form.txtBoxName.Text;
You should make a public field that provides the values you want to get out of the form. If you go to the source of Form1, you should add in something like this:
public string TextValue1 {
get {return TextBox1.Text;}
}
Now, you can use the Form1.TextBox1 to retrieve a string value from your Textbox.
You could try using ShowDialog it will create your Form as a Model Dialog box, you can then check the DialogResult to know wether the data was saved or the Form was canceled.
i.e.
private void button2_Click(object sender, EventArgs e)
{
Form3 frm = new Form3();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
savedSettings = frm.getTextBoxValues();
}
}
Form3
public partial class Form3 : Form
{
string[] textValues = new string[6];
public Form3()
{
InitializeComponent();
}
public string[] getTextBoxValues()
{
return textValues;
}
private void saveSettings_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.OK;
textValues[0] = textBox1.Text;
textValues[1] = textBox2.Text;
textValues[2] = textBox3.Text;
textValues[3] = textBox4.Text;
textValues[4] = textBox5.Text;
textValues[5] = textBox6.Text;
this.Close();
}
private void cancelSettings_Click(object sender, EventArgs e)
{
DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.Close();
}
}

Categories