Windows Forms C#: Access TabControl from UserControl - c#

I have a TabControl and a UserControl interacting in the following way:
Each time a new tab is opened, the UserControl loads onto the new tab.
In the UserControl there's a Panel, a TexBox and a Button. Each time text is entered into the TexBox and the Button is pressed, it's supposed to update the title of the current tab
How do I access the tab title from within the UserControl?

Better if the user control does not know where it is embedded into.
Consider providing a TitleChanged event in the user control instead. Then it can be the responsibility of the consumer to update itself accordingly.
public class MyUserControl : UserControl
{
// [...]
public string Title { get; private set; }
public event EventHandler TitleChanged;
// [...]
private void MyTextBox_TextChanged(object sender, EventArgs e)
{
Title = MyTextBox.Text;
TitleChanged?.Invoke(this, EventArgs.Empty);
}
}
And the necessary code of the consumer class can be sg like this:
// after subscribing the myUserControl.TitleChanged event:
private void MyUserControl_TitleChanged(object sender, EventArgs e)
{
myTab.Text = myUserControl.Title;
}
Even better if you use data binding in the user form:
myTab.DataBindings.Add(nameof(TabPage.Text), myUserControl, nameof(MyUserControl.Title));

Related

Changing panel control from inside the panel

I am creating an application using WinForms. I have panel in which I show a user control. Inside this user control I have a button. When I click the button, I want to clear the panel and show a different user control. I am trying to do that using the following code:
private void btnCreateOffer_Click(object sender, EventArgs e)
{
var myControl = new WindowsFormsDemo.View.CreateOffer();
MockUpForm.panMain.Controls.Clear();
MockUpForm.panMain.Controls.Add(myControl);
}
This works from the buttons placed directly in the parrent form, but when I use in inside the user control, it says:
'MockUpForm.panMain' is inaccessible due to its protection level
I suppose it has something to do with private/public classes. But I would rather have the "correct" solution, as opposed to just changing everything to public.
Any suggestions on how this is usually done?
Solution 1 (ugly):
Make panMain public in the designer:
Solution 2 (somewhat better):
Provide public methods to achieve such tasks safely:
// MockUpForm code:
public void ClearPanelControls()
{
panMain.Controls.Clear();
}
public void AddControlToPanel(Control c)
{
panMain.Controls.Add(c);
}
And then call these methods instead of publishing the full panel, which makes possible for example to dispose the whole panel and such things...
To access parent form's control from UserControl You can use delegate and event
something like this....
Windows Form (Parent Form) Code....
public Form1()
{
InitializeComponent();
userControl1.CreateOffer += UserControl1_CreateOffer;
}
private void UserControl1_CreateOffer()
{
var myControl = new WindowsFormsDemo.View.CreateOffer();
this.panMain.Controls.Clear();
this.panMain.Controls.Add(myControl);
}
User Control Code...
internal delegate void CreateOfferDelegate();
internal event CreateOfferDelegate CreateOffer;
public UserControl1()
{
InitializeComponent();
}
private void btnCreateOffer_Click(object sender, EventArgs e)
{
CreateOffer();
}

How to allow control inside the splitview's pane close the pane in uwp?

I have a customized control inside the splitview's pane. Now I have the method hideinternal() defined inside the control which makes the control invisible. However, at the same time I also want to close the pane. What should I do? (I know it is SplitView.IsPaneOpen = false; I don't know how to get access to it inside the control's code behind)
I would raise an event from within hideinternal that can then be subscribed to in your view where you could hide the Pane.
eg in your Custom Control the event
public event EventHandler CloseSplitViewPane;
public void OnCloseSplitPaneView(object sender, EventArgs e)
{
CloseSplitViewPane?.Invoke(sender, e);
}
in hideinternal
public void hideinternal()
{
OnCloseSplitViewPane(this, new EventArgs());
}
in your view for example MainPage.xaml.cs constructor (Your Custom Control is called MyControl in this example)
public MainPage()
{
this.InitializeComponent();
MyControl.CloseSplitViewPane += (sender, e) =>
{
SplitView.IsPaneOpen = false;
};
}
Hope that helps
I finally defined an event handler inside the control;
public event EventHandler handlesomething;
then in hideinternal I notify the event;
private void hideinternal()
{
doSomething();
this.NotifyEvent(handlesomething);
}
in the main page's XAML I sign the event handler to some events inside the page
<Control
handlesomething ="SomeMethodsInsideMainPage"
/>

How to pass textbox data between two forms?

How to send textbox value to textbox between two forms without Show()/ShowDialog() by button?
I want to textBox will get value without open form.
To access the textbox data you need to use: textBox1.Text
a form is an object so you can define a method that updates the text box value (you can expose the textbox itself with a public accessor)
To pass information from a parent from to a child form you should create a property on the child form for the data it needs to receive and then have the parent form set that property (for example, on button click).
To have a child form send data to a parent form the child form should create a property (it only needs to be a getter) with the data it wants to send to the parent form. It should then create an event (or use an existing Form event) which the parent can subscribe to.
An example:
namespace PassingDataExample
{
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ChildForm child = new ChildForm();
child.DataFromParent = "hello world";
child.FormSubmitted += (sender2, arg) =>
{
child.Close();
string dataFromChild = child.DataFromChild;
};
child.Show();
}
}
}
namespace PassingDataExample
{
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
public string DataFromParent { get; set; }
public string DataFromChild { get; private set; }
public event EventHandler FormSubmitted;
private void button1_Click(object sender, EventArgs e)
{
DataFromChild = "Hi there!";
if (FormSubmitted != null)
FormSubmitted(this, null);
}
}
}
I don't know what exactly you mean by saying "without Show()/ShowDialog()", but that is not relevant anyway or I'll just assume here further that you have both windows open (doesn't matter how you accomplished that).
You would like to avoid much coupling between two forms, especially not implementation details like a textbox etc. You could work with delegates and events to trigger the "sending" of data between your two forms. You can then easily pass event data and your subscribed other form (or any other object as a matter of fact) doesn't know the exact implementation details of your form, it only knows the data it will receive through the delegate (event). I am not going to post all code here, because it is already nicely explained at following URL: http://www.codeproject.com/Articles/17371/Passing-Data-between-Windows-Forms .

Pass Value Selected From a User Control to a Parent

I have a Winform dialog that contains several user controls - all of them are some sort of Datagridview. The main parent has details about a user, and the user controls each have additional details on that person. When my Dialog first loads all of the UserControls work but I am trying to figure out how to update the UserControl2 based on a position change in UserControl1.
So, I am trying to select a row in UserControl1 and have the data in UserControl2 update based on a value that I just selected.
I have tried using MouseDownEvents on the UserControl1 and BindingSourcePositionChanged but I can't figure out how to get the value selected back to my parent form and then use that value to refresh the other datagrids?
I looked at delegates and events but I guess the lack of sleep is making it incredibly hard to comprehend. I understand that I need to create my delegate and event on the UserControl1 and then somehow call it on my mainform but that's where I get stuck and have no clue where to start.
Is this the right direction? Or is there another way to get this done? Can anyone offer up any suggestions on how this works?
Yes this is the correct approach something like the following will provide an event handler that you can use to the retrieve a public property from the UserControl:
public class SomeClass : BaseControl
{
public event EventHandler PersonSelected;
public string Name{get;set;}
protected void FindUser()
{
var find = new Button {ID = (ToString() + "search"), Text = "Search"};
find.Click += delegate(object sender, EventArgs e)
{
if (PersonSelected!= null)
{
//forward this event to the page's event handler
PersonSelected(this, e);
}
};
}
}
public class SomeOtherClass : Page
{
public void Main()
{
var sp = (SomeClass)Control;
sp.PersonSelected += BtnClick;
}
public void BtnClick(object sender, EventArgs e)
{
//Get some value from the (SomeClass)Control here
}
}

How can I make sender in an eventhandler my custom control and not the labels inside it

I created a custom control which is actually just two labels inside a panel. I want to add an event so that when my custom control is clicked (which would really be clicking either one of the labels) it would return the properties of the whole control, I think that would mean that 'sender' in the event handler would be my custom control and not one of the lables. I don't know if I made myself clear but what I mean is to treat the control as a 'whole' when it is clicked mmm anyway hope you get my point.
How can I do this?
Thanks in advance
What you can do is let the custom control consume the event of the label, and in the custom control implement a new event. Then, when the label event fires, you can fire your own event from the custom control.
For example:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public event EventHandler MyCustomClickEvent;
protected virtual void OnMyCustomClickEvent(EventArgs e)
{
// Here, you use the "this" so it's your own control. You can also
// customize the EventArgs to pass something you'd like.
if (MyCustomClickEvent != null)
MyCustomClickEvent(this, e);
}
private void label1_Click(object sender, EventArgs e)
{
OnMyCustomClickEvent(EventArgs.Empty);
}
}
You can get the container object of your label and cast it to your custom control
private void Label1_Click(object sender, System.EventArgs e)
{
Box box = (object as Label).Parent as Box;
if(box != null)
{
//Do what you need here.
}
}

Categories