set window title from listboxitem name - c#

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.

Related

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;
}

How to add items to / modify the forms default contextmenu

Solution found here: How can I customize the system menu of a Windows Form?. Anyway, thanks for your help :)
I want to add a new item to the default contextmenu of a form, which appears when right clicked on the top bar (where the minimize box and maximize box are).
I'd also prefer to to it programatically (not in the designer)
I've tried this
public Form1()
{
InitializeComponent();
this.ContextMenu.Add(new MenuItem("Test")); //->NullReferenceException
this.ContextMenu = new ContextMenu(/*..*/); //-> Not what I want
}
which leads to a NullReferenceException.
if I set this.ContextMenu it affects only the contextmenu that appears when you right click into the form, which I don't need in this case.
I hope someone can help me out ^^ I know it needs to be possible somehow since I've seen it in a lot of Programs already
Here's one example by Microsoft:
public partial class TextBoxContextMenuDemo : Form
{
ContextMenu mnuContextDefault;
ContextMenu mnuContextAlt;
MenuItem mnuItmAltMenuTest;
public TextBoxContextMenuDemo()
{
InitializeComponent();
InitializeAltContextMenu();
}
private void InitializeAltContextMenu()
{
mnuContextDefault = new ContextMenu();
mnuContextDefault = this.TextBox1.ContextMenu;
mnuItmAltMenuTest = new MenuItem();
mnuItmAltMenuTest.Index = -1;
mnuItmAltMenuTest.Text = "Test Menu Item";
mnuItmAltMenuTest.Click += new System.EventHandler(this.mnuItmAltMenuTest_Click);
mnuContextAlt = new ContextMenu();
mnuContextAlt.MenuItems.Add(mnuItmAltMenuTest);
}
private void TextBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if ((Control.ModifierKeys == Keys.Control))
{
this.TextBox1.ContextMenu = mnuContextAlt;
TextBox1.ContextMenu.Show(TextBox1, new Point(e.X, e.Y));
}
else
{
this.TextBox1.ContextMenu = mnuContextDefault;
}
}
}
private void mnuItmAltMenuTest_Click(object sender, System.EventArgs e)
{
MessageBox.Show("You clicked the alternate test menu item!");
}
}
may be these help
http://www.c-sharpcorner.com/UploadFile/deepak.sharma00/how-to-customize-default-contextmenu-of-a-textbox-control-in/
Is it possible to obtain and modify standard system context menu for textbox?

How to effect change from child to parent window

This is probably a fairly simple question, but I have had no luck researching it thus far. I have a child window that has a yes and no button on it. When I click no I would like a check box to become unchecked in the parent window (which is the main window of my program).
Is there anyway that I could do something like?:
//No Button
private void No_Click(object sender, RoutedEventArgs e)
{
NameOfParent.checkBox.Checked = false;
}
I've seen this question but do not think that it exactly addresses my problem.
What is the correct way to go about this?
I've been using this to open my other windows:
Parent Window - Current code:
//Open new window
private void checkBox5_Checked(object sender, RoutedEventArgs e)
{
var newWindow = new ChildWindow();
newWindow.button_no.Click += buttonNo_click;
newWindow.Show();
}
//Unchecks Checkbox 5
private void buttonNo_click(object sender, RoutedEventArgs e)
{
checkBox5.IsChecked = false;
}
Opening the child window from the parent window:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var window = new ChildWindow();
window.buttonNo.Click += buttonNo_Click;
window.Show();
}
In the code of the parent window, include this click event for the child window's no button:
private void buttonNo_Click(object sender, RoutedEventArgs e)
{
//code to uncheck the checkbox goes here
}
This turned out even simpler than the solution for Windows Forms that I've been using...and it appears to work in Windows Forms as well.
On construction of the child window, simply add a parameter that is of the type of the parent window.
Then when you are constructing your child window in the parent window code do the following (In The Parent Window Class):
ChildWindow child = new ChildWindow(this, other_param, other_param2.....);
ChildWindow.Show();
The keyword this will pass the parent window into the ChildWindow
Then you can store it into a class variable and access properties of it.
Child class constructor and parentWindow variable:
private ParentWindowType m_parentWindow = new ParentWindowType();
public ChildWindow(ParentWindowType parent, int other_param, string other_param2....)
{
m_parentWindow = parent;
}
Then in other child class methods, such as your button click handler you can access properties from the parent window:
public void ButtonClickHandler(....)
{
m_parentWindow.checkBox1.Enabled = true;
m_parentWindow.checkBox1.Checked = true;
}
This way you can store the parent window within the child window, and always have access to it.
Its nice and clean and simple this way. Ensure to make the parent window variable private in the child window class.
If made public, it would allow you to do funny things like
parentWindow.childWindow.m_parentWindow.childWindow etc......

Silverlight4.0: Set childwindow field in MainPage not work

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

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