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();
}
}
Related
please be kind enough to note that I'm new to visual studio.I have a form named form1 in it I have 2 user controls namely uctxt and ucbtn. uctxt has a textbox named txt1 and ucbtn has a button named btn1.
I need to fill txt1 with some text by clicking btn1(I tried public modifiers). I searched everywhere on the internet for a solution for this and I found nothing.
I tried:
public void Btn1_Click(object sender, EventArgs e)
{
uctxt ucText = new uctxt();
ucText.txt1.Text = "welcome";
}
You need at least to expose the OnClick event on the ucbtn:
public event EventHandler UserControlButtonClick;
protected void Btn1_Click(object sender, EventArgs e) =>
if (this.UserControlButtonClick != null)
this.UserControlButtonClick(this, e);
And the label on the uctxt:
public String UserControlLabelText
{
get{return txt1.Text;}
set{txt1.Text = value;}
}
After that you can use them both from the main form like this:
ucbtn1.UserControlButtonClick += new EventHandler(ucbtn1_ButtonClick);
protected void ucbtn1_ButtonClick(object sender, EventArgs e)
{
uctxt1.UserControlLabelText = "your text";
}
I'm not sure if focus is the right word but I have an on key up event on my form that will open a new form and close the current form, however after i enter a textbox or other such object i can't re select the form to be able to activate the key up event
This is code a I am using currently when i click on my form, to try select my currently open form however it does not close the current form when i active the key up event, when i do it this way
private void frmLevel1_Click(object sender, EventArgs e)
{
this.BackColor = GlobalClass.BG;
frmLevel1 lvl1 = new frmLevel1();
lvl1.Select();
}
I'm not 100% sure, but you have a few different questions. I hope it helps.
// Button click event
private void button1_Click(object sender, EventArgs e)
{
// Focus on textbox
this.ActiveControl = textBox1;
}
// Form load event
private void Form1_Load(object sender, EventArgs e)
{
// Focus on textbox
this.ActiveControl = textBox1;
}
// Close the current form and open another one. Use any event what you want
private void button1_Click(object sender, EventArgs e)
{
using (Form2 frm = new Form2())
{
// Hide the current form. If you close it it will dispose of all further events
this.Hide();
// Open the new form
frm.Show();
// Close the current form
this.Close();
}
}
I have two window forms, Form1 is for country name and Form2 is for city name. In form2 I have a comboBox which fetch country name from database that I saved by Form1 and its working fine and fetching data properly. But I want a button beside comboBox like
please see this image for better understanding,
and when click on it button will open Form1 and when I will add a new country in Form1, comboBox in Form2 should get updated and Form1 should close. How to do this? My code is...
In Form2 (for city name)
private void addBtn_Click(object sender, EventArgs e)
{
Add_Country ac = new Add_Country();
ac.ShowDialog();
}
public void refreshComboBox()
{
comboBox_CountryName.Refresh();
}
In Form1(for country name)
private void saveBtn_Click(object sender, EventArgs e)
{
string country = txtBox_countryName.Text.ToLower();
insertCountry(country);
showCountry();
Add_City ad = new Add_City(); //Form2 object refrence.
ad.refreshComboBox();
this.Close();
MessageBox.Show("Country added successfully.");
clearControl();
}
The line ac.ShowDialog() helps you handle after the Add_Country form is closed. So, you can refetch the countries from database just after ShowDialog line such as;
private void addBtn_Click(object sender, EventArgs e)
{
Add_Country ac = new Add_Country();
ac.ShowDialog();
// Repopulate the Country Combobox
}
in form2 after showdialog() refresh your combo,
you don't need to refresh it in the first form
private void addBtn_Click(object sender, EventArgs e)
{
Add_Country ac = new Add_Country();
ac.ShowDialog();
refreshComboBox()
}
when you are closing the dialog the run time cursor will be back to the form2 and the other story will be executed
I have a form called ListaDeAlunos with a DataGridView on it.
When I double click on any cell, I want to open a form called Alunos for the selected row of the DataGridView on the form ListaDeAlunos, so I can edit the record. I almost got it to work, but the form Alunos does not show the right record when it opens.
Here's what I did so far:
On the source form I created a class called Variables
and in that class I created a public static string called RecordName
class Variables
{
public static string RecordName;
}
On the source form I created a CellDoubleClick event:
private void tbl_alunosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
Form Alunos = new Alunos();
Alunos.MdiParent = this.MdiParent;
Alunos.Show();
Variables.RecordName = this.tbl_alunosDataGridView.CurrentRow.Cells[1].Value.ToString();
}
On the second form (the one that will open in the DoubleClick event), I have the following code on the Form_Load event:
private void Alunos_Load(object sender, EventArgs e)
{
this.tbl_alunosBindingSource.Filter = string.Format("Nome LIKE '{0}%'", Variables.RecordName);
}
Any ideas on how to fix this? It's almost working!
PROBLEM SOLVED !!!
All I had to do was to put the last line of code on top. Simple.
private void tbl_alunosDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{ Variables.RecordName = this.tbl_alunosDataGridView.CurrentRow.Cells[1].Value.ToString();
Form Alunos = new Alunos();
Alunos.MdiParent = this.MdiParent;
Alunos.Show();
}
So, I've just started learning C# and I have been looking on tutorials in YouTube but in Console Applications.
I have now made my first WFA and I'm trying to create a Calendar where you can add different times with different texts so you stay informed for example a upcoming test.
So far I've come this far: It's in Swedish
And I've also connected the "Lägg Till" (Add in English) to another form called laggTill
Connection in codeform -
laggTill lgtl = new laggTill ();
lgtl.Show ();
Form2 called laggTill looks like:
Also in Swedish
So my question is, how can I by pressing the "Spara" button (Save in English) put the text from the TextBox in laggTill form to the CheckBox in the "Kommande datum:" CheckBox?
Create public properties in Form2, in the example below you can see how to access them.
you did not asked that, but sometimes you dont want to update Form1 (for example the user pressed on cancel button) - use the DialogResult value of Form 2 in order to determine if there is a need to update Form1, in the example DialogResult is DialogResult.OK but it could be also DialogResult.Cancel.
user presses "Spara" button on Form2:
public string valueToForm1 { get; set; } //public properties to access from form1
public DateTime value2ToForm1 { get; set; }
private void button1_Click(object sender, EventArgs e)
{
this.valueToForm1 = "SomeValue";
this.value2ToForm1 = dateTimePicker1.Value;
this.DialogResult = DialogResult.OK;
this.Close();
}
calling Form2 from Form1:
private void button1_Click(object sender, EventArgs e)
{
using (var form = new Form2())
{
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
//values preserved after close
string val = form.valueToForm1;
DateTime dateValue = form.value2ToForm1;
//for example
this.txtValueFromForm2.Text = val;
this.dateTimePicker1.Value = dateValue;
}
}
}
on another button on Form2 (lets say cancel button) you can do that piece of code, if there is a case that you dont want to update Form1:
private void button2_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}