How to access selected tab tabcontrol from another component class? - c#

I am trying to access(get) the selected tab from tabcontrol from another component class. I want see what tab is pressed every time someone clicks on a different one.
I have a component class which is embedded into another class which has the tabcontrol in it.
Thanks in Advance

If I understand you correctly, add an event handler for the tab control's TabIndexChanged event in your other component class.

Ex: you can use like this or you can achieve using property also
public class A
{
B obj1 = new B();
private void tabControl1_TabIndexChanged(object sender, EventArgs e)
{
obj1.PageChanged(tabControl1.SelectedIndex.ToString());
}
}
public partial class B
{
public void PageChanged(string Val)
{
//put your code
// after page chaged what should happen
}
}

Related

How to call a class method from a different class

I am creating Windows form application and its main form contains a panel. Different user controls are being loaded on that panel on button click.
namespace LearnCSharp
{
public partial class MainForm : Form
{
private void configButton_Click(object sender, EventArgs e)
{
var uControllerDashboard = new Controllers.Dashboard();
panel.Controls.Add(uControllerDashboard);
updateNotification("active");
}
private void updateNotification(string state)
{
switch (state)
{
case "active" :
//Do something here
break;
}
}
}
}
when click config button it loads Dashboard user controller into Panel there is a another apply button in Dashboard userControl. When I click that button I need to call updatNotification method in MainForm Class.
namespace LearnCSharp.Controllers
{
public partial class Dashboard : UserControl
{
private void btnApply_Click(object sender, EventArgs e)
{
// Need to call updateNotification method here.
}
}
}
how can I achieve my requirement. I appreciate any help. Thanks.
Use events for that.
Instead of calling the MainForm inside the UserControl, create an event in the UserControl and have the MainForm subscribe that event. Inside the UserControl you just need to trigger the event.
There are many examples on web about this. Just take a look at this:
How do I make an Event in the Usercontrol and Have it Handeled in the Main Form?
How do i raise an event in a usercontrol and catch it in mainpage?
Hope this helps.

How to move methods to external file in windows form C#

newbie question :(
I'm making a program using windows forms and i have a lot of small methods like this
private void label1_Click(object sender, EventArgs e)
{
textBox1.Select();
}
private void label13_Click(object sender, EventArgs e)
{
textBox13.Select();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
plotGraph(prostokat);
}
in the Form1.cs file and to make the code more transparent, I would like to move these small methods out somewhere to an external file (class?) but I don't really know how to do this. If they were normal methods I would just make a class and create an object of that class and just call the methods using that object but these are functions that "happen" when a user action is performed i.e. a textbox is clicked, so I'm not sure how to make this work.
It is possible to create an extra partial class (separated file) for your Form1 and place your cluttering methods there.
Or you could collapse them with #region
#region UI Handlers
#endregion
The perfect solution would be using some kind of MVVM for WinForms. In that case in your ViewModel you can implement your business logic separately from the code-behind.
Check out this:
https://www.codeproject.com/articles/364485/mvvm-model-view-viewmodel-patte
Hope it helps!
Have a look at your Form class subsection. It most cases it is still a partial class. Create a new .cs file in the same subsection in your project and add another partial form class to it.
You can find additional information here:
Partial Classes and Methods
Sure, you can add a new class to your project (right-click the project in Solution Explorer --> Add Class --> ) and put your methods there. Then you will need to hook the methods up to the controls in code:
I added a static class called "Form Methods" and put a method in there for label1 Click event:
static class FormMethods
{
public static void label1_Click(object sender, EventArgs e)
{
Label label = (Label) sender;
// Try to find the textbox through the label's parent form
TextBox textBox = (TextBox) label.Parent.Controls.Find("textBox1", true).First();
if (textBox != null)
{
textBox.Select();
}
}
}
Then in the Form Designer code, you can hook up the event:
this.label1.Click += new System.EventHandler(FormMethods.label1_Click);
Alternatively, you can make the class part of your original form class, and it will still be a separate file. If you want to do this, you can then make your event a private non-static method, and you would change the class definition to a public partial class:
public partial class Form1 // <-- This used to be: static class FormMethods
{
// This used to be: public static void label1_Click
private void label1_Click(object sender, EventArgs e)
{
. . .
And then hooking up the event looks like:
this.label1.Click += new System.EventHandler(label1_Click);
You can create any number of partial class files mimicking your original and group methods inside as your functionally needs - however, you won't be able to use the designer to directly navigate to your callbacks. That is, if you double click a graphic element or click an event of a graphic element you will have an unexpected behavior: in both cases you will have an event handler generated in your first partial and a hook created to that . . . so you can't directly navigate to those handlers anymore, and you need to go trough your partial files looking for their definitions.
Use partial to split C# code like this.
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}

C# - Unable to add item to listview from method invoked from class

Im having a strange problem with my programme, whenever I call a method from another class, it doesnt work as expected. Basically, what I am trying to do is add an item to a listview, and the code is in a method, and I am trying to invoke that method from another class. Here is my code:
public class Main1
{
public void addItemToLV(string text)
{
listView1.Items.Add(text);
}
}
public class MainForm
{
Main1 m1 = new Main1();
m1.addItemToLV("test");
}
Any ideas why this is happening? Thanks.
Under normal circumstances, the controls on a form are declared as protected properties within the designer.cs file rather than public properties, so those controls will normally not be accessible to code outside of the form on which they are declared. This is intentional, as it encourages good object-oriented programming habits.
Rather than modifying a control directly from code that exists outside the form, a better practice is to declare public methods and properties within your form that can be called from outside code to manipulate the protected controls:
public class Main1
{
public static void Main()
{
var mainForm = new MainForm();
mainForm.AddItemToListView("test");
}
}
public class MainForm
{
public void AddItemToListView(string text)
{
listView1.Items.Add(text);
}
}
You could try to pass the ListView as an argument in the constructor and make the function return this ListView to your MainWindow.
public class Main1
{
private readonly ListView _lv;
public Main1(ListView listview)
{
_lv = listview;
}
public ListView addItemToLV(string text)
{
_lv.Items.Add(text);
return _lv;
}
}
I think your code sample is not enough but I am trying to guess the reason behind the issue and I think it is one of the following:
1.You are initialising your listView1 in the load event of Main1, and you are trying to show the form after calling the method. In that case you should call the method after showing the Main1 form.
2. Or simply you are trying to call the method on a different instance than the one displayed.

How to get the value from a listBox1_SelectedIndexChanged event from another class?

I have a main form with a listbox, I also have a listBox1_SelectedIndexChanged event and more code going on for each item when it changes.
Is there a simple way to make this event in another class and not on the main form?
If not, as I meant, I don't want all this code on my main form's code so I want to move it to another class where it is more related.
what is the "best practice" way to notify Class B when listBox1_SelectedIndexChanged occurs? is it by a delegate? I tried to figure it out but didn't really understand. Help will be much appreciated.
Thanks.
I am not sure how both classes are linked to each other and their types but using following code you can get idea to solve your problem
public class A
{
public delegate void ItemSelectedHandler(string title);
public event ItemSelectedHandler OnItemSelected;
public void listBox1_SelectedIndexChanged(object sender, EventArg, e)
{
//other code
if(OnItemSelected!=null)
{
OnItemSelected("Something");
}
}
public void LaunchB()
{
var b = new B(this);
b.ShowDialog();
}
}
public class B
{
private A _parent;
public B(A parent)
{
_parent = parent;
_parent.OnItemSelected += onItemSelected;
}
public void onItemSelected(string title)
{
//will fire when selected index changed;
}
}
I think I solved my question in a simpler way.
I set the listbox1 access modifier to Internal
I created properties for the main form.
In Class B, I subscribed to the listBox1.SelectedIndexChanged event and created an event handler like this:
form1Properties.listBox1.SelectedIndexChanged+=listBox1_SelectedIndexChangedforClassB;
Then I've implemented the event handler in Class B like this:
private void listBox1_SelectedIndexChangedforClassB(object sender, EventArgs e)
{
MessageBox.Show("listbox1 selected item has changed!");
}
I hope I am using all the right terms, please correct me if I'm wrong, and please tell me if you find my solution is flawed in any way. Thanks.

Set User Control's default event

I have a User Control containing a bunch of controls. I want to set the default Event of this User Control to the Click event of one of my buttons.
I know for setting default event to one of the UserControl's events I should add the attribute:
[DefaultEvent("Click")]
public partial class ucPersonSearch : UserControl
...
I'm wondering if it's possible to do something like:
[DefaultEvent("btn1_Click")]
public partial class ucPersonSearch : UserControl
...
I want to fire some methods in the form hosting this User Control at the time btn1 is clikced.
This is really a knit in my project, and you're answer will be valueable.
You can't expose events of your class members to the outside of the class. How can others subscribe to the Click event of a Button inside your UserControl? Did you try it? It's not possible unless you make the button accessible from the outside, which is not good (everybody can change all the properties).
You have to define a new event, and fire your new event when your desired event (clicking on the button) happens:
[DefaultEvent("MyClick")]
public partial class UCPersonSearch : UserControl
{
Button btnSearch;
public event EventHandler MyClick;
public UCPersonSearch()
{
btnSearch = new Button();
//...
btnSearch.Click += new EventHandler(btnSearch_Click);
}
void btnSearch_Click(object sender, EventArgs e)
{
OnMyClick();
}
protected virtual void OnMyClick()
{
var h = MyClick;
if (h != null)
h(this, EventArgs.Empty);
}
}

Categories

Resources