Silverlight4.0: Set childwindow field in MainPage not work - c#

I want to pass some data from MainPage to its childwindow as follows:
public MainPage()
{
InitializeComponents();
ChildWindow childwind = new ChildWindow();
childwind.textblk1.Text += "Test!";
//break point here
ContentFrame.Content = childwind;
}
textblk1 is the name of a TextBlock in ChildWindow. But there is no text "Test!" shown in the page when I run it. Though I work through this problem by just setting the text directly in the ChildWindow's constructor from reading added property in App, I still wonder why the above method failed? Thx.

I dont understand the usage you are trying but below should be enough if you want to set anything in a ChildWindow.
public MainPage()
{
InitializeComponent();
ChildWindow childwind = new ChildWindow();
childwind.Content = "Test!";
childwind.Show();
}

public MainPage()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
label1.Content = "Button is hit";
ChildWindow1 objchld = new ChildWindow1();
objchld.label1.Content = "I am child window";
objchld.Show();
}
hope this will useful for u

Related

Button click - redirect to UserControl.cs page in Windows Form

In my project, It consist two page. Those are Form1.cs,myusercontrol1.cs.
Form1 having Button. In Button_click event need to redirect Form1 to myuserontrol1.cs page.
This is my code:
private void button1_Click(object sender, EventArgs e)
{
// Code1
var destinationform = new myusercontrol1();
destinationform.Show();
// Code2
myusercontrol1 destinationformobj = new myusercontrol1();
destinationformobj.Show();
}
But it never redirect to myusercontrol1.cs page
Is this possible to redirect from Form to UserControl page?
Thanks in advance
Ok according to the comments you can do this to solve your problem with a form:
public frmUserControl : Form
{
private UserControl control;
public frmUserControl(UserControl control)
{
this.control = control;
this.Load += frmUserControl_Load;
}
public frmUserControl_Load(object sender, EventArgs e)
{
this.Controls.Add(control);
}
}
You also have to take care of a few things like the size of the new form and the position of your user control inside the form. I tried to find my old code for that but it was too old and I don't have it right now. So I hope it helps :)

C# usercontrol show

I have created one user control with some things on it, and I need to know if it's possible in my form1 click in one button and that button open my usercontrol but not inside the form1.
I want to see the usercontrol separated from the form1, so if the user want to close the usercontrol he will close it and can keep the from1, or if the user want's to minimize the form1 and keep the usercontrol in the screen.
i have tested with this
UC lauchUC = new UC(person);
lauchUC.Show();
but that don't show nothing, and also tested with this:
UC lauchUC = new UC(person);
this.Controls.Add(lauchUC);
but it appears in the form
can someone help me or telling me if it's possible show it separated from the form?
You could pass an instance of your UserControl to the constructor of the Form. In this constructor, you can add it to it's Controls. Just create a new Form and alter it's constructor.
The (container) Form:
public partial class Form1 : Form
{
public Form1(UserControl control)
{
InitializeComponent();
this.Controls.Add(control);
}
}
How to open it.
public void ButtonClick(object sender, EventArgs e)
{
var myControl = new MyUserControl();
var form = new Form1(myControl);
form.Show();
}
You can Place it in a Window and call Window.ShowDialog.
private void Button1_Click(object sender, EventArgs e)
{
Window window = new Window
{
Title = "My User Control Dialog",
Content = new UC(person)
};
window.ShowDialog();
}

Two windows display and control WPF C#

I am pretty new in WPF C#. I am held up with the following Issue:
Lets say I have a MainWindow.xaml, and have the main program logic in it.
I have a second window, called Second.xaml
I am calling Second.xaml in MainWindow.xaml.cs,
currently I am doing:
MainWindow.xaml.cs:
var wind = new Second();
wind.Show();
This successfully opens the second window, where I have a few buttons.
My motive is to trigger events in MainWindow using Second.xaml.cs
(i.e.)
in Second.xaml.cs:
....
..
MainWindow mainwindowID = new MainWindow();
....
..
.
private void nextButton_Click(object sender, RoutedEventArgs e)
{
mainwindowID.textBox.Content = "Displaying In Mainwindow";
}
When I click the Next button in Second.xaml, I want the text Box inside Mainwindow to be updated.
Though the program in running, nothing changes inside MainWindow.
Is it possible to control it like that?
I have the MainWindow displayed using a projector, and the second window on the monitor. So I trigger events inside the second window and want them to be displayed in the MainWindow.
Is there any other solution for this kind?
Update:
If the textbox is inside SecondPage.xaml and displayed inside MainWindow.xaml using a Frame, how do I call it from Second.xaml?
In the first window (MainWindow) you can invoke the second window in this way:
var wind = new Second();
wind.FirstWindow = this;
wind.Show();
while the second window can look like this:
public MainWindow FirstWindow { get; set; }
private void nextButton_Click(object sender, RoutedEventArgs e)
{
FirstWindow.textBox.Content = "Displaying In Mainwindow";
}
I would suggest using Delegates. Have a look at the link here;
Delegates
This way you can create a method in the first Window like so;
private void WaitForResponse(object sender, RoutedEventArgs e)
{
var second = new SecondWindow();
second.ReturnTextBoxText += LoadTextBoxText;
SecondWindow.ShowDialog();
}
Then in your second Window;
internal Action<string, int> ReturnTextBoxText;
private void nextButton_Click(object sender, RoutedEventArgs e)
{
ReturnTextBoxText("Displaying In Mainwindow");
}
Finally load that response in your first Window;
private void LoadSelectedCompany(string text, int passedCompanyID)
{
contactCompanyTextBox.Text = companyName;
}

set window title from listboxitem name

This is my first time coding in C# and building applications in VS2010.
My task is to build a application, that has two windows. First with ListBox with several items. The second one opens on MouseDoubleClick on any item. At that point, second window opens and the title of it should be the same as ListBoxItem Name.
I have searched for a way to do this. But had no luck what so ever.
At this point I have this in code:
...
namespace WpfApplication20
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
var newWindow = new Window1();
newWindow.Show();
}
private void seznamSporocil_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var newWindow = new Window1();
newWindow.Show();
}
}
}
At the end this should be an Email application, like Outlook or similar.
Thank you for all the help!
Whilst Freelancers answer is somewhat right, his solution assumes that the event was and will always be triggered by that one specific ListBox. You should really use the sender parameter to get a reference to the listbox that was triggering the event though, like so:
private void seznamSporocil_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var listBox = sender as ListBox;
var item = listBox.SelectedItem as ListBoxItem;
var newWindow = new Window1();
newWindow.Title = item.Content.ToString();
newWindow.Show();
}
Some error-checking would not hurt either, i.e. verify wheather the sender object really is a ListBox-Type, etc.
Try with following:
var newWindow = new Window1();
newWindow.Title = listBox.SelectedItem.toString();
newWindow.Show();
Link:
http://msdn.microsoft.com/en-us/library/system.windows.controls.window.title.aspx
Hope its helpful.

Is there a way to show a "blocking" WinForms ContextMenu?

Is there a way to show a ContextMenu and block further execution until an item has been selected? In particular, I want to get behavior similar to ShowDialog() but for a ContextMenu.
The straight forward approach doesn't work:
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("1", (s,e) => {value = 1;});
cm.Show(control, location);
since the Click callback isn't called directly from Show() but instead at some later point when the message loop processes the click event.
If you are unlucky, menu is garbage collected before the event is processed and in that case the event is just silently lost. (Meaning you can't really use local variables for ContextMenus in this way.)
This seems to work, but feels "unclean":
using (ContextMenu cm = new ContextMenu()) {
cm.MenuItems.Add("1", (s,e) => {value = 1;});
cm.Show(control, location);
Application.DoEvents();
}
Is there a better way?
Sorry for the first answer. Here is what I've tried. I made another Form where I put the context menu and a timer.Form2 is displayed as modal from Form1 then the timer shows the context menu on Form2.
Note that Form 2 has some properties set : to not be visible in task bar, not have boarders and the size should be equal with the size of the context menu.
Hope this helps.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Form2 ctxForm = new Form2();
ctxForm.Location = this.PointToScreen(e.Location);
ctxForm.Size = new Size(0, 0);
ctxForm.ShowDialog();
}
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void timer1_Tick(object sender, EventArgs e)
{
//show menu once
contextMenuStrip1.Show(this, PointToClient(Location));
contextMenuStrip1.Focus();
timer1.Enabled = false;
}
private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
this.Close();
}
}
You can easily prevent the garbage collection of the ContextMenu whilst it is still being shown.
The problem is that you are using a lambda as the event handler for the menu item. This is an
anonymous method and so not itself attached to any object instance that would cause the ContextMenu to be referenced and so kept alive. Add a method to the enclosing object and then create a standard EventHandler. That way the existence of the enclosing instance will keep the ContextMenu alive. Not as concise and very C# 1.0 but it will solve the problem.
Just wait for the menu to not be visiable.
ContextMenu cm = new ContextMenu();
cm.MenuItems.Add("1", (s,e) => {value = 1;});
cm.Show(control, location);
while (cm.Visible == true) Application.DoEvents();

Categories

Resources