So essentially I'm using the dotNetBrowser for a project that i'm loading into a panel on my main form, and I have a button in a usercontrol for user input so it can interact with the browser. Here's what I have:
public partial class Form1 : Form
{
public BrowserView browserView = new WinFormsBrowserView();
public Form1()
{
InitializeComponent();
this.panel1.Controls.Add((Control)browserView);
browserView.Browser.LoadURL("URL TO BE LOADED");
browserView.Browser.FinishLoadingFrameEvent += delegate (object sender, FinishLoadingEventArgs e)
{
if (e.IsMainFrame)
{
// Do stuff when loaded
} else return;
}
};
}
}
That works fine, in my usercontrol.cs I have:
public void button1_Click(object sender, EventArgs e)
{
BrowserView br = (this.Parent as Form1).Controls["browserView"] as BrowserView;
br.Browser.LoadURL("NEW URL");
}
So that when the button is clicked it can load a new url. But this is throwing a null exception.
Basically I need these two components to be able to pass information on to eachother. The method I've used worked fine for other Form1 controls, but not the browser it seems.
Any advice?
In your case, browserView is a name of the public variable, so you can simply use(this.Parent as Form1).browserView to access it.
Your are adding browserView to Form1.panel1, but trying to get it from (this.Parent as Form1).
You don't need to search for BrowserView when you have explicit reference to it. I suggest giving this reference to the user control. User control having the knowledge of the innards of the hosting form means that information is flowing in the wrong direction.
Names of controls are given to them by IDE, and are empty when controls are created in code.
Related
Hi I'm relatively new to C# and completely new to windows form and basically trying to create a subliminal messaging program that at timed intervals will quickly display a message for it to then be hidden again.
I've managed to by looking through various other posts created another form that will pop up and then hide very quickly using
msgObject.Activate();
that brings the form to the front. However it is stopping me from being able to type when I'm working and I basically wanting to know if it is possible to make some kind of message or form appear at the front of all my other programs without it interrupting my current work or opening or closing of other windows and tasks if that makes sense. As currently it brings the form to the front of everything but will also stop me from being able to type etc.
I'm not sure if this is possible with my current method of using a form but if there is a way of achieving the result I'm after I'd be very grateful to find out
Thanks.
Here is more of my code to clarify
public partial class FormHomePage : Form
{
private bool startSubliminal = false;
msg msgObject = new msg();
List<string> subliminalMessages = new List<string>();
public FormHomePage()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (startSubliminal)
{
msgObject.Show();
msgObject.BringToFront();
msgObject.Activate();
}
}
private void button1_Click(object sender, EventArgs e)
{
subliminalMessages.Add(txtBox1.Text);
msgObject.LabelText = txtBox1.Text;
txtBox1.Text = string.Empty;
startSubliminal = true;
msgObject.Show();
msgObject.BringToFront();
}
private void timer2_Tick(object sender, EventArgs e)
{
msgObject.Hide();
}
}
How are you showing the second form (the message form) in the first place? You're probably using .Show() (right?), which will make the form "steal" the focus anyway. The same applies to .Activate() and .BringToFront().
Instead, what you can do is to show the message form and make sure it stays on top the current form, and then activate the current/main form once again.
Something like this should work:
var frm = new YourMsgForm();
frm.Show(this);
this.Activate();
Here's a demonstration:
Note that I used .Show(this) instead of .Show(), that's in order to set the current form as the Owner of the new one, that way we guarantee that the new form will stay on top of the current one. This is equivalent to calling frm.Owner = this; then frm.Show();.
Another way to make sure the form stays on top is by setting the TopMost property instead of the Owner property. However, doing so will make the new form on top of the other windows as well (not just your application).
EDIT: If I change in Home Form private to public void then I must do a kinda concvert to bool from void... but I don't know how that works. Can you help me guys?
I am stuck here in the code.... I wanted to know how to access to my other form which has menustrip from another form.
E.G:
I want that clicking on the menustrip from other form where menustrip doesn't exists.
Here is the code:
Form 1
Home frm = new Home();
frm.IsMdiContainer = true;
if(frm.Controls["todasEntradasToolStripMenuItem"].Click += frm.todasEntradasToolStripMenuItem_Click)
{
{something}
}
The form Home is "frm" variable and it is where it has the menu strip. I want help to change the protection level so that this form (Form1) can accept this code... Anyone can help me please?
Solution 1 (nice):
Add your Click event in some Init-method or the constructor in Home. There you can access your control.
todasEntradasToolStripMenuItem.Click += todasEntradasToolStripMenuItem_Click;
Also in Home you define a new event:
public event EventHandler<EventArgs> TodasEntradasToolStripMenuItemClick;
private void OnTodasEntradasToolStripMenuItemClick(EventArgs e)
{
if (todasEntradasToolStripMenuItem != null)
{
TodasEntradasToolStripMenuItemClick(this, e);
}
}
In the Click handler you raise your own public event:
private void todasEntradasToolStripMenuItem_Click(object sender, System.EventArgs e)
{
OnTodasEntradasToolStripMenuItemClick(e);
}
In Form1 you add your Handler to this public event:
Home frm = new Home();
frm.TodasEntradasToolStripMenuItemClick += frm_TodasEntradasToolStripMenuItemClick;
In this handler you can "do something":
private void frm_TodasEntradasToolStripMenuItemClick(object sender, EventArgs e)
{
// Do something
}
Solution 2 (do not do it):
You asked for changing the protection level. So you can change
private todasEntradasToolStripMenuItem
in Home to
internal todasEntradasToolStripMenuItem
or even
public todasEntradasToolStripMenuItem
But I do not suggest you not to do this. You should choose Solution 1. With Solution 2 you would open Home for more changes than you have to.
I have a button on Form1 that starts disabled by default. I have a ConfigureForm, where I have a menu strip, with an option to enable the button in Form1.
So my code is:
private void Portal2HammerButtonEnable_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.Portal2HammerButton.Enabled = true;
}
But when I close ConfigureForm and look at the button, it's still disabled.
That's because you create a new Form1 and enable on that form the button. Instead, you have to pass the instance of the form you actually have open.
For design purposes you may want to use a controller class between these two forms. This will help you to simplify the complexity of passing data or actions between the two forms and will give you the power to escalate better the app..
When you open the ConfigureForm you have to do the following (in the simplest form however not recommended.)
...
{
ConfigureForm frmConfigure = new ConfigureForm(this);
}
Then inside the ConfigureForm:
public partial class ConfigureForm : Form
{
private From1 mainForm = null;
public ConfigureForm()
{
InitializeComponent();
}
public ConfigureForm(Form callingForm):this()
{
mainForm = callingForm as Form1;
}
private void Portal2HammerButtonEnable_Click(object sender, EventArgs e)
{
mainForm.Portal2HammerButton.Enabled = true;
}
}
You're creating a new form on the click of this button. What you want instead is a valid reference to the actual instance of Form1.
You have some options available:
If one of these forms is the "main" form of the application then you can ensure that it is created first and thus creates the other "sub" forms. you can override the constructors of any sub forms to include a reference to your "main" form.
You can keep references to all your important forms in a public static class such that all your forms can get to those references
You add your own public method to assign the "parent form" as a member or property of the child forms.
You can use reflection to find the instance of the "main" or "parent" form during creation or display of any child forms. If you do this, only do it once rather than upon every request. Try to cache that information.
You can read through the System.Windows.Forms namespace to find out if there's already a collection of objects through which you could iterate to find your main form.
I'd recommend option 2 or 5.
I have a combo box in my C# which is place in form named frmMain which is automatically fill when I add (using button button1_Click) a product in my settings form named frmSettings. When I click the button button1_Click I want to reload the frmMain for the new added product will be visible.
I tried using
frmMain main = new frmMain();
main.Close();
main.Show();
I know this code is so funny but it didn't work. :D
This is windows form!
EDIT
Please see this image of my program for better understanding.
This is my frmMain
Here is what my settings frmSettings form look like. So, as you can see when I click the submit button I want to make the frmMain to reload so that the updated value which I added to the settings will be visible to frmMain comboBox.
Update: Since you changed your question here is the updated version to update your products
This is your products form:
private frmMain main;
public frmSettings(frmMain mainForm)
{
main = mainForm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
main.AddProduct(textBox1.Text);
}
It will need the mainform in the constructor to pass the data to it.
And the main form:
private frmSettings settings;
private List<string> products = new List<string>();
public frmMain()
{
InitializeComponent();
//load products from somewhere
}
private void button1_Click(object sender, EventArgs e)
{
if (settings == null)
{
settings = new frmSettings(this);
}
settings.Show();
}
private void UpdateForm()
{
comboBoxProducts.Items.Clear();
comboBoxProducts.Items.AddRange(products.ToArray());
//Other updates
}
public void AddProduct(string product)
{
products.Add(product);
UpdateForm();
}
You then can call UpdateForm() from everywhere on you form, another button for example.
This example uses just a local variable to store your products. There are also missing certain checks for adding a product, but I guess you get the idea...
this.Close();
frmMain main = new frmMain();
main.Show();
There is no such built in method to set all your values as you desire. As i mentioned in the comment that you should create a method with your required settings of all controls, here is the sample code:
private void ReloadForm()
{
comboBox.ResetText();
dataGridView.Update();
//and how many controls or settings you want, just add them here
}
private void button1_Click(object sender, EventArgs e)
{
ReloadForm(); //and call that method on your button click
}
Try out this code.
this.Refresh();
Application.Doevents();
this.Refresh();
Refresh();
this.Hide();
frmScholars ss = new frmScholars();
ss.Show();
you want to Invalidate the form
http://msdn.microsoft.com/en-us/library/598t492a.aspx
IF you are looking to refresh page from usercontrol .Here is expample where i amrefreshing form from usercontrol
Find the form in which this reload button is.
Then Call invalidiate tab control and refresh it.
Dim myForm As Form = btnAuthorise.FindForm()
For Each c As Control In myForm.Controls
If c.Name = "tabControlName" Then
DirectCast(c, System.Windows.Forms.TabControl).Invalidate()
DirectCast(c, System.Windows.Forms.TabControl).Refresh() 'force the call to the drawitem event
End If
Next
Not required reload for entire form. Just create a function for form initialise.
you can call this function any time. This will refresh the form.
private void acc_Load(object sender, EventArgs e)
{
form_Load();
}
public void form_Load()
{
// write form initialise codes example listView1.Clear();...
}
private void button1_Click(object sender, EventArgs e) //edit account
{
//Do something then refresh form
form_Load();
}
If you want to automatically update the value of the other form when you click the button from another one you can use timer control. Just set the timer to 0.5s in order to update the form fast
I think that, by calling the frmMain_load(sender,e) when you are clicking the button should reload the form.
You may also try to Invalidate() the form just like #Nahum said.
I'm new to Visual C# and I'm currently stuck on how to create a new form (with code, not design) and add things (namely labels and textboxes) to this new form. Here's what I have right now:
namespace AccountInfo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
profileForm profile = new profileForm(); // Make new form
profile.Name = "newProfile";
profile.Text = "Add a new profile";
profile.LabelText = "test";
profile.Show(); // Display form
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public class profileForm : Form
{
// Controls
Label label1 = new Label();
public profileForm()
{
}
public string LabelText
{
set { label1.Text = value; }
}
private void profileForm_Load(object sender, EventArgs e)
{
}
}
}
When I run this code, I get the default form and I click button1. It brings up a new form, but with nothing on it. I expect a label to show up but it won't. I've tried this multiple different ways (this being my most recent method) and I can't get anything to show up. I've looked around StackOverflow and one other topic came up, but its solution didn't work for me. I'd appreciate any insight into this :) Thanks a ton!
Edit: I've also tried this using the constructor instead. It didn't help.
You're creating a Label object in memory but you're not assigning it to a particular parent control, or setting it's position etc... Google "Dynamically create controls C#" and you'll find a tonne of examples.
You basically need to call the following two lines from somewhere in profileForm.
label1.Location = new Point(25,25);
this.Controls.Add(label1);
As suggested by Dylan, you need to add the Label object to the profileForm in the load event as follows:
this.Controls.Add(label1);
Soon, i was watching a video that answers to this question.
It is complete guide how to add dinamicly controlls with the flow layout.
Here is the video: http://windowsclient.net/learn/video.aspx?v=13245