Can I capture events from a WPF combobox in a winform? - c#

I had implemented a wpf combobox into a winform and now have to somehow make it call a function or an event in my winform when the box is changed, as well as be able to change the value from the winform.
main:
namespace main
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ElementHost elhost = new ElementHost();
elhost.Size = new Size(174, 24);
elhost.Location = new Point(93,60);
MyWPFControl wpfctl = new MyWPFControl();
elhost.Child = wpfctl;
this.Controls.Add(elhost);
elhost.BringToFront();
}
public void status_change(int val)
{
MessageBox.Show("Box value has changed to:" + Convert.ToString(val) );
}
and wpf:
namespace main
{
/// <summary>
/// Interaction logic for MyWPFControl.xaml
/// </summary>
public partial class MyWPFControl : UserControl
{
public MyWPFControl()
{
InitializeComponent();
}
private void statComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
main.status_change(statComboBox.SelectedIndex);
/// says:
/// Error 1 The type or namespace name 'status_change' does not exist in the namespace 'main' (are you missing an assembly reference?) C:\Users\Robert\documents\visual studio 2010\Projects\XMPP\main\wpf_combo_status.xaml.cs 31 18 main
}
}
}
any idea what I might be doing wrong? Thanks!

Expose an event in MyWPFControl like:
public SelectionChangedEventArgs:EventArgs
{
public int SelectedIndex {get; private set;}
public SelectionChangedEventArgs (int selIdx)
{
this.SelectedIndex = selIdx;
}
}
public partial class MyWPFControl : UserControl
{
public event EventHandler<SelectionChangedEventArgs> SelectionChanged;
public MyWPFControl()
{
InitializeComponent();
}
private void statComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
OnSelectionChanged(statComboBox.SelectedIndex);
}
private void OnSelectionChanged(int selIdx)
{
if (SelectionChange != null)
SelectionChanged(this, new SelectionChangedEventArgs(selIdx));
}
}
Then subscribe it in your form, like:
public Form1()
{
InitializeComponent();
ElementHost elhost = new ElementHost();
elhost.Size = new Size(174, 24);
elhost.Location = new Point(93,60);
MyWPFControl wpfctl = new MyWPFControl();
elhost.Child = wpfctl;
this.Controls.Add(elhost);
elhost.BringToFront();
wpfctl.SelectionChanged += myWPFControls_SelectionChanged;
}
private void myWPFControls_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
status_change(e.SelectedIndex);
}
P.S.:
I can't test it, so there could be some errors, but just to give you the idea... ;)

It's hard to say without more context, but it would seem that the code you want is probably:
private void statComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
main.status_change(statComboBox.SelectedIndex);
}
Assuming main is the instance of Form1 on which you want to call status_change.

Related

C# Passing data Between windows(WPF)

Recently picked up C# in university, trying to work out how to pass the variable "name" in MainWindow.xaml to ThirdWindow.xaml?
The below code is for the main window where the data is assigned to the variable "name"
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void NameBox_TextChanged(object sender, TextChangedEventArgs e)
{
string name = NameBox.Text;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondWindow newWin = new SecondWindow();
newWin.Show();
this.Close();
}
}
The below code is for the third window
public partial class ThirdWindow : Window
{
public ThirdWindow()
{
InitializeComponent();
}
public void LstThanks_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LstThanks.Items.Add(name);
}
}
You can simply pass that string name variable in the constructor as an argument to the ThirdWindow on Button_Click event.
private void Button_Click(object sender, RoutedEventArgs e)
{
var name = "your name";
var newWin = new ThirdWindow(name);
newWin.Show();
this.Close();
}
That string text will be available in the constructor of ThirdWindow.
public partial class ThirdWindow: Window
{
public ThirdWindow(string name)
{
InitializeComponent();
}
public void LstThanks_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
LstThanks.Items.Add(name);
}
}
You can pass the variable through the constructor of the new window
var win = new ThirdWindow(name);
public ThirdWindow(string name)
{
InitializeComponent();
}
Another method is to pass it through an event message. This will require you to write a new message and add an event listener to your constructor in the ThirdWindow class. If you google this there are a variety of examples out there on how to do such a thing.
Here you are defining a local variable name. This variable is visible only inside the {} block. So we cannot use it anywhere else.
public void NameBox_TextChanged(object sender, TextChangedEventArgs e)
{
string name = NameBox.Text;
}
You could add a new string property into second window and pass value through that to all the way into third form.
So, add new property into your two windows (SecondWindow, ThirdWindow)
public string Name { get; set; }
These properties are holding the data for whole their lifetime (until they are closed).
Remove NameBox_TextChanged event handling, because we don't need it.
Add property setting inside your buttons click event
private void Button_Click(object sender, RoutedEventArgs e)
{
SecondWindow newWin = new SecondWindow();
newWin.Name = NameBox.Text; //Store value into SecondWindow variable
newWin.Show();
this.Close();
}
Now when SecondWindow is visible (Show is called), you have name value available in Name variable and you should be able to copy this behavior for ThirdWindow.
If the ThirdWindow window is dependent on the name value then you can pass it through the constructor:
public partial class ThirdWindow : Window
{
public string Name { get; set; }
public ThirdWindow(string name)
{
InitializeComponent();
Name = name;
}
}
or if not then make a method on the ThridWindow to set the name:
public partial class ThirdWindow : Window
{
public string Name { get; set; }
public void SetName(string name)
{
Name = name;
}
}

wpf need to transfer info from one textbox to another

hi guys im trying to create a button which can take my information from one textbox to another but i the informationer is in Contactformula.xaml and i need it over to Mainwindow.xaml hope u understand what i mean here is the code...
Mainwindow.xaml.cs
namespace debug
{
public partial class MainWindow : Window
{
public ContactFormula CF = new ContactFormula();
public object Frame { get; private set; }
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ContactFormula win2 = new ContactFormula();
win2.Show();
}
ContactFormula.xaml.cs
namespace debug
{
/// <summary>
/// Interaction logic for ContactFormula.xaml
/// </summary>
public partial class ContactFormula : Window
{
public ContactFormula()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
}
and here is a screenshot of the program so maybe u understand me better
Try making the list of contacts a static property in a static class, that way you can get or add contacts from anywhere in the program.
public static class Session
{
public static List<Contact> Contacts = new List<Contact>();
}
you can add contacts from any page using:
Session.Contacts.Add(contact);

Get string from dll in second window

I have a problem read string from a dll file. Below are some listings. I don't know why in window1 I get empty string. In MainWindow I can read from dll file. I want to read this string from dll in window2, window3 ... What to do?
Dll file,
namespace stringTest
{
public class DllstringTest
{
string string1;
public string testString
{
get
{
return this.string1;
}
set
{
this.string1 = value;
}
}
}
}
MainWindow application,
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DllstringTest st = new DllstringTest();
st.testString = "5";
Window1 w1 = new Window1();
w1.Show();
}
}
Window1,
namespace stringTest_app
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DllstringTest st1 = new DllstringTest();
MessageBox.Show(st1.testString);
}
}
}
You should use static variables. The problem you are facing at the moment is that you have two different class instances.
The solution would be to create a single static DllstringTest variable and use it across your application.
Inject st into window1 via the constructor:
MainWindow application
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
DllstringTest st = new DllstringTest();
st.testString = "5";
Window1 w1 = new Window1(st);
w1.Show();
}
}
}
Window1:
namespace stringTest_app
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private DllstringTest st;
public Window1(DllstringTest st)
{
this.st = st;
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//DllstringTest st1 = new DllstringTest();
MessageBox.Show(this.st.testString);
}
}
}
The problem is that you are using separate instances of your DllstringTest. You cannot set a value in one instance and have it magically set all future instances. (Well technically you could but it isn't what you want to do, and it wouldn't be magic either)
You could use a static class is you only ever want a single instance:
public static class DllstringTest
{
public static string testString { get;set; }
}
which you can access like so:
DllstringTest.testString
OR (probably a better approach) you could pass your instance from your main form to your other forms:
//IN MainWindow
private void button1_Click(object sender, RoutedEventArgs e)
{
DllstringTest st = new DllstringTest();
st.testString = "5";
Window1 w1 = new Window1();
w1.DllInstance = st;
w1.Show();
}
//IN Window1
public partial class Window1 : Window
{
public DllstringTest DllInstance { get;set; }
private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(DllInstance.testString);
}
}

C# Progress Bar based on event and delegate not working

I need help in troubleshooting my code. I have 3 classes. Class 1 is a WinForm with Progressbar on it. Class 2 is where the event is fired. Class 3 is the EventArg for the progress. The program compiles with out any errors, but when I click the button, the progress bar does not move!.
namespace WindowsFormsApplication1
{
class Class1
{
//Declaring a delegate
public delegate void StatusUpdateHandler(object sender, ProgressEventArgs e);
//Declaraing an event
public event StatusUpdateHandler OnUpdateStatus;
public int recno;
public void Func()
{
//time consuming code
for (recno = 0; recno <= 100; recno++)
{
UpdateStatus(recno);
}
}
public void UpdateStatus(int recno)
{
// Make sure someone is listening to event
if (OnUpdateStatus == null) return; <--------------OnUpdateStatus is always null not sure why?
ProgressEventArgs args = new ProgressEventArgs(recno);
OnUpdateStatus(this, args);
}
}
}
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Class1 testClass;
public Form1()
{
InitializeComponent();
testClass = new Class1();
testClass.OnUpdateStatus += new Class1.StatusUpdateHandler(UpdateStatus);
}
public void button1_Click(object sender, EventArgs e)
{
Class1 c = new Class1();
c.Func();
}
public void UpdateStatus(object sender, ProgressEventArgs e)
{
progressBar1.Minimum = 0;
progressBar1.Maximum = 100;
progressBar1.Value = e.Recno;
}
}
}
namespace WindowsFormsApplication1
{
public class ProgressEventArgs : EventArgs
{
public int Recno { get; private set; }
public ProgressEventArgs(int recno)
{
Recno = recno;
}
}
}
You're using two different objects of Class1.
In button click handler, the object c is not the same as member object testClass. Use testClass in place of c and it shud work
public void button1_Click(object sender, EventArgs e)
{
testClass.Func();
}
You never added an event handler to c's event.
You did add a handler to testClass' event, but testClass is never used.

Winforms user controls custom events

Is there a way to give a User Control custom events, and invoke the event on a event within the user control. (I'm not sure if invoke is the correct term)
public partial class Sample: UserControl
{
public Sample()
{
InitializeComponent();
}
private void TextBox_Validated(object sender, EventArgs e)
{
// invoke UserControl event here
}
}
And the MainForm:
public partial class MainForm : Form
{
private Sample sampleUserControl = new Sample();
public MainForm()
{
this.InitializeComponent();
sampleUserControl.Click += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
// do stuff
}
}
Aside from the example that Steve posted, there is also syntax available which can simply pass the event through. It is similar to creating a property:
class MyUserControl : UserControl
{
public event EventHandler TextBoxValidated
{
add { textBox1.Validated += value; }
remove { textBox1.Validated -= value; }
}
}
I believe what you want is something like this:
public partial class Sample: UserControl
{
public event EventHandler TextboxValidated;
public Sample()
{
InitializeComponent();
}
private void TextBox_Validated(object sender, EventArgs e)
{
// invoke UserControl event here
if (this.TextboxValidated != null) this.TextboxValidated(sender, e);
}
}
And then on your form:
public partial class MainForm : Form
{
private Sample sampleUserControl = new Sample();
public MainForm()
{
this.InitializeComponent();
sampleUserControl.TextboxValidated += new EventHandler(this.CustomEvent_Handler);
}
private void CustomEvent_Handler(object sender, EventArgs e)
{
// do stuff
}
}

Categories