how to call a combobox from another class C# - c#

I have 2 forms, let's call them form1 and form2. Inside form1 i have created a method that connects to an access database, and also inserts the values from the database into a combobox. My question is : how do i call my method which is inside my form1, for a combobox inside form2? The error that i get says The name "MyComboBoxName" does not exist in the current context.
I have tried to use inheritence between my 2 classes, but then i get 2 of the same comboboxes.

The easiest solution is to define your method statically in the first form:
public partial class Form1 : Form
{
public static void CustomMethod()
{
//Your codes
}
public Form1()
{
InitializeComponent();
}
}
Then call it from the second form:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Form1.CustomMethod();
}
}

Related

Handling event from main form in UserControl

I am trying to execute code in a UserControl when an even from the main form is raised.
The Form code:
public partial class mainForm : Form {
...
public event EventHandler listBoxIndexChanged;
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
listBoxIndexChanged?.Invoke(sender, e);
}
}
Important to mention here is that the actual form's Name is also mainForm, just like the classname.
The UserControl code:
public partial class userControl1 : UserControl {
public userControl1() {
InitializeComponent();
mainForm.listBoxIndexChanged += mainForm_listBox1_IndexChanged;
}
private void mainForm_listBox1_IndexChanged(object sender, EventArgs e) {
// my code
}
}
This code is throwing the error An object reference is required for the non-static field, method, or property 'mainForm.listBoxIndexChanged'. I'm sure it's something obvious, but what could I be doing wrong?
WinForms .NET Framework 4.8, VS 2019.
The way this runs is that MainForm is created first and then creates an instance of your UserControl inside the MainForm. So, MainForm knows about UserControl but UserControl does not know about MainForm. So when you tell UserControl to use MainForm it doesn't know what MainForm is (no instance). Instead you'll want the MainForm to trigger a method inside the UserControl and pass it the needed information.
In your user control create a method that the MainForm can call:
public class UserControl1
{
public void doSomethingWhenSelectedIndexChanges(int selectedIndex){
// do stuff inside user control...
}
}
Then in your MainForm call the UserControl method.
public class MainForm
{
private void ListBox1_SelectedIndexChanged(object sender, eventargs e)
{
UserControl1.doSomethingWhenSelectedIndexChanges(ListBox1.SelectedIndex);
}
}
This way the MainForm is telling the UserControl that the ListBox selected index has changed and is passing the selected index to the UserControl.

Why I can't Invoke from a different class?

My problem is, I can't invoke a message into the TextBox and I can't understand why.
There is a main class and a second class, both with call to the other one.
Where is my error?
using System;
using System.Windows.Forms;
namespace Class_Test___Invoke
{
public partial class MAINFORM : Form
{
public MAINFORM()
{
InitializeComponent();
_INVOKER = this;
}
private MAINFORM _INVOKER;
private static CLASS _CLASS = new CLASS();
private void button1_Click(object sender, EventArgs e)
{
_CLASS._MESSENGER();
}
public void _LOGGING(string _MESSAGE)
{
if (InvokeRequired)
{
_INVOKER.Invoke(new Action<string>(_LOGGING), new object[] { _MESSAGE });
textBox_ausgabe.AppendText(_MESSAGE);
return;
}
else textBox_ausgabe.AppendText(_MESSAGE);
}
}
}
namespace Class_Test___Invoke
{
class CLASS
{
private MAINFORM _MAINFORM = new MAINFORM();
public void _MESSENGER()
{
_MAINFORM._LOGGING("Test");
}
}
}
You are assuming that the _MAINFORM you create in the CLASS constructor is the same instance as the form where the button was clicked, which is not the case. You have a chicken-and-egg problem. Your form creates a CLASS, and the CLASS creates a form. So now you have two different forms. (or two different CLASS instances since you don't show how the first form or CLASS is created)
You need to "connect" the form and the class, either by passing the form to the constructor as a parameter or by some other means.
Finally, I would encourage you to do some research on best practices for class and member names. It's a bit disconcerting for a seasoned C# developer to see names in all caps and prefaced by underscores.

how to use listview data from another class?

im working on vb(or c#).
For example, i have a three class with different designs.
Class 1 of the main design has a function that receives data from the equipment, and this function returns the data as an array.
Class 2 of the second design invokes a function in class 1 to store and display the data in the listview.
I want to output and use the data of the listview of the second designer's class in the third designer.
To summarize, class 1 gets data, class 2 calls class 1 and outputs data to listview. Class 3 tries to use class 2's listview data.
Did my intentions pass exactly? I'm sorry I do not English well.
Thank you for your attention! :)
Your class2 should hold data in raw form (say list of strings) it should use this list to display data on listview and also expose this data to class3
I have created a sample program showing how to do SO!
lets take three forms Form1, Form2, and Form3
Form1 (class1) : this will hold the actual data, which we need to display on listview of Form2 and also we need to pass this information to Form3 via Form2
public partial class Form1 : Form
{
//Data which need to be shown in list view
//and also need to pass to form3 (class3)
public List<string> Data = new List<string>();
public Form1()
{
InitializeComponent();
Data.Add("item1");
Data.Add("item2");
}
private void btnForm2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
}
Form2 (class 2) : this class will hold data of Form1 as raw form (here list of string) so it doesn't have to expose entire listview to Form3 .
you see, form3 just need data, not control on which it is displayed, so better to keep control (listview) not accessible by external codes.
public partial class Form2 : Form
{
Form1 m_form1;
List<string> m_dataOfListview;
//exposing row data publically so form3 can use it
//note that, it is read only, so form3 can only read it. Not modify
public List<string> DataOfListView
{
get { return m_dataOfListview; }
}
public Form2(Form1 form1)
{
InitializeComponent();
m_form1 = form1;
}
private void btnFetchData_Click(object sender, EventArgs e)
{
m_dataOfListview = m_form1.Data;
foreach(string data in m_dataOfListview)
listView1.Items.Add(data);
}
private void btnShowForm3_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3(this);
form3.Show();
}
}
and at last Form3 (class 3)
public partial class Form3 : Form
{
Form2 m_form2;
public Form3(Form2 form2)
{
InitializeComponent();
m_form2 = form2;
//here in this class you can get value of data of listview
List<string> dataOfListView = form2.DataOfListView;
}
}

Unable to pass a text to another forms listbox

I am trying to pass a value from textbox1 of my form3 to the ListBox of my form1.
Here is the code for form3 :
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1(textBox1.Text);
f1.Show();
}
And here it is what is wrote in form1 :
public partial class Form1 : Form
{
public Form1(string something)
{
InitializeComponent();
listBox1.Items.Add(something);
}
The error is:
Form1' does not contain a constructor that takes 0 arguments.
Thank you for your help in advance!
You should take a look at the line where this error comes from (). I would wildly guess that there is a line in your code that uses a parameterless constructor like :
Form1 foo = new Form1();
or even if it is the starting form:
Application.Run(new Form1());
You should overload the constructor and not simply change it, since it is auto-generated it might highly probably be that it is already used in this form somewhere. Just add a second constructor:
public partial class Form1 : Form
{
public Form()
{
InitializeComponent();
}
public Form(string something)
{
InitializeComponent();
listBox.Items.Add(something);
}
}
Edit:
trying to pass a value from textbox1 of my form3 to the listbox of my form1
This is a slightly different problem then your error suggested in the first place. A different approach would be advisable. The constructor is useless, because it will create a different instance/object which is not the same that you see on the screen! In other words, all Elements will loose their values!
One of many solutions can be to create a method which would add items to the ListBox in the class Form1:
public void AddItemToListBox(string s)
{
listBox.Items.Add(something);
}
and pass the instance of the current window Form1 via Form2 to Form3. Have a variable in each class(Form2 and Form3) of type Form1
public Form1 temp_form1;
and pass the instance of the starting window Form1 to the variable temp_form1 when you call Form2 in the class Form1:
Form2 form2 = new Form2();
form2.temp_form1 = this; // "this" stands for the instance of the current object
and the same hold for Form3 when you call it in the class Form2:
Form3 form3 = new Form3();
form3.temp_form1 = this.temp_form1;
At the end just call the method to update the listbox in the class Form3:
temp_form1.AddItemToListBox("yourstring");
Do not delete the default constructor of your form, leave it there and add another one with your custom parameters under it instead.
public Form1()
{
//Default constructor
InitializeComponent();
}
public Form1(string something)
{
//Your custom constructor
InitializeComponent();
listBox1.Items.Add(something);
}

How Multiple Forms can Call Same Method on Their Load Event

I have around 20 forms in my Project of C#. I have created a method in a Class. I want all the 20 forms to call that method in their Load event. I can put the Call statement in the Load Event of each form one by one. However, in future the forms may increase. Also, it may be possible that some form gets missed to write that Call statement in its Load event mistakenly.
Is their any logic, that the specific method can handle the load events of all the forms of the Project? We can get the list of all the forms of the project by using System.Reflection. So, is it possible to attach the load events of all these forms to the same specific method of the class?
You can create your custom form and override Onload (or any event)
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace test
{
public class MyForm : Form
{
protected override void OnLoad(EventArgs e)
{
MessageBox.Show("test"); //call your method(s)
base.OnLoad(e);
}
}
}
and your new form
namespace CustomFormProject
{
public partial class Form1 : MyForm
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//MessageBox will be shown automatically from your base (MyForm) class
//other codes
}
}
}
You can do something like this:
public partial class Form2 : MainForm
public partial class Form3 : MainForm
...
...
...
public partial class Form19 : MainForm
public partial class Form20 : MainForm
Inherit the MainForm in your other Forms and call the method in their Form_Load event.
The MainForm is the class in which you've created the method, further which you want to access it in the other forms.
Suppose, the example below is the Method in your MainForm.
public partial class MainForm : Form
{
public Form1()
{
InitializeComponent();
}
public void Msg()
{
MessageBox.Show("Hello");
}
}
Call the method Msg() on the Form2_Load event by first inheriting from the MainForm class:
public partial class Form2 : MainForm
^^^^^^^^
Now, call this method:
private void Form2_Load(object sender, EventArgs e)
{
Msg();
}
Similarly, you do the same thing with the rest of your Forms.

Categories

Resources