Get string from dll in second window - c#

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

Related

Putting information from one window to a datagrid in another window without the use of database

So my MainWindow with a datagrid is suppose to have an "add client" button and it will bring you to a new window and that window will ask information about the soon to be client and if I click the "save" button it should close the window and put the information to the datagrid on my MainWindow and I tried many things but I can't seem to get it right and I'm using wpf
public partial class GetInfo : Window
{
public GetInfo()
{
InitializeComponent();
}
public class Client
{
public string name { get; set; }
public string address { get; set; }
}
private void SaveBT_Click(object sender, System.EventArgs e)
{
MainWindow main = new MainWindow();
Client addClient = new Client();
addClient.name = NameTB.Text;
addClient.address = AddressTB.Text;
main.DataGridXAML.Items.Add(addClient);
this.Close();
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
private void LogOut_Click(object sender, RoutedEventArgs e)
{
LoginScreen logout = new LoginScreen();
logout.Show();
Close();
}
private void AddClient_Click(object sender, RoutedEventArgs e)
{
GetInfo infoget = new GetInfo();
infoget.Show();
}
}
Seems like your GetInfo is working like a dialogue.
You may store the info in GetInfo and get info after user choose closed.
In your MainWindow
private void AddClient_Click(object sender, RoutedEventArgs e)
{
GetInfo infoget = new GetInfo();
infoget.ShowDialog();
if( get.DialogResult != true ) {
return;
}
main.DataGridXAML.Items.Add(addClient);
}
In GetInfo
public partial class GetInfo : Window
{
public GetInfo()
{
InitializeComponent();
}
public class Client
{
public string name { get; set; }
public string address { get; set; }
}
// Save info in window as a property.
public Client Info { get; set; }
private void SaveBT_Click(object sender, System.EventArgs e)
{
Client addClient = new Client();
addClient.name = NameTB.Text;
addClient.address = AddressTB.Text;
Info = addClient;
DialogueResult = true;
}
}
What's wrong with your original code? Look at comments below.
private void SaveBT_Click(object sender, System.EventArgs e)
{
// This is the problem, this main is NOT your actual instance of MainWindow which calls this GetInfo.
MainWindow main = new MainWindow();
Client addClient = new Client();
addClient.name = NameTB.Text;
addClient.address = AddressTB.Text;
main.DataGridXAML.Items.Add(addClient);
this.Close();
// After this command, main is not getting used anymore, so you get nothing in your actual MainWindow
}

Creating multiple instances of class

I have this code:
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Human
{
public string Name;
public Human(string name)
{
Name = name;
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Jane";
}
private void AddNewHuman_Click(object sender, EventArgs e)
{
Human h1 = new Human(textBox1.Text);
}
}
}
Is there a way, how to create a new instance of Human whenever I click the Button(AddNewHuman_Click)?
After clicking few times on the button, there will still be only one Human h1, right?
You will have to create list of object to store multiple object of human class.
I do changed here for you. I hope it will work for you.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Human
{
public string Name;
public Human(string name)
{
Name = name;
}
}
List<Human> objHumanList;
private void Form1_Load(object sender, EventArgs e)
{
objHumanList=new List<Human>();
textBox1.Text = "Jane";
}
private void AddNewHuman_Click(object sender, EventArgs e)
{
Human h1 = new Human(textBox1.Text);
objHumanList.add(h1);
/** Or
objHumanList.add (new Human(textBox1.Text))
**/
}
}
}
You can create a list of Humans and keep on adding a new Human to the list whenever the button is clicked.

Return List value from another window

I have two windows, windowA that has a button to open windowB, and windowB has a button to close itself and also return List value. I tried this code, but the value keep null. windowB has RadGridView control, i want to get the selectedItem from it and add it on a list.
public class WindowA : Window
{
...
private void button_Click(object sender, RoutedEventArgs e)
{
WindowB winB = new WindowB();
if (winB.ShowDialog() == false)
{
listClass lc = winB.SelectedItemButton;
...
}
}
}
public class WindowB : Window
{
...
public listClass SelectedItemButton
{
get { return selectedItem; }
set
{
selectedItem = ((listClass)AGridView.SelectedItem);
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
the results are a listClass, but has no value inside. Why? and how can i make selectedItem = ((listClass)AGridView.SelectedItem); this line works to another window?
An example for you:
public partial class MainWindow : Window
{
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 dlg = new Window1();
if(dlg.ShowDialog()??false)
{
MessageBox.Show(dlg.S);
}
}
}
// Dialog
public partial class Window1 : Window
{
public string S
{
get
{
return this.txt1.Text;
}
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
}
you should create your listClass variable in Window1 and while you are opening the Window2 you should pass this variable as a parameter. Here is my demo:
First window:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TestClass testClass = new TestClass();
testClass.Test = "Initial";
Second second = new Second(testClass);
second.ShowDialog();
label.Content = testClass.Test; // It prints "Changed"
}
}
Second window:
public partial class Second : Window
{
TestClass testClass;
public Second(TestClass sent)
{
InitializeComponent();
testClass = sent;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
testClass.Test = "Changed"; // Change the value
}
}
My testClass (listClass):
public class TestClass
{
public string Test { get; set; }
}

Call to a Superclass function which alters a component (label)

If a Superclass has a function A() which changes a Label to "Hello World". How can I get a subclass to call A() with the same result? As of now, I get no compile error, but the text won't change!
Example code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
FunctionA("Hello");
}
public void FunctionA(string s)
{
label1.Text = s;
}
private void button2_Click(object sender, EventArgs e)
{
Test t = new Test();
}
}
class Test : Form1
{
public Test()
{
FunctionA("World");
}
}
Both the Forms must be having their own Label control to display messages. You might be using one Label to show the message which is not part of displaying Form.
I am not sure what are to trying to achieve but why don't you just pass the Label control to FunctionA to modify the message this way:
public void FunctionA(ref Label lbl, string s)
{
lbl.Text = s;
}
ADDED: You can do it this way:
First creating the instance of FormA.
static void Main()
{
//...
FormA frmA = new FormA();
Application.Run(frmA);
}
Passing the instance of FormA to FormB by exposing a parameterized constructor for any manipulation in FormA from within FormB.
FormB frmB = new FormB(frmA);
//...
public partial class FormB : Form
{
public FormB()
{
InitializeComponent();
}
//parameterized constructor
public FormB(FormA obj)
{
FormA = obj;
InitializeComponent();
}
public FormA FormA { get; set; }
}
Instantiate your forms before running the main form. Assign the form1 reference to form2
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 mainForm = new Form1();
new Form2() { Form1 = mainForm }.Show();
Application.Run(mainForm);
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form1 Form1 { get; set; }
private void button1_Click(object sender, EventArgs e)
{
this.Form1.Update("World");
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Update("Hello");
}
public void Update(string text)
{
this.label1.Text = text;
}
}

Can I capture events from a WPF combobox in a winform?

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.

Categories