How to access runtime controls from another form? (C#) - c#

I have a form (Form1) and a button on it. When I press that button, I create runtime panels stored in an array of panels, declared like that:
Panel[] Panouri_variabile = new Panel[20];
If I press the button, a panel is created. If I press the button again, another panel is created underneath the previous panel and so on.
Each panel has a textbox inside it. Obviously, the textboxes are stored in an array of textboxes, declared like that:
TextBox[] Nume_variabila = new TextBox[20];
The user writes something in each textbox of each panel.
Now, I want to access the data written by the user in those textboxes, from another form, like that:
Form1 form = new Form1();
form.Panouri_variabile[i].Nume_variabila[i].Text
That could be easily done if the panels and the texboxes are created at design-time, by simply setting the Modifier property of all controls to public.
The problem is that they are created at run-time, so I can't change the Modifier property.
After a lot of searches I found the following posible solution:
Panel new_Panel = Panouri_variabile[i];
And then declare the following property at the same level as the event - handlers are (class-level I think)
public Panel new_Panel { get; private set; }
I noticed that I can see the new_Panel from another form, so I can access it like that:
Form1 form = new Form();
form.new_Panel
but the problem that it is not indexable! I have an array of panels (and an array of texboxes) so I should access them using an index, as I specified above!
Is there a way of accessing those texboxes from another form? Or should I create them design-time?

Related

c# create a user control on button press, won't show

counters.Add(new Counter());
foreach (Counter con in counters)
{
con.Show();
con.Top = this.Top;
con.Left = this.Left;
}
counter is a very basic UserControl I made with 3 buttons and textbox. I'm trying create a draggable Counter, I can see the counter on the list (counters) but I can't see it anywhere on screen.
I was wondering if new Counter() is enough to create the UserControl on screen.
(i mean to create this "counter" object dynamically)
Control needs to have a parent that hosts the control. The parent control can be either a form or another container control. When you want to add your control to the hosting control, add the instance into the Controls collection of the hosting control like this:
var counter = new Counter();
var form = new Form();
form.Controls.Add(counter);
form.Show();
I think user Controls need to be in some sort of container, for example a flowLayoutPanel or your instance of the Form class itself to visually exist.
You need to call CONTAINERINSTANCE.Controls.Add(con).

How should I know which form is loaded inside the panel programmatically

I have a form which consists a panel and three buttons called save and loadForm1, loadForm2. When I click loadForm1(2) button it loads the particular form into the panel.
But I have only one save button. I want to save the data which are on forms in the panel into the database..
How can I access the particular form? (I mean I want to know which form is loaded in the panel when I click the save button)
var form = Panel1.Controls.OfType<Form>().First();
if(form.Name =="Form1") // or you can check ID
{
}
Panel has Children property, which is array actually. Also every control has Tag property, you can write here whatever you want. So when you load some control into panel just initialize its Tag property with some value and when save button is pressed check its tag property value.
var form1 = new SomeControl();
form1.Tag = 1;
yourPanel.Children.Add(form1);
// and then somewhere in code
if (yourPanel.Children[0].Tag == 1)
{
// control 1 action
}
It's the easiest way I think.

Accessing multiple forms in C#

I'm trying to access a form from my main form of my project. The new forms name will be AboutBox1 and I want it to open when a user clicks on the About toolstrip button.
When I go to code in the About toolstrip button though the Intellisense does not recognize my new form (AboutBox1) that I had created
I have read that I need to create an instance of my new form however I'm not sure where or how to do that.
Below is the code that I have. (Ignore all the stuff in the middle, the toolbox button that needs to be clicked to access the new form is at the bottom)
https://gist.github.com/anonymous/5366535
in the onClick-Handler of you MenuItem:
//assuming AboutBox is derived from Form
AboutBox AboutBox1 = new AboutBox();
AboutBox1.Show(this);
First, if you need access to your form from outside the method, make a variable at the top of the class.
AboutBox1 frmAboutBox;
[STAThread]
...
But seeing this is an About page I don't think you need that.
Then, make and launch your form.
AboutBox frmAboutBox = new AboutBox();
frmAboutBox.Show();
You can also do frmAboutBox.ShowDialog() to make the newly hatch form always be on top.

How can I add an existing tab of a windows form in another windows forms page?

Working on c# 2.0
.NET windows forms
I would like to show existing tab of a windows form in another windows forms page referecing the page which has the tab I want to show.?
I am not sure if I understand your problem correct, but here's what I get:
You have a window form say FormA on which you have a Tab Control, the Tab Pages might contain some more controls like TextBoxes etc.
Now you want this TabControl with all its child controls to be MOVED to a new form say FormB, by MOVED I mean the very instance of the Tab Control on FormA rather than a new instance. Which will mean that if user had typed something on say a TextBox within the TabPage even the User's typed text should be present in FormB when the entire control is moved there.
If the above scenario is right, then as someone above pointed out this is NOT ALLOWED in .Net one control is hosted in one container it can not be visible at two places simulteneously.
However, you might be interested in showing(rather using) the control in another form just for a while and it is alright if I totally rip off the control from the first form. Say a scenario where FormA will open FormB as a modal window and pass on its own Tab Control to it, user will use the control on FormB then close the modal Form FormB and the Tab control is back in its original position.
This is quite possible, just remove the Tab Control from its container in FormA pass it to FormB which will add it to some container and display it and vice versa.
For eg. say on a button Click on FormA you can write:
FormB myForm = new FormB();
this.Controls.Remove(this.tabControl1);
myForm.AddTabControlToPanel(this.tabControl1);
myForm.Show();
then the FormB will have a function
public void AddTabControlToPanel(TabControl tc)
{
panel1.Controls.Add(tc);
tc.Left = 10;
tc.Top = 10;
tc.Width = 200;
tc.Height = 200;
}

User control in windows forms application

I have a simple user control with a text box and label in it. I created public properties to access the text in the textbox when I use the user control in another form.
My problem is the property is returning null value when I call it in the form. Am i missing anything?
My property is as follows::
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string rtnTxtMake
{
get
{
return txtMake.Text;
}
set
{
txtMake.Text = value;
}
}
}
and in the next forms button click event i call the property as follows
UserControl1 Usc = new UserControl1();
string Make = Usc.rtnTxtMake;
MessageBox.Show(Make)
UserControl1 Usc = new UserControl1();
string Make = Usc.rtnTxtMake;
If your user control has by default an empty textbox field, then it seems correct that the above two lines of code would return either null or String.Empty (check via String.IsNullOrEmpty), since you explicitly create a new instance of your user control.
I suppose what you really want is this:
You have inserted a user control into a form in the Designer. Let's call this user control instance ctlUser.
You have a button with a Click event handler. The last few lines of code in your question are from that handler method.
In the handler, you wouldn't create a new instance of your user control (Usc) but refer to the one that you previously inserted into your form, ctlUser. Then things should work as expected.
Your UserControl must be added to the Controls collection of a parent Form/Control before it can be properly initialized. Normally you would not write the code yourself that creates and adds the UserControl.
Instead, first build your project, then go to the Deisgner view of your main form and look at the Toolbox.
Your UserControl name (and an icon) should appear towards the top of the toolbox, and you can simply drag it to the main form. The Windows Forms designer will automatically generate the needed initialization code for you.
You should not create a new instance of your control in your button click event handler. Using the Designer approach to create your control you can simply access the existing instance of your control as follows:
public void button_Click(object sender, EventArgs e)
{
// myUserControl1 has already been created and initialized by the Deisgner generated code
// Note the name 'myUserControl1' is just an example, yours may be different.
string controlText=myUserControl1.rtnTxtMake;
// Or to change the UserControl textbox value
myUserControl1.rtnTxtMake="Testing";
}
What exactly to you mean when you say that the property is returning a null value? Is it actually null, or is your MessageBox simple showing empty?
I quickly duplicated your code and it behaves exactly as expected - the MessageBox shows, but it is empty because the default value of the Text property of the TextBox control is an empty string.
Also, the way you are approaching this is a little unusual.
Firstly, the line:
UserControl1 Usc = new UserControl1();
You do not generally need to instantiate a user control like this. Instead you can drag the control from the toolbox onto the design surface of your form. This will then take care of instantiating and initialising your control for you.
I think that this is actually your problem - when you include the line of code above, you are creating a new instance of the user control, and this is is no way realted to the user control that you have dragged onto the designer.
If you go to the designer view of your form and click on the user control, you should see a properties window somehere. If you do no, then either select it from the View menu, or press F4. In the list of properties, there should be one "Name" this is the programatic name generated for your user control. You can change this here if you want, but when you refer to this control in the rest of the form, this is what you must use.
Secondly, the next two lines:
string Make = Usc.rtnTxtMake;
MessageBox.Show(Make)
You can access the property rtnTxtMake directly. Unless you later need to access the Make string in the rest of your code, then directly accessing the property would usually be considered better style.
MessageBox.Show(userControl.rtnTxtMake);

Categories

Resources