Change Form 1 Label value using UserControl Window Form - c#

I am new to windows Forms and didn't know about user control. I have a form 1 with a label and want to change the value of the label using user control. I also called user control in form1. I tested the form function using the usercontrol_click event. It shows the message box but not changing the label value.
Form 1 user control name flowLayoutPanel1
user control name ListView
private void ListItem_Click(object sender, EventArgs e)
{
Form1 a = new Form1();
//a.emailbody();
a.label5.Text = "work";
}
public void emailbody()
{
MessageBox.Show("Welcome");
}

You are creating a new form with Form1 a = new Form1(); in the click event handler, but you never show this form using a.Show(); or a.ShowDialog().
Did you intend to set the label on the current form? If yes, then don't create a new form.
private void ListItem_Click(object sender, EventArgs e)
{
// Uses label in this form:
label5.Text = "work";
}
You simply write emailbody(); to call a method on the current form.
If you intended to set the label on another form that was already open, you need a reference to this form. E.g. you can store it in a field in the current form
private Form1 _form1;
private void OpenOtherFormButton_Click(object sender, EventArgs e)
{
_form1 = new Form1();
_form1.Show();
}
private void ListItem_Click(object sender, EventArgs e)
{
if (_form1 is not null) {
_form1.label5.Text = "work";
}
}
If you want to change the label of your user control, the label must be public. Assuming that the user control is on your form, do something like this:
myUserControl.label5.Text = "work";
If it is on another form, the user control must be public as well:
_form1.myUserControl.label5.Text = "work";
if the user control is on the panel:
flowLayoutPanel1.myUserControl.label5.Text = "work";
If the user control is on the same form as where the label is and ListItem_Click is in the user control, then you must get reference to the form first.
// In the user control
var form1 = FindForm() as Form1;
if (form1 is not null) {
form1.label5.Text = "work";
}

Related

Creating a temporary name for a button

I've been looking for a solution for days now.
Currently have 2 Forms. Main Form has multiple buttons, lets say (1-10).
All buttons will open up my 2nd Form (say I press Button 4). On my 2nd Form I have a ComboBox with different names and a confirm button. When I choose a name from the ComboBox, then press the confirm button.
I want the name selected in the ComboBox to be displayed as the new button text from my Main form (So name3 from Form 2 ComboBox will replace Button text (Button 4) on Main Form).
Any suggestions on how I can achieve this?
I can get the text from ComboBox to Main Form into a Label or a Button of my choosing, but I can't do it from the pressed button on Main Form which opened Form 2.
I've tried changing the button pressed on Main Form to a buttonTemp name, then letting the text from ComboBox change buttonTemp text, but it's coming up as it doesn't exist on Form 2.
Form 1 code:
public void b1111_Click(object sender, EventArgs e)
{
b1111.BackColor = Color.Red;
buttonTemp.Name = "bTemp2";
b1111.Name = "buttonTemp";
Classroom f4 = new Classroom();
f4.Show();
}
this is on Form 2:
private void button1_Click(object sender, EventArgs e)
{
temp1 = comboBox1.Text;
// trying to figure out the label text
foreach (Term1 Form1 in Application.OpenForms.OfType<Term1>())
{
Form1.buttonTemp.Text = comboBox1.Text;
}
this.Close();
}
Do not operate on the controls of other forms. Instead operate with values.
In your case when you finished and closed Form2 you can return a value back to the Form1 and update button text with a returned value.
In Form2 create public property which will be populated before you close Form2.
public string SelectedName { get; set; }
private void selectNameButton_Click(object sender, EventArgs e)
{
SelectedName = comboBox1.Text;
this.Close();
}
In Form1 use .ShowDialog() method to display form in modal form
public void openForm2Button_Click(object sender, EventArgs e)
{
openForm2Button.BackColor = Color.Red;
using (var form = new Classroom())
{
form.ShowDialog();
// next line will be execute after form2 closed
openForm2Button.Text = form.SelectedName; // update button text
}
}
Suggested by #Enigmativity in the comments:
// Form 2
public string SelectedName => comboBox1.Text;
private void selectNameButton_Click(object sender, EventArgs e)
{
this.Close();
}
// Form 1 remains same
There is many ways to get your goal.
I hope you try to use event.
You can make your own event as below.
private void Form1_Load(object sender, EventArgs e)
{
//define listen event from custom event handler
_form2.OnUserSelectNewText += new Form2.TextChangeHappen(_form2_OnUserSelectNewText);
}
When you have member variable for remember which button clicked by user.
private Control activeControl = null;
and you can get text that user's choice from your custom event at Form2.
//to make simple code, centralize all buttons event to here.
private void button_Click(object sender, EventArgs e)
{
//to remeber which button is clicked.
activeControl = (Button)sender;
_form2.ShowDialog();
}
and then you just change text of "activeControl".
private void _form2_OnUserSelectNewText(string strText)
{
activeControl.Text = strText;
}
please refer this, how to make custom event with delegate.
public partial class Form2 : Form
{
//you can expand your own custom event, string strText can be Control, DataSet, etc
public delegate void TextChangeHappen(string strText); //my custom delegate
public event TextChangeHappen OnUserSelectNewText; //my custom event
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// to prevent null ref exception, if there is no event handler.
if (OnUserSelectNewText != null)
{
OnUserSelectNewText(this.comboBox1.Text);
}
this.Close();
}
}

Call a user control from other user control

So basically I have two User Control
1.Maptab.cs
2.TransactionTab.cs
And a Main menu.
So I have a button in main menu and when click it calls the maptab.cs, and in maptab.cs I have a button named btnbuy. So what I want to do is to call the transactiontab.cs or bring to front in the main menu when the button btnbuy in the maptab.cs is clicked.
I tried it like this in maptab.cs:
private void btnBuy_Click(object sender, EventArgs e)
{
Mainmenu main = new Mainmenu();
main.transactionTab1.BringToFront();
}
and it was not working. And also I'm gonna pass some values from maptab to transactiontab.
After creating an instance of form, just use Show() or ShowDialog() to display your desired form.
For example,
private void button1_Click(object sender, EventArgs e)
{
Form2 secondform = new Form2();
secondform.value = 33; //"value" variable must be declared as public in Form2
secondform.Show(); //Use secondform.ShowDialog(); to prevent user from entering main form without closing Form2.
}

how to open windows form in panel from the another form

I am working with windows form ASP.net C#
I have panel1 in which I am opening a form named form1.
form1 has a button. On this button clicke I want to close the form1 and open the new form named form2 in the same panel1
I have opened the form1 in panel1 as follows
Form1 frm = new Form1();
frm.TopLevel = false;
frm.FormBorderStyle = FormBorderStyle.None;
panel1.Controls.Clear();
panel1.Controls.Add(frm);
frm.Visible = true;
Please help
First - I suggest you to use User Controls, which are supposed to be used as reusable containers of other controls. Forms are supposed to represent windows. So, usage of form as a controls container hosted on another window is not very good idea. If you need to have ability to show same data both in your panel and in separate window, then use same user control both on your form with panel and on Form1.
So, with user control. Adding them to panel is really simple:
UserControl1 control1 = new UserControl1();
control1.Dock = DockStyle.Fill;
control1.SomethingHappened += UserControl1_SomethingHappened; // see below
panel1.Controls.Clear();
panel1.Controls.Add(control1);
To have ability to switch user controls you can add event to UserControl1 and raise it when button is clicked:
// UserControl1 code
public event EventHandler SomethingHappened;
private void Button1_Click(object sender, EventArgs e)
{
if (SomethingHappened != null) // notify listeners, if any
SomethingHappened(this, EventArgs.Empty);
}
Then handle this event on your main form:
// MainForm code
private void UserControl1_SomethingHappened(object sender, EventArgs e)
{
UserControl1 control1 = (UserControl1)sender;
sender.SomethingHappened -= UserControl1_SomethingHappened;
UserControl2 control2 = new UserControl2();
control2.Dock = DockStyle.Fill;
panel1.Controls.Clear();
panel1.Controls.Add(control2);
}

C# change textbox text on a modal form from an another form

I'm trying to change a text on a TextBox on a modal main form by clicking on a button from an another active form, need help.
Main form *Modal mode
public void changetext(){
textbox1.text = textnew;
}
form2 *active form
private void btnChange_Click(object sender, EventArgs e)
{
mainform form1 = new mainform;
public String textnew = "NEW"
form1.changetext();
this.close
}
Ive tired to use this code but it gives me the error of : Invoke or BeginInvoke cannot be called on a control until the window handle has been created.:
public void LabelWrite(string value)
{
if (InvokeRequired)
Invoke(new LabelWriteDelegate(LabelWrite), value);
else
{
textBox1.Text = value;
}
}
delegate void LabelWriteDelegate(string value);
i think there's a logic issue. If i understand your requirement, you have a main form which contains a search textbox. When the user launch a serach, you open a modal form where all possible results are displayed. The user selects the value he wants and then you get the result in the main form. Is this correct? If so you should do it this way:
Create a public property on the modal form which contains the result.
Either create a public property or create a new constructor on the modal form to pass the query.
On the main form, you can access the public properties of the modal form as long as it is not disposed.
For instance:
var result = null;
var modal = new ModalForm(query);
if(modal.ShowDialog() == DialogResult.OK) // This means the user has selected a value
{
result = modal.SelectedResult;
}
modal.Close();
modal.Dispose();
The easiest way is to pass the new text to the modal window.
For example:
Main form Modal mode
public void changetext(String textnew){
textbox1.text = textnew;
}
form2 active form
private void btnChange_Click(object sender, EventArgs e)
{
mainform form1 = new mainform;
form1.changetext("NEW");
this.close
}
If I were you I would also change form names, they are a little bit confusing.
P.S. I still don't get what is this.close is needed for.

Writing from textbox in Form2 to Datagridview in Form1

I am still new to C# so please bear with me.
I have Form1 with a DataGridView and a Button. This button opens Form2.
Form2 contains a TextBox and a Button that closes Form2.
I need to write the text from the TextBox in Form2 into the first cell of the DataGridView in Form1. In the application I am developing, there is other data already in the DataGridView in Form1.
I have uploaded the Visual Studio 2010 file here.
EDIT:
Please look at this screenshot:
Here is the code I'm using:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.dataGridView1.Rows[0].Cells[0].Value = textBox1.Text;
this.Close();
}
}
I seem to instantiating a new Form1 when I don't want to.
Appreciate the help.
You don't need Form2 to instantiate (again) the main form (Form1).
A more appropriate approach is to open the auxiliary form containing the text-box as a modal-dialog window, and let the opener form (Form1) access the text entered by the user at Form2 instance.
Here below are described the changes needed:
Form2 changes:
1.- Add a new class member to store the string that is to be introduced in the text-box textBox1.
public String textFromTextBox = null;
2.- Add code to your OK button's clic event-handler, so that you store in the new class member textFromTextBox the value introduced in the text-box:
3.- Last, in the same clic event-handling code set the DialogResult property to DialogResult.OK.
The Form2 code would look like this:
public partial class Form2 : Form
{
[...]
// This class member will store the string value
// the user enters in the text-box
public String textFromTextBox = null;
[...]
// This is the event-handling code that you must place for
// the OK button.
private void button1_Click(object sender, EventArgs e)
{
this.textFromTextBox = this.textBox1.Text;
this.DialogResult = DialogResult.OK;
}
}
Form1 changes
1.- In your button with the label "Enter Text" (that is actually missing in your code), in the Click event-handler put the code necessary to open the Form2 as a modal Dialog.
2.- Set the cell value in your data-grid accordingly by recovering the value stored in the Form2's textFromTextBox member.
3.- Finally dispose your Form2 instance.
Form2 myFormWithATextBox = new Form2();
if (myFormWithATextBox.ShowDialog(this) == DialogResult.OK)
{
this.dataGridView1.Rows[0].Cells[0].Value = myFormWithATextBox.textFromTextBox;
}
myFormWithATextBox.Dispose();
Take into account that your main form is Form1 while Form2 it is just an auxiliary form control, it should not take much control on the flow of your application, and therefore not assume the responsibility of instantiating the main form.
You can pass variable from Form to another by create another contractor that accept parameters as the following:-
1) go to form1 then create another contractor:
public Form1(string myString)
{
InitializeComponent();
if (myString != null)
dataGridView1.Rows[0].Cells[0].Value = myString;
}
2) go to form2 and under the button write this code:
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1(textBox1.Text);
frm1.ShowDialog();
}
Here you are your application after modification

Categories