How to access form1.webbrowser from form3? - c#

I made a web browser in C# Windows forms, and I made form 3 to be the history, form 3 contains listbox and a button called go!
I want button_click to navigate the webbrowser1 (located in form1) to listbox1.selecteditem.tostring().
In form1 constructor:
public Form1()
{
x = new Form3();
x.Show();
x.Visible = false;
InitializeComponent();
}
and in the button that open the form 3
{
x.Visible = true;
}
in form 3 button that says go :
{
// namespace.form1.webbrowser1.navigate(listbox1.selecteditem.tostring()); //
this.Visible = false;
}
the error in the comment line , what is the solution to access the webbrowser from form 3 !!

Pass Form1 to Form3 constructor as parameter:
class Form3
{
Form1 _parent;
public Form3(Form1 parent)
{
_parent = parent;
}
public void Method()
{
_parent.webbrowser1.navigate(listbox1.selecteditem.tostring());
this.Visible = false;
}
}
also, make webbrowser1 public or better make a public method in Form1:
class Form1
{
public void Navigate(string uri)
{
webbrowser1.navigate(uri);
}
}
and call it from Form3:
public void Method()
{
_parent.Navigate(listbox1.selecteditem.tostring());
this.Visible = false;
}

Related

How to fix "Change MdiParent in MdiChild" in C#

My codes to change MdiChild in MdiParent class
public void SetupMdi(Form form)
{
clearMdi();
activeMdiForms.Add(form);
form.MdiParent = this;
form.Show();
form.Location = new Point(0, 0);
foreach(Form forms in activeMdiForms)
{
MessageBox.Show(forms.ToString());
}
return;
}
public void clearMdi()
{
foreach(Form form in activeMdiForms)
{
form.Dispose();
}
activeMdiForms.Clear();
return;
}
It's working perfectly in parent class
private void Menu_Load(object sender, EventArgs e)
{
VersionChecker ver = new VersionChecker();
versionLbl.Text = "Depo Stok Programı Version " + earlySettings.version;
SetupMdi(new Login());
GCTimer.Start();
}
But I called SetupMdi method from child form its working but child form not showing but it using ram
public partial class Login : Form
{
public async void login()
{
earlySettings.usrName = obj.UserName;
MainMenu form = new MainMenu();
new Menu().SetupMdi(new MainMenuMdi());
this.Dispose();
}
}
I tried an ApiClass its not working like child class
But I called SetupMdi method from child form its working but child
form not showing but it using ram
Assuming your code is in an actual MdiChild, then you can cast the MdiParent property to your parent type (I believe, Menu?), and then call the SetupMdi() method:
// ... assuming we are in an MdiChild ...
((Menu)this.MdiParent).SetupMdi(new MainMenuMdi());

Add UserControl2 to MainForm from programmatically added UserControl1 to MainForm

I have a MainForm which has a panel. I have added a UserControl1 in it while start the application. by clicking a button inside of UserControl1 trying to call UserControl2 to add the panel and clear UserControl1 from main form panel. Then by clicking second button inside of UserControl2 trying to call UserControl1 and clear UserControl2 from main forms panel. But in my code when I call UserControl2 MainForm comes blank. what is the problem? here is code:
namespace UserControlTest
{
public partial class MainForm : Form // main from
{
private static MainForm _instance;
public static MainForm Instance
{
get
{
if (_instance == null)
{
_instance = new MainForm();
}
return _instance;
}
}
public MainForm()
{
InitializeComponent();
if (!panel1.Controls.Contains(UserControl1.Instance)) // checking UserControl1 existance
{
panel1.Controls.Add(UserControl1.Instance); // add
UserControl1.Instance.Dock = DockStyle.Top; // dock it
UserControl1.Instance.BringToFront(); // bring to front of panel1
}
}
public void CallUserControl1() // method helps to bring UserControl1 to front of panel 1
{
if (!panel1.Controls.Contains(UserControl1.Instance))
{
panel1.Controls.Clear();// clearing controls
panel1.Controls.Add(UserControl1.Instance);
UserControl1.Instance.Dock = DockStyle.Top;
UserControl1.Instance.BringToFront();
}
else
{
UserControl1.Instance.BringToFront();
}
}
public void CallUserControl2()// method helps to bring UserControl2 to front of panel 1
{
if (!panel1.Controls.Contains(UserControl2.Instance))
{
panel1.Controls.Clear();// clearing controls
panel1.Controls.Add(UserControl2.Instance);
UserControl2.Instance.Dock = DockStyle.Top;
UserControl2.Instance.BringToFront();
}
else
{
UserControl2.Instance.BringToFront();
}
}
}
}
// first user control which helps to call UserControl2 to come front page
public partial class UserControl1 : UserControl
{
private static UserControl1 _instance;
public static UserControl1 Instance
{
get
{
if (_instance == null)
{
_instance = new UserControl1();
}
return _instance;
}
}
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MainForm mainForm = new MainForm();
mainForm.CallUserControl2();
}
}
// second user control which helps to call UserControl1 to come front page
public partial class UserControl2 : UserControl
{
private static UserControl2 _instance;
public static UserControl2 Instance
{
get
{
if (_instance == null)
{
_instance = new UserControl2();
}
return _instance;
}
}
public UserControl2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MainForm main = new MainForm();
main.CallUserControl1();
}
}
Update: Thanks to comment of #RonBeyer this is worked but needed to clear controls of panel first than
var main = this.FindForm() as MainForm;
main.CallUserControl1();

Get Text from text box on Form1 when calling function on Form2

Background/Context
So I am building a program that take 2 Excel Files and compares them highlight the differences. This is working fine. Now i am developing a second form, which does a very similar thing but essentially "Applies" the changes. Now in the first form I have two text boxes which contain the file locations, however on the second form which appears after the changes are highlighted there is only an Apply button hence I need to pull down the text box path for the file from Form1 however this doesn't seem to work in mt code:
CODE
public partial class Form2 : Form
{
Form1 form1 = new Form1();
public Form2()
{
InitializeComponent();
btnApply1.Click += new EventHandler(this.btnApply_Click);
btnCancel1.Click += new EventHandler(this.btnCancel1_Click);
}
private void btnApply_Click(object sender, EventArgs e)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
new CRCCompareWorksheets.CompareHelper().ApplyChanges(
form1.ExcelPath1.Text, form1.ExcelPath2.Text, "CRC");
}
private void btnCancel1_Click(object sender, EventArgs e)
{
new CRCCompareWorksheets.CompareHelper().CancelApplication();
}
}
The Problem
So at the line where i call the function for Applying the changes, the variables form1.ExcelPath1.Text and form1.ExcelPath1.Text are both blank hence the file locations are not being pulled through and nothing works :(
The problem here is, that you create a new Instance of Form1. I guess, that Form2 is opened by Form1. In that case I would provide a reference of the calling form to the newly generated Form2. This could look something like the following:
Form2
public partial class Form2 : Form
{
Form1 form1 = null;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
btnApply1.Click += new EventHandler(this.btnApply_Click);
btnCancel1.Click += new EventHandler(this.btnCancel1_Click);
}
private void btnApply_Click(object sender, EventArgs e)
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("EXCEL"))
{
clsProcess.Kill();
break;
}
}
new CRCCompareWorksheets.CompareHelper().ApplyChanges(form1.ExcelPath1.Text, form1.ExcelPath2.Text, "CRC");
}
private void btnCancel1_Click(object sender, EventArgs e)
{
new CRCCompareWorksheets.CompareHelper().CancelApplication();
}
}
In Form1 you would then need to change the calling of Form2 to something like this:
Form2 frm = new Form2(this);
frm.Show();
Additionally it would be good practice to create properties for the values you want to read from the TextBoxes instead of making the controls public:
// Properties in Form1
public string ExcelPath1Text
{
get
{
return this.ExcelPath1.Text;
}
set
{
this.ExcelPath1.Text = value;
}
}
public string ExcelPath2Text
{
get
{
return this.ExcelPath2.Text;
}
set
{
this.ExcelPath2.Text = value;
}
}
And then use the properties in Form2:
CRCCompareWorksheets.CompareHelper().ApplyChanges(form1.ExcelPath1Text, form1.ExcelPath2Text, "CRC");
At the moment you are creating a new form1 in form2, rather than using the existing form1 that has the data you want to pass to form2.
You should pass form1 to the form2 constructor:
public partial class Form2 : Form
{
Form1 form1 = { get; set; }
public Form2(Form form1)
{
this.form1 = form1;
InitializeComponent();
btnApply1.Click += new EventHandler(this.btnApply_Click);
btnCancel1.Click += new EventHandler(this.btnCancel1_Click);
}
And when you create form2 (I assume this is in form1):
Form2 form2 = new Form2(this);

MDI Parent form

Form1 objForm1 = new Form1 ();
objForm1 .MdiParent = this;
objForm1 .Show();
This is my code to open MDI form . IF I open this page again, it appears again and again and so many windows opens. Can anybody help?
If you want to create a new form only if it is not already opened you can do this:
ShowFormIfNotOpen(this,typeof(Form1));
public static void ShowFormIfNotOpen(Form mainform,Type type)
{
foreach (Form item in mainform.MdiChildren)
if (item.GetType() == type)
{
item.Activate();
return;
}
Form form = Activator.CreateInstance(type) as Form;
form.MdiParent = mainform;
form.Show();
}
Update
1)Add a public static bool field/property in your form (IsAlreadyShown)
public static bool IsAlreadyShown { get; set; }
2)Set it to true in the constructor of the form
public Form1()
{
InitializeComponent();
IsAlreadyShown = true;
}
3)Call ShowForm1(this);
public static void ShowForm1(Form parentForm)
{
if(Form1.IsAlreadyShown ==true)
return;
Form1 objForm1 = new Form1 ();
objForm1 .MdiParent = parentForm;
objForm1 .Show();
}
What i have understand so far from your question, you want form1 to be MDI Container. if you want this,then simply set the property ISMdiContainer to be true.Now if you want this form Form1 to be set as parent of any form you can use your code
FormAny objFormAny = new FormAny ();
objFormAny .MdiParent = objForm1; // reference of MDI Container
objFormAny .Show();

How to add row in another form if running in mainForm

I've tried this code to add a row of function that I created on the main form (Form1) in DataGridView in Form2, but there is no result add row when I run its function, does anyone know where the mistake?
Thanks
[edit]Code in MainForm1 :
public partial class Form1 : Form
{
Form2 sub = new Form2();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
sub.AddRow(new string[] { "1", "2" }); // able to loop
sub.Show();
this.Enabled = false;
}
}
[edit] Code in Form2 :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void AddRow(string[] values)
{
this.DGVFile.Rows.Add(values);
}
}
I get a few hints at some answers. My questions were answered when the data is loaded in datagridview in Form2 and also I want the mainform (Form1) can be disabled when opening Form2. Now it has been resolved. Thanks All.
Use sub.Show(); instead. This way, the code bellow sub.ShowDialog(); will not even get called.
ShowDialog opens a new form as a modal window, which means, that the focus is only in the new window and lines below this call are called when the new windows is closed.
You can find more information about these two methods on MSDN here and here.
UPDATE:
but i want disabled mainform if open form2
I can think of two options.
The first, and in my opinion better, is to postopne opening of the form
Form2 sub = new Form2();
sub.RegisterParent(this);
for (int x = 1; x <= 5; x++ )
{
string[] row;
row = new string[] { "1","2"};
sub.DGVFile.Rows.Add(row);
}
sub.ShowDialog(); // open when everything is prepared
The second is
sub.Show();
this.Enabled = false;
but this is not really a nice solution, because you would have to enable it from the other form before closing it and there are many ways how to close a form and you would have to consider all.
Take the creation of the second form from the button click event out: Form2 sub = new Form2();. This way the second form is created only once. Then use ShowDialog and Hide methods to show a modal second form (the first form will be inactive). Create an extra public method in the second form to add new row to the existing DataGridView.
A possible solution can look like this:
public partial class MainForm : Form
{
private DataForm dataForm = null;
public MainForm()
{
InitializeComponent();
// init data form
dataForm = new DataForm();
dataForm.Init();
}
private void buttonAddRow_Click(object sender, EventArgs e)
{
// add values
dataForm.AddRow(new string[] { "10", "20" });
// show a modal second form, so that the first can not be selected while the second is open!
dataForm.ShowDialog(this);
}
}
public partial class DataForm : Form
{
public DataForm()
{
InitializeComponent();
}
// init the grid
internal void Init()
{
this.dataGridView.Columns.Add("A", "A");
this.dataGridView.Columns.Add("B", "B");
for (int x = 1; x <= 5; x++)
{
string[] row;
row = new string[] { "1", x.ToString() };
this.dataGridView.Rows.Add(row);
}
}
// add values to the grid
public void AddRow(string[] values)
{
this.dataGridView.Rows.Add(values);
}
// hide second form
private void buttonClose_Click(object sender, EventArgs e)
{
Hide();
}
}
Output after two additions is:
The code after sub.ShowDialog(); is executed only after the form sub is closed. Try using sub.Show()
Please review the following code:
namespace TEST_CSA
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 sub = new Form2();
sub.Show(this);
this.Enabled = false;
sub.RegisterParent(this);
for (int x = 1; x <= 5; x++)
{
string[] row;
row = new string[] { "1", "2" };
sub.DGVFile.Rows.Add(row);
}
}
}
}
The command Show(this) makes Form1 the parent form of Form2 and also ensures that Form2 will be always displayed on top of Form1. To deactivate Form1 use this.Enabled = false; In order to reactivate Form1 when Form2 closes use the following code:
namespace TEST_CSA
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 _parent;
internal void RegisterParent(Form1 form)
{
this._parent = form;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
_parent.Enabled = true;
}
}
}

Categories

Resources