how to control main form from another form example I want to access the table in main form from another form in wpf?
You can either use UI automation (which would only let you interact with it as if you were a user clicking on/typing in the controls):
http://msdn.microsoft.com/en-us/library/dd561932(VS.85).aspx
Or you can use code behind to pass a reference from one window to the other, probably in your Application class.
There is nothing specific to WPF that makes either option any easier or harder to implement.
salamonti,
Do you want to access a control on the main form or the data that the control is displaying? If the latter I would suggest you to keep the data in a separate area than the control presenting it. This can be achieved with MVVM and several other view separation patterns. You can also use Routed Events and Routed Commands to execute code in one "form" from a different one.
If you want to access the "main form" from a child form, you can create a property on the child form of type FrameworkElement for example. Then, when you create the child form, just populate this property with the instance of the main form. This way you'll have access to whatever you want in the main form.
this is a tiny sample of communication between windows in WPF
you can refer to controls as the way you do with class fields, cause that's what they are
public class Form1 : Window
{
public DateTime FormCreationDate {get; set;}
private void button1_Click(object sender, RoutedEventArgs e)
{
Form2 a = new Form2();
a.Owner = this;
a.Show();
}
}
public class Form2 : Window
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.label1.Content = string.Format(
"the owner of this window was created on {0}",
((Form1)this.Owner).FormCreationDate.ToString());
}
}
Related
So I have user control I want to hide/send to back and I want to call the public function in my form where the user control is, from the control itself.
I have a button in the user control with the following code:
mainboard MAIN = new mainboard(); // mainboard is a form to call to.
MAIN.PastLockScreen(); // PastLockScreen is a public void inside mainboard
When I click the button the public function in mainboard does not get called. There are no errors, what am I doing wrong, and how can I call a function in a form from a user control?
void inside mainboard
public void PastLockScreen()
{
lockscreen1.SendToBack(); // lockscreen1 is the usercontrol that this function gets called from
}
The void is being referenced but not called?
Edit: I have done some investigating and turns out that my timers I have in any form or control, also dont work. But buttons on the actual form itself do work. (and yes I did do timerName.Start(); when the form/control loads.)
Solved the issue above, my timers needed to display time, which the time string I defined inside the class instead of inside the timer.tick
From within the UserControl, just cast ParentForm to type mainboard:
// .. form within the UserControl that is CONTAINED by form mainboard ...
private void button1_Click(object sender, EventArgs e)
{
mainboard MAIN = this.ParentForm as mainboard;
if (MAIN != null)
{
MAIN.PastLockScreen();
}
}
Note that this is a TIGHTLY COUPLED approach that limits your use of the UserControl to only mainboard.
A better approach would be to make the UserControl raise some kind of custom EVENT that the mainboard form then subscribes to. When the event is received then the form itself would run the appropriate method. This would mean you could potentially utilize the UserControl in a different form without changing any code within it. This latter approach would be LOOSELY COUPLED.
Try This
In Form1
to Show the Form2
Form2 frm2 = new Form2();
frm2.Show();
To call the method do you want
Form2 cmd = new Form2();
cmd.PastLockScreen();
In Form2
public void PastLockScreen()
{
this.SendToBack();
}
I'm making a windows form app and I have one form that represents a base form and it gets data from a different class (that's almost like a database):
private void base_form_Load(object sender, EventArgs e)
{
database_class dc = new database_class();
button1.Text =dc.Name;
}
NOTE: The reason why this code is in the form_Load is because it doesn't show up, unless I put it there, which I find strange, but it might not be?
I have a main form, that acts like a menu - it has four buttons on it and all the buttons lead to the base form. The database class is actually supposed to change the names of the controls in the base form based on the what button is chosen in the main form(menu). The base form has a lot more buttons than the main form.
Since this is confusing, here's an example of what I want to do: If the menu had options (buttons) Mozart, Beethoven, Liszt, Chopin - when people click on Mozart they're supposed to get buttons with the names of his compositions, if they click Beethoven then they get his compositions and so on.(These buttons in the base form do lead to something else, if that's important/helpful). The reason I'm not making separate forms for these menu buttons, is because I have a lot of buttons and I don't think making plenty of forms is ideal (it's a simple app, I don't want to slow it down with a lot of forms).
My question is what is the best way to do this? Do I have to somehow assign the data I want to the button (mouse) click events in the menu (main form)? Is there a possibility of having different form loads in the base form, that can be assigned to the mouse clicks in the menu?
Thank you for your time.
Example constructor as requested - form has a richTextBox that gets populated by the constructor and also a string value that's used later:
public partial class Form2 : Form
{
public string _filename;
public Form2(string text, string filename)
{
InitializeComponent();
richTextBox1.Text = text;
_filename = filename;
}
}
Create an instance of this form from the main form:
private void button1_Click(object sender, EventArgs e)
{
string textForRTB = "Some text";
string valueForFilename = "\some\file\name";
Form2 frm2 = new Form2(textForRTB, valueForFilename);
frm2.Show();
}
PROBLEM SOLVED
SHORT STORY
I want to detect "FormClosing()" event through different forms, ie, when form1 is closed that is instantiated within form2, can form2 detect when user presses exit in form1?
LONG STORY
My team and I are working on a windows form application. Project has two forms: one is the main form page and the other is accessed via this main form. Main form looks like this:
And the second one looks like this:
If you press "Ekle/Sil" buttons within the main form, you are directed to form 2 where you can edit database entries. When you press "Sayfayı Yenile" button in the main form, the content of the text areas are refreshed by re-fetching entries from the database.
My problem is, I want to automatically refresh the main form when the user closes the second form. My research suggests I should use an "FormClosing()" event to detect a closing form. However, I want to detect this from the main form. Instantiating main form in second form's source code doesn't seem to be a reliable solution. Anyone can tell me how to do this?
EDIT
I solved the problem:
1) Created a public method within the main form that refreshes the page.
2) Send "this" property from the main form when creating the second form.
3) Added an "FormClosed()" handler within the second form that invokes this public method.
Still, I'm looking for a better solution.
EDIT 2
Better solution InBetween's answer
Simply use the Form.Closed event of the new child windows form. Everything is handled from the main form:
void EkleSil_Clicked(object sender, EventArgs e) //or whatever method is called when button is clicked
{
var newChildForm = new ChildForm();
newChildForm.Closed += childFormClosed;
newChildForm.Show();
}
void childFormClosed(object sender, EventArgs e)
{
((Form)sender).Closed -=childFormClosed;
updateShownData();
}
You can create a event in the second form and raise it when the form is closing .
Handle the event in the main form and refresh the main form when the event is raised
Another option would be to pass Form1 as an argument to Form2. Then use the Form.Closing event in Form2 and use the Form1 reference to trigger something.
Form1 form1Ref;
public Form2(Form1 mainform)
{
form1Ref = mainform;
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
form1Ref.SomeMethod();
}
I have a windows form app. The main form has a textbox, and a button to launch another form. When the other form is launched, both forms are on screen (but the launched form is modal). The child form has a textbox and button, and when the button is pressed, I want the textbox on the main form (the parent) to be updated with the value in the textbox on the child form.
How is this functionality achieved?
Ideally you want to keep both forms from being dependent on each other, this could be achieved with interfaces:
public interface IMainView
{
public void UpdateValue(string val);
}
public interface IChildView
{
public void Show(IMainView parent);
}
have your main form implement IMainView and the child implement IChildView, the parent calls child.show(this) and the child calls parent.UpdateValue(blah);
Hope this helps.
If the child form is closed when the button is clicked, you could put a public property which wraps the value of the textbox on the child form. Then the main form can read this property after calling ShowDialog.
If you want this to happen without closing the child form, you can create a function on the main form to change the textbox. Then the child form would call that function.
The best ways to achive this situation are clockWize's and Hans Passants's advices.
But what about that?
Write a property for your textbox at parent form, like this.
public string TextBoxText
{
get { return txtTextBox.Text;}
set { txtTextBox.Text = value;}
}
When you are opening the child form set the owner.
ChildForm f = new ChildForm();
f.Owner = this;
f.Show();
Create an event handler to child forms button click event.
public Button1_Click(object sender; EventArgs e)
{
ParentForm f = (ParentForm)this.Owner;
f.TextBoxText = txtChildTextBox.Text;
}
i didn't compile code; so may have errors :)
}
When a button is pressed to close the launched form, returning you to the main form- the launched form's text box is still in scope.
Closing a form is merely changing the object's state, not disposing of it. So in the button eventhandler that launches the form from the main form, the next line after launching your modal window, it can access the text from the object it launched as the textbox is a child of that form's object. Unless you're launching your modal window in another thread, which I wouldn't figure you are since it's modal, when it is closed, it should go to the next line in the buttons eventhandler that launched it.
your main form may have code something like this right now (haven't done winforms in a while so bear with me if I miss something):
public void Button1_Click(object sender, ClickEventArgs e)
{
SomeFormIWantToLaunch launchForm = new SomeFormIWantToLaunch();
launchForm.ShowDialog(this);
}
You need to just add after launchForm.ShowDialog(this); something like:
this.SomeTextBox.Text = launchForm.ATextBox.Text;
Is it possible to disable the hittest on a Windows Forms window, and if so, how do I do it? I want to have a opaque window that cannot be clicked.
Thanks in advance,
Christoph
If you're talking to a different process, you need to send and retrieve Windows messages.
http://www.c-sharpcorner.com/UploadFile/thmok/SendingWindowsMessageinCSharp11262005042819AM/SendingWindowsMessageinCSharp.aspx
Have a look at this link:
Using Window Messages to Implement Global System Hooks in C#
http://www.codeproject.com/KB/system/WilsonSystemGlobalHooks.aspx
Global system hooks allow an application to intercept Windows messages intended for other applications. This has always been difficult (impossible, according to MSDN) to implement in C#. This article attempts to implement global system hooks by creating a DLL wrapper in C++ that posts messages to the hooking application's message queue.
Do you want a window that cannot be moved? Set FormBorderStyle to none.
Well, I still don't know much about your use case, but I'll take a stab anyway, and provide a simple example.
I assume that you want to control something on the main form from your floating form.
To do this, you need a reference to your main form from your floating form. You do this by creating a constructor overload in your floating form that accepts an instance of your main form, like this:
public FloatingForm(MainForm mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
The floating form contains a textbox named floatingFormTextBox, and a button named Button1. The partial class for the floating form looks like this:
public partial class FloatingForm : Form
{
MainForm _mainForm;
public FloatingForm()
{
InitializeComponent();
}
public FloatingForm(MainForm mainForm)
{
InitializeComponent();
_mainForm = mainForm;
}
private void button1_Click(object sender, EventArgs e)
{
_mainForm.DoSomething(floatingFormTextBox.Text);
}
}
The main form just contains a textbox named mainFormTextBox. When the main form loads, it creates an instance of the floating form, passing a reference to itself to the floating form's new constructor overload. The partial class for the main form looks like this:
public partial class MainForm : Form
{
FloatingForm _floatingForm;
public MainForm()
{
InitializeComponent();
}
public void DoSomething(string text)
{
mainFormTextBox.Text = text;
this.Refresh();
}
private void MainForm_Load(object sender, EventArgs e)
{
_floatingForm = new FloatingForm(this);
_floatingForm.Show();
}
}
Now, when I put some text into the textbox of the floating form and click the button, the text shows up in the textbox of the main form.