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);
Related
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;
}
}
i'm making a program where it will count each answer correct on each window and display the counter as a result. I've already figured out how to count the answer and display it on one page.
If anyone could point me in a direction on how to keep that count throughout multiple pages until the last one? Not asking for people to write me code out just don't have a lot of knowledge of carrying data through multiple windows.
Any help/links are appreciated.
Thanks.
EDIT: I took your advice and assigned it to a static variable, this code updates the lblScore to 1, but when it navigates to the last page, it doesn't show that value in the label. Any advice?
public partial class Question1 : Window
{
public Question1()
{
InitializeComponent();
}
private void Question1_Load(object sender, EventArgs e)
{
lblScore.Content = MyGlobals.Score.ToString();
}
private void btnNext1_Click(object sender, RoutedEventArgs e)
{
if (textBox.Text == "1111")
{
MyGlobals.Score = MyGlobals.Score + 1;
lblScore.Content = MyGlobals.Score.ToString();
MessageBox.Show("Noice");
}
new Question3().Show();
this.Hide();
}
}
}
public static class MyGlobals
{
public static int Score;
}
/*Question3 Window*/
namespace Maths_Quiz
{
/// <summary>
/// Interaction logic for Question3.xaml
/// </summary>
public partial class Question3 : Window
{
public Question3()
{
InitializeComponent();
}
private void Question3_Load(object sender, EventArgs e)
{
lblScore.Content = MyGlobals.Score.ToString();
}
private void btnReturn_Click(object sender, RoutedEventArgs e)
{
new MainWindow().Show();
this.Hide();
}
}
}
Create an overload for the constructor and pass in the data as a parameter or store the data in properties settings
I hope that my question is relevant to be answered because I'm a newbie.
For example, I have two coding named with Class1.cs and Form1.cs. Basically, Class1.cs is the program where the process of image filtering is occured, meanwhile in Form1 is the program where I allow it to load an image from a file.
Is it possible for me to access Class1 from Form1 right after I click on the button to process it?
Class1.cs
public class Class1
{
public static class Class1Program
{
// My program for image filtering is starting here
}
}
Form1.cs
public partial class Form1 : Form
{
public Form1() => InitializeComponent();
private void LoadImageButton_Click(object sender, EventArgs e)
{
// My program here is to open up a file image right after I click on this button
}
private void ResultButton_Click(object sender, EventArgs e)
{
// Here is the part where I don't know how to access the program from "Class1.cs".
// I'm expecting the image that I've load before will be able to filter right after I clicked on this button
}
}
I'm wondering if there is a need to add or edit some program on the "Program.cs".
I'm hoping that my question can be answered. Thank you so much for your time.
Let me add one property and a method to the static class for make it understandable. Let the Class1Program looks like the following:
public static class Class1Program
{
public static int MyProperty{get; set;} // is a static Property
public static void DoSomething() // is a static method for performing the action
{
//my program for image filtering is starting at here
}
}
Now you can access the method and property inside the Form1 like this:
private void ResultButton_Click(object sender, EventArgs e)
{
Class1.Class1Program.MyProperty = 20; // assign 20 to MyProperty
int myint = Class1.Class1Program.MyProperty; // access value from MyProperty
Class1.Class1Program.DoSomething();// calling the method to perform the action
}
You can do one of these two things:
1.You can access data in Class1 by using static variables:
namespace MyProgram
{
class Class1
{
public static int Class1Variable;
}
}
namespace MyProgram
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void LoadImageButton_Click(object sender, EventArgs e)
{
//Load image logic
}
private void ResultButton_Click(object sender, EventArgs e)
{
Class1.Class1Variable = 1;
}
}
}
2.You can create an instance of Class1 inside Form1:
namespace MyProgram
{
class Class1
{
public int Class1Variable;
public Class1()
{
}
}
}
namespace MyProgram
{
public partial class Form1 : Form
{
public Class1 c1;
public Form1()
{
InitializeComponent();
c1 = new Class1();
}
private void LoadImageButton_Click(object sender, EventArgs e)
{
//Load image logic
}
private void ResultButton_Click(object sender, EventArgs e)
{
c1.Class1Variable = 1;
}
}
}
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);
}
}
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.