Opening a Form2 by clicking a textbox? - c#

How do I make so when I click on a textbox, make form2 open?
I understand is something like
Form2 f2 = new form2();
f2.show();
but its not a button, it's a textbox.
Im currently expressing some values in the textbox, and I want to click the textbox and form 2 to appear, which haves 4 option to change the value into.
listBox2.Items.Add(" 1 " + seatArray[0].PrintInfo);
listBox2.Items.Add(" 2 " + seatArray[1].PrintInfo);
listBox2.Items.Add(" 3 " + seatArray[2].PrintInfo);
listBox2.Items.Add(" 4 " + seatArray[3].PrintInfo);
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string test = listBox2.SelectedItem.ToString().Substring(0,2);
int num = int.Parse(test);
textBox1.Text = test;//Row
textBox2.Text = seatArray[num-1].A.ToString();
textBox7.Text = seatArray[num-1].B.ToString();
textBox5.Text = seatArray[num-1].C.ToString();
textBox3.Text = seatArray[num-1].D.ToString();
textBox6.Text = seatArray[num-1].E.ToString();
textBox4.Text = seatArray[num-1].F.ToString();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
when I click and item on my listbox, it summons the form 2, but I need for it to open when I click the textbox itself.

You need an event handler for the click-event of the textbox:
private void NameOfYourTextBox_Click(oject sender, EventArgs e)
{
var secondForm = new form2();
secondForm.Show();
}
Normally you would use
secondForm.ShowDialog();
to provide a modal form and wrap everything inside an using:
private void NameOfYourTextBox_Click(oject sender, EventArgs e)
{
using (var secondForm = new form2())
{
var result = secondForm.ShowDialog();
// result contains informations on what user did with the form
}
}
because this disposes the form-instance after usage.
So where in this chain is your gap?

place in textbox click event
private void textBox1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}

Related

I want add data from datagridview to another form - not display data

I have form5 and form6 and use these code to display data from datagridview to textbox
private void button1_Click(object sender, EventArgs e)
{
Form6 FRM = new Form6();
FRM.ShowDialog();
}
private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Form5 frm = new Form5();
frm.textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
frm.Show();
}
My question is: I want to add data to textbox not display data.
For example I have in textbox1 in form5 'mahmoud' I want to when action double click in datagridview to add 'johan' next to mahmoud
mahmoud,johan,jjjj,kkkk,jjjj,ahaha
try below codes
string name = dataGridView1.CurrentRow.Cells[1].Value.ToString();
frm.txtCustomer.Text = name;
frm.Show();
OR
StringBuilder textvalues = new StringBuilder();
textvalues.Append(dataGridView1.CurrentRow.Cells[1].Value.ToString());
frm.Show()
Why not this?
string selectedText;
private void dGV1_doubleClick(....){
selectedText= dataGridView1.SelectedCells[0].Value.ToString();
frm5.TextBox1.Text+= ","+selectedText;
frm5.BringToFront(); //I suppose the form is opened.
}

c# add rows from another form

I got 2 forms, Form1 contains the datagridview and button "add", Form2 contains the textboxs and button "save",
I want to add row and form2 appears when add button is clicked, then save informations from form2 in the datagridview when save button is clicked
this is the code i'm using for both add and save buttons, but when i do, it only saves informations wrote from form1 (save button doesn't do much if not updating datagridview)
private void AddButton_Click(object sender, EventArgs e)
{
Form2 windowAdd = new Form2();
windowAdd.SetDesktopLocation(this.Location.X + this.Size.Width, this.Location.Y);
windowAdd.ShowDialog();
var frm2 = new Form2();
frm2.AddGridViewRows(textName.Text, textDescription.Text, textLocation.Text, textAction.Text);
textName.Focus();
this.stockData.Product.AddProductRow(this.stockData.Product.NewProductRow());
productBindingSource.MoveLast();
}
private void SaveButton_Click(object sender, EventArgs e)
{
productBindingSource.EndEdit();
productTableAdapter.Update(this.stockData.Product);
this.Close();
}
Try this approach.
Form2 could accept some parameters for construction. You will have to resolve the reference to the productBindingSource and productDataAdaptor.
public partial class Form2 : Form
{
private DataRow _theRow;
private bool _isNew;
public Form2(DataRow theRow, bool isNew)
{
InitializeComponent();
_theRow = theRow;
_isNew = isNew;
}
private void Form2_Load(object sender, EventArgs e)
{
textName.Text = _theRow["Name"];
// Etc
}
private void btnSave_Click(object sender, EventArgs e)
{
// This is your add / edit record save button
// Here you would do stuff with your textbox values on form2
// including validation
if (!ValidateChildren()) return;
if (_isNew)
{
// Adding a record
productBindingSource.EndEdit();
productTableAdapter.Update();
}
else
{
// Editing a record
}
this.Close();
}
}
This changes your call in the Form1.Add Button event. I have shown below the way to utilize a using block to show a form.
private void btnAdd_Click(object sender, EventArgs e)
{
DataRow newRow = stockData.Product.NewProductRow();
using (var addForm = new Form2(newRow, true))
{
addForm.StartPosition = FormStartPosition.CenterParent;
addForm.ShowDialog(this);
// Here you could access any public method in Form2
// You could check addForm.DialogResult for the status
}
}
This isn't the best way to do this, but this direction might be a easier way to try...
Hope it helps

C# - Form2 value to Form1

I'm having trouble passing values ​​entered in form2(citacao) to form1(principal).
Principal.cs (form1)
richEditControl1.Document.AppendText(citacao.valor_edit[0]);
Citacao.cs (form2)
public string[] valor_edit = new string[3];
private void simpleButton2_Click(object sender, EventArgs e)
{
valor_edit[0] = memoEdit1.Text;
valor_edit[1] = comboBox1.SelectedItem.ToString();
valor_edit[2] = textEdit1.Text;
}
But when I click the button nothing happens , the values ​​are not inserted into the richedit I like it.
I already have this on form (Pass DataGrid to ComboBox)
Form1 (principal)
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
citacao cita = new citacao(this);
cita.Show();
}
form2(citação)
public citacao(principal gridForm)
{
InitializeComponent();
frm1 = gridForm;
}
// LOAD ALL FONTS (Referencias);
private void citacao_Load(object sender, EventArgs e)
{
comboBox1.Items.Clear();
foreach (DataGridViewRow row in frm1.DataGridView1.Rows)
{
comboBox1.Items.Add(row.Cells[0].Value.ToString());
}
comboBox1.SelectedIndex = 0;
}
let's see whether I understood your situation :)
declare your variable in Form 1 as a class variable
private citacao cita;
then initialize it in the button press event
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
cita = new citacao(this);
// subscribe to the closing event
cita.FormClosing += form_FormClosing;
cita.Show();
}
// when Form 2 will be closed you can execute your important line in the event
void form_FormClosing(object sender, FormClosingEventArgs e)
{
// BUT! you have to use the variable name!
richEditControl1.Document.AppendText(cita.valor_edit[0]);
}
EDIT:
Ok after looking at the entire code:
please remove the button3! and this entire code:
private void button3_Click(object sender, EventArgs e)
{
cita = new citacao(this);
richEditControl1.Document.AppendText(citacao.valor_edit); // this line is the problem!
}
The function AppendText probably needs a string as parameter and you give the entire array!
If you subscribe to the closing event in Form1 / principal and also implement
the event, your data will be transmitted automatically as soon as the Form 2 disappears from the screen :)

ShowDialog issue while opening form

I have 2 forms
Form1
Form2
I have one button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.ShowDialog();
f2.Dispose();
}
but issue is while opening form it's bliking and diasparing
i have tried to use show() also but not solved the problem
If i have not used Disposed method then first time when run the form it appering and disappered but sencond time onward by clicking on button it's working fine...
In Form2_Load event i am using this two property
private void Form2_Load(object sender, EventArgs e)
{
this.RightToLeft = RightToLeft.Yes;
this.RightToLeftLayout = true;
}
Don't change the form layout while its loading. Change it before you launch. Remove the code from Form2_Load and put it in button1_Click:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.RightToLeft = RightToLeft.Yes;
f2.RightToLeftLayout = true;
f2.ShowDialog();
}
I would guess you want to show and close the form2 using the same button. And I doubt your initial problem description
"issue is while opening form it's bliking and diasparing"
I think form2 is not 'blinking' while opening, but is 'blinking' while you try to click the button again in form1
ShowDialog() will exit your execution after u called it. Mean, it will exit the execution after you click the button.
Thus, you should try Show() with conditional statement within the button click event
In form1.cs
bool flag = false;
Form2 frm2;
private void button1_Click(object sender, EventArgs e)
{
if (flag == false)
{
frm2 = new Form2();
frm2.Show();
frm2.Load += new EventHandler(frm2_Load);
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
flag = true;
}
else
{
frm2.Close();
flag = false;
}
}
void frm2_Load(object sender, EventArgs e)
{
//set what ever properties you like
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
flag = false;
}
See also: A dialog disables all of the windows that your program displays
Remove this Property
this.RightToLeft = RightToLeft.Yes;
and run your form...
Try This :
private void button1_Click(object sender, EventArgs e)
{
using(Form2 f2 =new Form2())
{
f2.ShowDialog();
}
}

Control properties don't allow to be changed

My mainForm contains two buttons(btnLoad & btnChange) and a panel
When the btnLoad is clicked, it loads the other forms(there are 5 different froms with different controlers) into the panel. Let me assume one of it named Form2 which contains a label(labelMessage)
My problem is, when I click the btnChange the following statement won't work.
f2.labelMessage.Text = "Button Change Clicked";
My codes are
// codes on mainFrom
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = new From2();
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
private void btnLoad_Click(object sender, EventArgs e)
{
panelDock.Controls.Clear();
Form f2 = new Form2();
f2.TopLevel = false;
panelDock.Controls.Add(f2);
f2.Show();
}
is this wrong?
Since Form2 is already shown you should use Application.OpenForms instead of creating a new instance of Form2
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)Application.OpenForms["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
From your comment that Form2 is in a panel you can try
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)panel1.Controls["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}

Categories

Resources