When tabpage add controls like form, how set form owner - c#

when i individual add two forms to two pages controls,
how set form owner?
Because owner is empty, could not set data from form's event to another form's textbox text, how slove it, please help me .
-main form-
//tabpage add controls
private void TestForm2_Shown(object sender, EventArgs e)
{
//var DataUpdate = new DataUpdate();
var DataUpdate = new TestForm3();
DataUpdate.TopLevel = false;
//DataUpdate.Visible = true;
//DataUpdate.Top = 0;
//DataUpdate.Left = 0;
DataUpdate.Dock = DockStyle.Fill;
DataUpdate.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
tabPage1.Controls.Add(DataUpdate);
//var SystemSetting = new SystemSetting();
var SystemSetting = new TestForm4();
SystemSetting.TopLevel = false;
//SystemSetting.Visible = true;
//SystemSetting.Top = 0;
//SystemSetting.Left = 0;
SystemSetting.Dock = DockStyle.Fill;
SystemSetting.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
tabPage2.Controls.Add(SystemSetting);
SystemSetting.Show();
}

// In your TestForm3,
private void button1_Click(object sender, EventArgs e)
{
TestForm4 frm = new TestForm4();
frm.owner=this;
frm.Show();
}
// In your TestForm4,
private void button1_Click(object sender, EventArgs e)
{
TestForm3 mainForm=(TestForm3)this.owner;
mainForm.LabelText = textBox1.Text;
}

Related

Switch form in panel

I'm trying to create a panel with somes button when you click on it, it show specific form inside the panel. The panel is blocked on the first form showed even when you try to remove it.
private void button1_Click(object sender, EventArgs e)
{
Settings settings = new Settings();
Dashboard dashboard = new Dashboard();
dashboard.TopLevel = false;
panel1.Controls.Add(dashboard);
panel1.Controls.Remove(settings);
dashboard.FormBorderStyle = FormBorderStyle.None;
dashboard.Dock = DockStyle.Fill;
dashboard.Show();
settings.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
Settings settings = new Settings();
Dashboard dashboard = new Dashboard();
settings.TopLevel = false;
panel1.Controls.Remove(dashboard);
panel1.Controls.Add(settings);
settings.FormBorderStyle = FormBorderStyle.None;
settings.Dock = DockStyle.Fill;
settings.Show();
dashboard.Hide();
}

How to reset pivotGridControl settings (DevExpress)?

Сannot update form butReset_Click does not work RestoreLayoutFromRegistry from another form. How to do it right? I created one form from another and it is necessary to reset the pivotGridControl settings from 2 forms. I can't reset the settings.
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
butReset.Click += new System.EventHandler(butReset_Click);
pivotGridControl1.SaveLayoutToRegistry(regKey);
}
string regKey = "DevExpress\\XtraPivotGrid\\Layouts\\PivotGridLayout";
public Button butLoad = new Button();
public Button butReset = new Button();
public void LoadBtn_Click(object sender, EventArgs e)
{
Form1 fr = new Form1();
Form form = new Form();
form.Show();
ListBox listBox1 = new ListBox();
listBox1.Size = new System.Drawing.Size(200, 100);
listBox1.Location = new System.Drawing.Point(10, 10);
form.Controls.Add(listBox1);
listBox1.MultiColumn = true;
listBox1.SelectionMode = SelectionMode.MultiExtended;
DirectoryInfo dir = new DirectoryInfo(#"E:\");
FileInfo[] files = dir.GetFiles("*.txt");
butReset.Text = "Сбросить настройки";
butReset.Location = new Point(140, 160);
form.Controls.Add(butReset);
butLoad.Text = "Принять";
butLoad.Location = new Point(30, 160);
form.Controls.Add(butLoad);
var fileNamesWithoutExtension = files
.Select(fi => Path.GetFileNameWithoutExtension(fi.Name));
foreach (string fn in fileNamesWithoutExtension)
{
listBox1.Items.Add(fn.ToString());
}
}
private void SaveBtn_Click(object sender, EventArgs e)
{
pivotGridControl1.RestoreLayoutFromRegistry(regKey);
}
static public void butReset_Click(object sender, EventArgs e)
{
Form1 er = new Form1();
er.pivotGridControl1.RestoreLayoutFromRegistry(er.regKey);
//MessageBox.Show("dsdsdfsdf");
}
}
}
Use PivotGridOptionsDataField.Reset() method to Resets all options to their default values. In your case
static public void butReset_Click(object sender, EventArgs e)
{
pivotGridControl1.Reset();
}
Reference document: https://docs.devexpress.com/CoreLibraries/DevExpress.XtraPivotGrid.PivotGridOptionsDataField.Reset

Switch forms with tab control

I am a beginner in C#, so please bear with me. I have a form where I have a tab control, this tab control has 3 tabs. I am trying to show a form with each tab page. I am able to show a form on the first tab, but for some reason, the other two tabs do not load the forms. This is the code that I have. Am I doing something wrong? Do you have any suggestions?
private void MainTab_Load(object sender, EventArgs e)
{
HomeScreen fHome = new HomeScreen();
fHome.TopLevel = false;
fHome.Visible = true;
fHome.FormBorderStyle = FormBorderStyle.None;
fHome.Dock = DockStyle.Fill;
MainOptions.TabPages[0].Controls.Add(fHome);
}
private void CustomerTab_Click(object sender, EventArgs e)
{
CustomerScreen fCustomer = new CustomerScreen();
fCustomer.TopLevel = false;
fCustomer.Visible = true;
fCustomer.FormBorderStyle = FormBorderStyle.None;
fCustomer.Dock = DockStyle.Fill;
MainOptions.TabPages[1].Controls.Add(fCustomer);
}
edit:
More:
Also, in the tabcontrol- InitializeComponent I have the following
// HomeTab
//
this.HomeTab.Location = new System.Drawing.Point(4, 22);
this.HomeTab.Name = "HomeTab";
this.HomeTab.Size = new System.Drawing.Size(677, 452);
this.HomeTab.TabIndex = 0;
this.HomeTab.Text = "Home";
this.HomeTab.UseVisualStyleBackColor = true;
this.HomeTab.Click += new System.EventHandler(this.MainTab_Load);
//
// CustomerTab
//
this.CustomerTab.Location = new System.Drawing.Point(4, 22);
this.CustomerTab.Name = "CustomerTab";
this.CustomerTab.Padding = new System.Windows.Forms.Padding(3);
this.CustomerTab.Size = new System.Drawing.Size(677, 452);
this.CustomerTab.TabIndex = 1;
this.CustomerTab.Text = "Customer";
this.CustomerTab.UseVisualStyleBackColor = true;
this.CustomerTab.Click += new System.EventHandler(this.CustomerTab_Click);
I think you're just missing the Show() method...
And a suggestion: I don't know what kind of Pattern you're using, but as a beginner, you should declare your forms outside those methods, as a variable within the class scope, so you can access then later...
private HomeScreen fHome = new HomeScreen();
private CustomerScreen fCustomer = new CustomerScreen();
private void MainTab_Load(object sender, EventArgs e)
{
fHome.TopLevel = false;
fHome.Visible = true;
fHome.FormBorderStyle = FormBorderStyle.None;
fHome.Dock = DockStyle.Fill;
MainOptions.TabPages[0].Controls.Add(fHome);
fHome.Show(); // add this
}
private void CustomerTab_Click(object sender, EventArgs e)
{
fCustomer.TopLevel = false;
fCustomer.Visible = true;
fCustomer.FormBorderStyle = FormBorderStyle.None;
fCustomer.Dock = DockStyle.Fill;
MainOptions.TabPages[1].Controls.Add(fCustomer);
fCustomer.Show(); // add this
}
There's a lot to improve here, but that's a start.

Create a button which creates button

I am a begginer in C# and I want to create a button which creates button.
but these buttons never appear...
please find my code :
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
}
Thanks a lot
You have to add the button (or any other controls) on the form using the Controls property, for sample:
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
// add control
this.Controls.Add(addstrat3_2);
}
You need to add the button to the form.
this.Controls.Add(addstrat3_2);

Refresh listbox after adding

I have a listbox in C# and want it to refresh after I added a new item(which gets opened with a new form dialog)
Here is my code which doesn't work.
private void showAllItems()
{
itemList = Db.getAllItems();
lb_itemList.DataSource = itemList;
}
private void showItemPreview(object sender, EventArgs e)
{
string curItem = lb_itemList.SelectedItem.ToString();
briefPreviewList = Db.getItemBriefPreview(curItem);
string itemInfos = string.Join(",", briefPreviewList.ToArray());
string[] infos = itemInfos.Split(',');
l_itemDB.Text = curItem;
l_CategoryDB.Text = infos[0];
}
private void b_addItem_Click(object sender, EventArgs e)
{
int uid = 1;
AddItem addItemForm = new AddItem(uid);
addItemForm.ShowDialog();
CurrencyManager cm = (CurrencyManager)BindingContext[itemList];
cm.Refresh();
}
I assume when you insert a new item it gets stored into the database, if this is the case then all you need to do is reset the datasource:
private void b_addItem_Click(object sender, EventArgs e)
{
int uid = 1;
AddItem addItemForm = new AddItem(uid);
addItemForm.ShowDialog();
addItemForm.Dispose();
this.showAllItems();
}

Categories