So I have one form:
I want to then turn that into a variable on form 2 that is printed on for Form 1 code I have
private void button1_Click(object sender, EventArgs e)
{
//have user type term to search, and bring that input to form2
Form1.Enable = false;
Form newForm = new Form2(textBox1); //send data to new form
newForm.ShowDialog();
thats on the button click where i assume that where it sends the data
but for the text box itself i have :
private void txtBox1_TextChanged(object sender, EventArgs e)
{
Form newForm = new Form2(textBox1); //send data to new form
}
for form 2 idk how to put it in the other text box as the string I am extremely new to c# as i am coming from python but here is my form 2 code:
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
this.Form2.Text = textBox1;
}
note I have this in form 2 : public Form2(string textBox1)
and I want it to display in here:
note I am new so please be kind
Something wonky here, you say you have modified the Form2 constructor to accept a string but then you seem to send a textbox into it instead:
private void button1_Click(object sender, EventArgs e)
{
...
Form newForm = new Form2(textBox1); //send data to new form
I would make this:
Form2 newForm = new Form2(textBox1.Text); //send data to new form
And then make your Form2's constructor set the text in the form2 textbox:
public Form2(string tx){ //you must have this already
{
InitializeComponent();
someOtherTextboxOnForm2.Text = tx; //do it after InitializeComponent call
Please call your controls something different than "Form2", "textbox1" - youre allowed to rename them, it takes seconds to do and your code won't degenerate into an incomprehensible mess after about 10 controls
Related
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();
}
}
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 wants to show form2(Filter Form) first. Actually I am calling form2 from Form1(Report Test Form) page load. But It appears Form2 in back position and Form1 in First position.
Code
private void ReportTestForm_Load(object sender, EventArgs e)
{
ReportFilterForm report = new ReportFilterForm();
report.Show();
}
Screenshot
Note
I don't want to hide Form 1
Use TopMost Property:
private void ReportTestForm_Load(object sender, EventArgs e)
{
ReportFilterForm report = new ReportFilterForm();
report.TopMost = true;
report.Show();
}
ShowDialog will force Form2 to be close to be able to return to Form1
report.ShowDialog();
Am a newbie when it come to C# Programming. Here's my trouble:
I want to Show a new Form 2 and Hide Form 1 on the Windows Form 1 Load.
Here's my current codes;
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Hide();
}
My en-counted Error with the current Code:
When Form 1 load its loading Form 2 but it's not hiding itself. this.Hide Statement not working, I've try this.Close but this will Close the entire software as it's closing the main form.
Can anyone kindly help me with this error.
The Error i thought is that you are showing the form2 before hiding the previous form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
form1.Visible=false;
Form2.Show();
}
You can also make form2 as modal by using show dailog method() by which focus is given to the form2 and form1 becomes inactive though it will be shown.
modal and modeless forms https://msdn.microsoft.com/en-IN/library/aa984358%28v=vs.71%29.aspx
Here's how I manage to make this work.
New Codes:
Form 1
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
}
private void Timer_Tick(object sender, EventArgs e)
{
if (Properties.Settings.Default.Status == "Form2Visible")
{
this.Hide();
}
}
Explanation:
On the Windows Form 1 Load am showing the the Form 2, then using a timer to verify a Properties.Settings Value. If the Properties.Settings = Form2Visible, Form 1 will Hide. Once am done on Form 2 I simply need to change the Properties.Settings to something else and stop the Timer on Form 1.
If their is a simplest way let me know.
You can use Visible property to hide your form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Visible=false;
}
In windows form (c#), i am showing a form when user click on button, it is working fine form is visible to user, but if user click again on the same button the same form is opening again two forms are displaying. Is there any way to prevent this, please give me any reference for this thank you. This is my code....
private void button1_Click(object sender, EventArgs e)
{
Form2 obj = new Form2();
obj.Show();
}
You are most likely doing something like this:
void button1_OnClick(object sender, EventArgs e) {
var newForm = new MyForm();
newForm.Show();
}
So you are showing a new instance of the form every time it is clicked. You want to do something like this:
MyForm _form = new MyForm();
void button1_OnClick(object sender, EventArgs e) {
_form.Show();
}
Here you have just one instance of the form you wish to show, and just Show() it.
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(MyFormType))
{
form.Activate();
return;
}
}
Form newForm = new MyFormType();
newForm.MdiParent = this;
newForm.Show();
i tried more than a ways to compare which one is better.
but i think this solution must be better than the answer.
You can try something like
private Form f;
private void button2_Click(object sender, EventArgs e)
{
if (f == null)
{
f = new Form();
f.Closed += f_Closed;
f.Show();
}
}
void f_Closed(object sender, EventArgs e)
{
f = null;
}
You are most probably creating a new instance of the form every time in the Click handler of the Button.
So you ill need to move the Form object creation outside the Button_Click.
Here's a good example of a proven solution
This will open the form if it is not already open.
If it is already open, it will place it in the foreground.
namespace MainProgram
{
public partial class Form1 : Form
{
private Form formNew = new FormToShowSomething();
private void button1_Click(object sender, EventArgs e)
{
formNew.Show();
formNew.Activate();
}
}
}
the easiest solution to your problem is replacing the Show command with ShowDialog, that way you won't any problem when it comes to preventing a form to show up twice
Form2 obj = new Form2();
obj.ShowDialog();
the code: .ShowDialog(); is what we are currently looking for that will solve the issue
10 years after , like the band :p
Thought to share the code that works for me. Nothing fancy, just checking if the form instance exists. Also, I don't prefer the ShowDialog, because the user is 'trapped' in that form and I find it annoying. The user might want to check other info from another source, for example when filling an online form and needs to copy paste a field info.
private void button1_Click(object sender, EventArgs e)
{
var obj = Application.OpenForms.OfType<Form2>().Select(t => t).FirstOrDefault();
if (obj != null)
{
obj.BringToFront();
}
else
{
obj = new Form2();
obj.Show();
}
}