Here are all my classes.
MainForm = listview,
CustomerFrame = textboxes
When I compile my program, my MainForm appears with an empty listview, and when I press on the add button to insert an item, my CustomerFrame class appears. When writing in the textboxes and clicking ok, no item inserted in my listview (MainForm). Why?
Some code:
MainForm
using(var customerframe = new CustomerFrame())
{
if (customerframe.DialogResult == DialogResult.OK)
{
CustomerFiles.Contact contact = customerframe.GetContact();
CustomerFiles.Address address = customerframe.GetAddress();
CustomerFiles.Phone phone = customerframe.GetPhone();
CustomerFiles.Email email = customerframe.GetEmail();
//Items in my listview
listviewitem = new ListViewItem();
listviewitem.SubItems.Add(contact.FirstName);
listviewitem.SubItems.Add(contact.LastName);
listviewitem.SubItems.Add(phone.Home);
listviewitem.SubItems.Add(phone.Mobile);
listviewitem.SubItems.Add(address.Country);
listviewitem.SubItems.Add(address.ZipCode);
listviewitem.SubItems.Add(address.City);
listviewitem.SubItems.Add(address.Street);
listviewitem.SubItems.Add(email.Personal);
this.listView1.Items.Add(listviewitem);
}
}
MainForm
private void addToolStripMenuItem_Click_1(object sender, EventArgs e)
{
customerframe.Show();
CustomerManager cm = new CustomerManager();
}
CustomerFrame
private void btnOk_Click(object sender, EventArgs e)
{
MainForm main = new MainForm();
DialogResult = DialogResult.OK;
}
By the way, when I use
if (customerframe.ShowDialog() == DialogResult.OK)
this will make the CustomerFrame form appear before the MainForm (which I don't want) and it will insert item, but only once.
Why do you open ANOTHER main form from DialogBox? I think that you should remove this line.
MainForm main = new MainForm();
And add this
DialogResult = DialogResult.OK;
Close();
Argh, to simplify - code in ButtonOK should look like this:
private void btnOk_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
EDIT: response to new problem
First create new CustomerFrame, show it and wait for it to close; then transfer new data to your ListView. I believe that your add handler should look like this:
private void addToolStripMenuItem_Click_1(object sender, EventArgs e)
{
using(var customerframe = new CustomerFrame())
{
// I don't know what this line does
CustomerManager cm = new CustomerManager();
if (customerFrame.ShowDialog() == DialogResult.OK)
{
CustomerFiles.Contact contact = customerframe.GetContact();
CustomerFiles.Address address = customerframe.GetAddress();
CustomerFiles.Phone phone = customerframe.GetPhone();
CustomerFiles.Email email = customerframe.GetEmail();
//Items in my listview
listviewitem = new ListViewItem();
listviewitem.SubItems.Add(contact.FirstName);
listviewitem.SubItems.Add(contact.LastName);
listviewitem.SubItems.Add(phone.Home);
listviewitem.SubItems.Add(phone.Mobile);
listviewitem.SubItems.Add(address.Country);
listviewitem.SubItems.Add(address.ZipCode);
listviewitem.SubItems.Add(address.City);
listviewitem.SubItems.Add(address.Street);
listviewitem.SubItems.Add(email.Personal);
this.listView1.Items.Add(listviewitem);
}
}
}
Related
My problem is that I have inserted a tab to a tabControl and added a Form to it (basically I wanted to display all the forms I was going to open as tabs).
Code for adding form to tabControl as tab in the Main class:
private void new_form_Click(object sender, EventArgs e)
{
add = new Add(null);
add.TopLevel = false;
add.Visible = true;
add.FormBorderStyle = FormBorderStyle.None;
add.Dock = DockStyle.Fill;
var tabIndex = tabControl1.TabCount;
tabControl1.TabPages.Insert(tabIndex, "New Tab");
tabControl1.SelectedIndex = tabIndex - 1;
tabControl1.TabPages[tabIndex - 1].Controls.Add(add);
}
When I was using multiple forms it was easy to rename the title from the form class:
private void surname_Leave(object sender, EventArgs e)
{
this.Text = surname.Text;
}
How can I rename the tab programmatically from inside the added form?
Edit:
I know how to rename a tab from the same class. I need to rename the tab from the form I open in the tab.
What I was trying to accomplish I achieved by passing the reference of one to another form.
in Form2:
public Form2()
{
InitializeComponent();
}
private Form1 mainForm;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void surname_Leave(object sender, EventArgs e)
{
mainForm.setTabTitle = surname.Text;
}
in Form1:
private void new_form_Click(object sender, EventArgs e)
{
add = new Add(this); \\this part was missing
add.TopLevel = false;
add.Visible = true;
add.FormBorderStyle = FormBorderStyle.None;
add.Dock = DockStyle.Fill;
var tabIndex = tabControl1.TabCount;
tabControl1.TabPages.Insert(tabIndex, "New Tab");
tabControl1.SelectedIndex = tabIndex - 1;
tabControl1.TabPages[tabIndex - 1].Controls.Add(add);
}
public string setTabTitle
{
set { tabControl1.TabPages[tabControl1.SelectedIndex].Text = value; }
}
I'm novice in c# and winforms, my idea is to create a simple app , which contains Form1 like this on image
There is dategridview where I can show data from database table Products(ID is not visible),then when I click on button New, Form2 will open
I want to use same Form2 when I want to edit some of the items, selecting specific row in datagridview and clicking on button Edit(I've managed to do this on 2 different win forms).How can I achieve this ?
This is what I've done so far:
button New
private void toolStripButton1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
method PerformRefresh()
public void PerformRefresh()
{
this.productsTableAdapter.Fill(this.dbtestDataSet.products);
this.dataGridView1.Refresh();
}
Form2
Form1 _owner;
public Form2(Form1 owner)
{
InitializeComponent();
_owner = owner;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing);
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
_owner.PerformRefresh();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
string Name = textBox1.Text;
int Quantity = Int32.Parse(textBox2.Text);
MySqlConnection cnn = new MySqlConnection(Konekcija.cnndbtest);
MySqlCommand cmd = new MySqlCommand("INSERT INTO products (Name,Quantity) VALUES(#name, #quantity)", cnn);
cmd.Parameters.AddWithValue("#name", Name);
cmd.Parameters.AddWithValue("#quantity", Quantity);
try
{
cnn.Open();
cmd.ExecuteNonQuery();
cnn.Close();
}
catch (Exception xcp)
{
MessageBox.Show(xcp.Message);
return;
}
MessageBox.Show("Object has been successfully entered into the database", "Message");
this.Close();
}
then, for button Edit on Form1 I've made class MyProducts , and made object of this class ,which I want to pass to Form2
MyProducts myProducts = new MyProducts();
myProducts.ID = Int32.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
myProducts.Name = dataGridView1.CurrentRow.Cells[1].Value.ToString();
myProducts.Quantity = Int32.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());
Now I want to show Form2 clicking on button Edit , and when Form2 is shown, I want to have selected Item properties displayed into 2 textbox controls.
Any idea ?
Thanks in advance.
Give Form2 a public Property of MyProducts and fill the TextBoxes
private MyProducts product;
public MyProducts Product
{
set
{
product = value;
if (product != null)
{
nameTextBox.Text = product.Name;
quantityTextBox.Text = product.Quantity.ToString();
}
}
Then before opening the form for edit pass the MyProducts-Element you wanna edit
private void toolStripButton2_Click(object sender, EventArgs e)
{
MyProducts myProducts = new MyProducts();
myProducts.ID = Int32.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
myProducts.Name = dataGridView1.CurrentRow.Cells[1].Value.ToString();
myProducts.Quantity = Int32.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());
Form2 f2 = new Form2(this);
f2.Product = myProducts;
f2.Show();
}
then on safe check if product has value to determine update or insert
private void toolStripButton1_Click(object sender, EventArgs e)
{
string Name = textBox1.Text;
int Quantity = Int32.Parse(textBox2.Text);
if (product != null)
{
//SQL Update command
}
else
{
//SQL Insert command
}
//SQL Execute
}
hope this helps
I get the default path from the registry for the Steam installation. But if someone has their games installed to a different folder, the user has to enter it in the Configure form. When the form gets closed, that path entered(from the folder browser or by typing the path in manually) should get saved to a string in the main form and should enable a different Combobox which turns on different buttons. I somehow managed to do the save to the mainform string, but the 2nd combobox doesn't seem to turn on. How can I do it correctly?
** MAIN FORM **
public string NewPath { get; set; }
private ConfigForm otherForm;
string InstallPath = (string)Registry.GetValue(#"HKEY_CURRENT_USER\SOFTWARE\Valve\Steam", "SteamPath", null);
private void PortalHammerButton_Click(object sender, EventArgs e)
{
Process.Start(InstallPath + #"\SteamApps\common\Portal\bin\hammer.exe");
}
private void Gamedropdown_SelectedIndexChanged(object sender, EventArgs e)
{
if (Gamedropdown.Text == "Portal") // When Portal is selected
{
// Enable the Portal SDK buttons
PortalHammerButton.Visible = true;
PortalModelViewerButton.Visible = true;
PortalFacePoserButton.Visible = true;
// Disable the CS:GO SDK buttons
csgoFacePoserButton.Visible = false;
csgoHammerButton.Visible = false;
csgoModelViewerButton.Visible = false;
}
else if (Gamedropdown.Text == "CS:GO") // When CS:GO is selected
{
// Disable Portal SDK buttons
PortalHammerButton.Visible = false;
PortalModelViewerButton.Visible = false;
PortalFacePoserButton.Visible = false;
// Enable CS:GO SDK buttons
csgoFacePoserButton.Visible = true;
csgoHammerButton.Visible = true;
csgoModelViewerButton.Visible = true;
}
}
private void ConfigureButton_Click(object sender, EventArgs e)
{
var configdialog = new ConfigForm();
configdialog.Show();
}
private void PortalDifferentHammerButton_Click(object sender, EventArgs e)
{
Process.Start(NewPath + #"\SteamApps\common\Portal\bin\hammer.exe");
}
private void NewDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
if (NewDropDown.Text == "Portal") // When Portal is selected
{
// Enable the Portal SDK buttons
PortalDifferentHammerButton.Visible = true;
PortalDifferentModelViewerButton.Visible = true;
PortalDifferentFacePoserButton.Visible = true;
// Disable the CS:GO SDK buttons
DifferentCSGOFaceposerButton.Visible = false;
DifferentCSGOHammerButton.Visible = false;
DifferentCSGOModelViewerButton.Visible = false;
}
else if (NewDropDown.Text == "CS:GO") // When CS:GO is selected
{
// Disable the Portal SDK buttons
PortalDifferentFacePoserButton.Visible = false;
PortalDifferentHammerButton.Visible = false;
PortalDifferentModelViewerButton.Visible = false;
// Enable the CS:GO SDK buttons
DifferentCSGOModelViewerButton.Visible = true;
DifferentCSGOHammerButton.Visible = true;
DifferentCSGOFaceposerButton.Visible = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
ConfigForm cfgfrm = new ConfigForm();
cfgfrm.Close();
}
}
}
**CONFIGURE FORM**
public partial class ConfigForm : Form
{
public ConfigForm()
{
InitializeComponent();
Form1 frm1 = new Form1();
frm1.NewPath = NewPathBox.Text;
}
public void DifferentFolderBrowseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string newpath = fbd.SelectedPath;
NewPathBox.Text = newpath;
Form1 frm1 = new Form1();
frm1.NewPath = NewPathBox.Text;
}
public void CloseButton_Click(object sender, EventArgs e)
{
this.Hide();
Form1 frm1 = new Form1();
frm1.Gamedropdown.Visible = false;
frm1.NewDropDown.Visible = true;
}
}
}
Any help would be appriciated.
Look at your ConfigForm. Here's your problem:
public ConfigForm()
{
InitializeComponent();
Form1 frm1 = new Form1();
frm1.NewPath = NewPathBox.Text;
}
What you're doing on your Form1 (which I'm guessing is your Main form) is creating a new instance of your ConfigForm and showing it. What you're doing in your ConfigForm is creating a new main form and setting the NewPath = to the value entered on your config form. The problem is this new Form1 is NOT the Form1 that created the ConfigForm. The Form1 that created your config form is not the one getting updated by your code, some arbitrary new Form1 that you create is the one getting updated. This is why your code isn't working as you expected.
Here's the approach I would take. Add a NewPath variable to your ConfigForm just like you have in Form1. Then, add a FormClosing method to FormConfig. Do something like this:
private void ConfigForm_FormClosing(object sender, FormClosingEventArgs e)
{
NewPath = NewPathBox.Text;
}
Then, change your code on Form1 to this:
private void button1_Click(object sender, EventArgs e)
{
ConfigForm cfgfrm = new ConfigForm();
cfgfrm.ShowDialog();
this.NewPath = cfgfrm.NewPath;
}
What this code is doing is creating and showing a new ConfigForm on your Form1 when you click button1. Then, when your user closes the FormConfig, the form saves the textbox value to the NewPath variable on the FormConfig. Then, once the form is closed, the code on Form1 resumes. Form1 then looks at the NewPath value that was saved when the user closed the FormConfig. Form1 grabs this new NewPath value and puts it in its own NewPath variable.
EDIT
To show/hide comboboxes:
private void button1_Click(object sender, EventArgs e)
{
ConfigForm cfgfrm = new ConfigForm();
cfgfrm.ShowDialog();
this.NewPath = cfgfrm.NewPath;
Gamedropdown.Visible = false;
NewDropDown.Visible = true
}
I'm not sure why you are making the secondary form so complicated. You don't need a pointer to it after you use it and you should be closing instead of hiding. Try this:
class ConfigForm : Form
{
public string newPath = null;
public void CloseButton_Click(object sender, EventArgs e)
{
newPath = NewPathBox.Text;
}
public void CloseButton_Click(object sender, EventArgs e)
{
Close();
}
}
...and in your main form:
public partial class Form1 : Form
{
string steamPath = null; // set to starting path
private void ConfigureButton_Click(object sender, EventArgs e)
{
bool valueChanged = false;
using (ConfigForm form = new ConfigForm())
{
form.newPath = null;
form.ShowDialog();
if (form.newPath != null)
{
steamPath = form.newPath;
valueChanged = true;
}
}
if (valueChanged)
{
// here is where you would handle reloading and changing the ComboBoxes
}
}
}
This will more cleanly return the new string. Whatever you want to do as a result of having changed the path can be done after you have disposed of the config form (bringing it up with the "using" conditional automatically does the disposal for you)
I create a custom control inside a tab control which contains a flowLayoutPanel on every tab by dragging files on the selected tab. I have a context menu to rename and delete tab pages, but i want also to be able to delete a button created when I right click on it and select "remove"... I cannot find a way to delete only the selected button..
This is what I have to create the buttons:
public void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
foreach (string s in fileList)
{
var button = new Button();
path_app = String.Format("{0}", s);
string filename = path_app;
file_name = Path.GetFileName(path_app);
Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filename);
Bitmap bmp = icon.ToBitmap();
CustomControl custom_btn = new CustomControl(button, new Label { Text = file_name });
button.Tag = path_app;
button.BackgroundImage = bmp;
button.BackgroundImageLayout = ImageLayout.Stretch;
FlowLayoutPanel selectedFLP = (FlowLayoutPanel)tabControl1.SelectedTab.Controls[0];
selectedFLP.Controls.Add(custom_btn);
button.Click += new EventHandler(button_Click);
ContextMenu cm2 = new ContextMenu();
cm2.MenuItems.Add("Remove", new EventHandler(rmv_btn_click));
custom_btn.ContextMenu = cm2;
}
}
private void rmv_btn_click(object sender, System.EventArgs e)
{
foreach (Control X in fl_panel.Controls)
{
fl_panel.Controls.Remove(X);
}
}
How do I get the button which I right click on it as sender in the rmv_btn_click event to know which one to delete?
If I understand what you mean, You need to use something like this.
private void rmv_btn_click(object sender, System.EventArgs e)
{
fl_panel.Controls.Remove(sender as Button);
}
private void rmv_btn_click(object sender, System.EventArgs e)
{
Button btn = new Button();
Label lbl = new Label();
CustomControl cst_btn = new CustomControl(btn, lbl);
cst_btn = sender as CustomControl;
DialogResult dialogResult = MessageBox.Show("Are you sure that you want to remove this object?", "Remove object", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
cst_btn.Dispose();
}
else if (dialogResult == DialogResult.No)
{
//do nothing
}
}
public EventHandler handlerGetter(CustomControl button)
{
return (object sender, EventArgs e) =>
{
rmv_btn_click(button, e);
};
}
I have a form and I want to get an instance of the same form as stated in the code below. And I have a button: every time I press this button, if a new form is created, I want it to focus to that window, if not, I want to create a new form.
I managed to create a new form but if I want to focus on it, the code did not work, any ideas?
private void btn_Click(object sender, EventArgs e)
{
if (opened == false)
{
Text = "form1";
var form = new myformapp();
form.Show();
opened = true;
form.Text = "form2";
}
else
{
if (Application.OpenForms[1].Focused)
{
Application.OpenForms[0].BringToFront();
Application.OpenForms[0].Focus();
}
if (Application.OpenForms[0].Focused)
{
Application.OpenForms[1].BringToFront();
Application.OpenForms[1].Focus();
}
}
}
You can try shortening your code without the need to introduce more variables with this example:
void button1_Click(object sender, EventArgs e) {
bool found = false;
for (int i = 0; i < Application.OpenForms.Count; ++i) {
if (Application.OpenForms[i].GetType() == typeof(myformapp) &&
Application.OpenForms[i] != this) {
Application.OpenForms[i].Select();
found = true;
}
}
if (!found) {
myformapp form = new myformapp();
form.Show();
}
}
Updated code from Francesco Baruchelli's comment.
If I understand correctly what you are trying to do, you can keep a static List with the opened forms. Everytime an instance of your Form is opened you add it to the List, and everytime it is closed you remove it. The when you press the button you can check the size of the List. If it is 1 you create a new Form, open it and set the focus on it. If the size is already 2, you look in the List for the instance which is different from the one executing the click event. The code could be something like this:
private static List<Form1> openForms = new List<Form1>();
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = null;
if (openForms.Count == 2)
{
foreach (Form1 aForm in openForms)
if (aForm != this)
{
frm = aForm;
break;
}
}
else
{
frm = new Form1();
frm.Show();
}
frm.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
openForms.Add(this);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
openForms.Remove(this);
}