Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Text = "Button";
btn.ID = "Button1";
pnlMain.Controls.Add(btn);
Panel pnl = new Panel();
pnl.ID = "pnl";
Label lbl = new Label();
lbl.ID = "lbl";
lbl.Text = "Hi this is my Balloon popup";
pnl.Controls.Add(lbl);
pnlMain.Controls.Add(pnl);
BalloonPopupExtender balloonPopupExtender1= new BalloonPopupExtender();
balloonPopupExtender1.TargetControlID = btn.ID;
balloonPopupExtender1.BalloonPopupControlID = pnl.ID;
balloonPopupExtender1.BalloonSize = BalloonPopupSize.Small;
balloonPopupExtender1.BalloonStyle = BalloonPopupStyle.Rectangle;
balloonPopupExtender1.DisplayOnMouseOver = true;
balloonPopupExtender1.DisplayOnClick = true;
balloonPopupExtender1.DisplayOnFocus = false;
}
This code executes correctly but balloon popup does not show up...
I have got the Solution
Panel pnlBalloon = new Panel();
pnlBalloon.ID="pnlBalloon";
Label LblBalloon = new Label();
LblBalloon.ID="LblBalloon";
pnlBalloon.Controls.Add(LblBalloon);
pnl_Message.Controls.Add(pnlBalloon);
LblBalloon.Text = "This is Balloon Popup";
AjaxControlToolkit.BalloonPopupExtender BalloonPopupExtender1 = new AjaxControlToolkit.BalloonPopupExtender();
BalloonPopupExtender1.ID = "BalloonPopupExtender1";
BalloonPopupExtender1.TargetControlID = labelShow.ID;
BalloonPopupExtender1.BalloonPopupControlID = pnlBalloon.ID;
BalloonPopupExtender1.BalloonSize = AjaxControlToolkit.BalloonPopupSize.Small;
BalloonPopupExtender1.BalloonStyle = AjaxControlToolkit.BalloonPopupStyle.Rectangle;
BalloonPopupExtender1.Position = AjaxControlToolkit.BalloonPopupPosition.BottomRight;
BalloonPopupExtender1.DisplayOnClick = false;
BalloonPopupExtender1.DisplayOnMouseOver = true;
BalloonPopupExtender1.DisplayOnFocus = false;
pnlConnection.Controls.Add(BalloonPopupExtender1);
Related
Our app has a list of items displayed in a DataGridView. The first column is a DataGridViewCheckBoxColumn. We want our app to allow the user to click anywhere on the row as a way to select the CheckBox in the first column.
We find that if the user clicks directly on the CheckBox, selection/deselection works well. The same is true if the user clicks on the data in the other columns.
However, if the user clicks just to one side of the checkbox, we get strange behavior. The CheckBox in that row is not selected/deselected, but often another row is selected. To get a clearer picture of what is happening, you can check out my Short Video of the buggy behavior.
I tried setting some breakpoints in the code, for example, on our SelectionChanged handler, our CellClick handler and our CellValueChanged handler. I find these breakpoints are hit in the same pattern, regardless of whether I click on the CheckBox, just to one side of the checkbox or on the data in the other columns.
Has anyone seen behavior like this? Any ideas what may be going on? Is it a bug in the .NET DataGridView code, or is there something I should look for in our code?
Here is the relevant code, as requested (or you can download a ZIP file with the complete solution)...
From Form1.cs:
public Form1()
{
InitializeComponent();
dgsControl.SetUp();
}
From Form1.Designer.cs:
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.dgsControl = new DGSelection();
this.Controls.Add(this.dgsControl);
//
// dgsControl
//
this.dgsControl.Dock = System.Windows.Forms.DockStyle.Fill;
this.dgsControl.Location = new System.Drawing.Point(3, 3);
this.dgsControl.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
this.dgsControl.Name = "dgsControl";
this.dgsControl.Size = new System.Drawing.Size(689, 325);
this.dgsControl.TabIndex = 0;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "DataGridView Demo";
}
From DGSelection.cs:
public partial class DGSelection : UserControl
{
#region Member variables
private class ListData
{
public string Option;
public string Description;
}
private static readonly List<ListData> TestData = new List<ListData>
{
new ListData { Option = "Option1", Description = "Description1" },
new ListData { Option = "Option2", Description = "Description2" },
new ListData { Option = "Option3", Description = "Description3" },
new ListData { Option = "Option4", Description = "Description4" }
};
public event EventHandler OptionsChanged;
#endregion
#region Constructor
public DGSelection()
{
InitializeComponent();
dgvTable.BackgroundColor = Color.DarkGray;
dgvTable.DefaultCellStyle.BackColor = Color.DarkGray;
dgvTable.DefaultCellStyle.ForeColor = Color.Black;
dgvTable.ColumnHeadersDefaultCellStyle.BackColor = Color.DarkGray;
dgvTable.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
dgvTable.GridColor = Color.DarkGray;
cbxCheckAll.BackColor = Color.DarkGray;
// Move label where it belongs (moved elsewhere in Designer for ease of editing).
lbl_empty.Top = Top + 5;
}
#endregion
#region Public Methods
public void SetUp()
{
dgvTable.Rows.Clear();
cbxCheckAll.Checked = false;
bool anyRows = TestData.Any();
lbl_empty.Visible = !anyRows;
cbxCheckAll.Visible = anyRows;
dgvTable.ColumnHeadersVisible = anyRows;
foreach (ListData ld in TestData)
{
dgvTable.Rows.Add(false, ld.Option, ld.Description);
}
}
#endregion
#region Event Handlers
private void DGSelection_SelectionChanged(object sender, EventArgs e)
{
dgvTable.ClearSelection();
}
private void cbxCheckAll_CheckedChanged(object sender, EventArgs e)
{
try
{
dgvTable.CellValueChanged -= DgvTableCellValueChanged;
bool checkAll = cbxCheckAll.Checked;
foreach (DataGridViewRow row in dgvTable.Rows)
row.Cells[0].Value = checkAll;
}
finally
{
dgvTable.CellValueChanged += DgvTableCellValueChanged;
}
OptionsChanged?.Invoke(this, EventArgs.Empty);
}
private void DGSelection_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
return; // Ignore clicks in the header row
DataGridViewCell checkBoxCell = dgvTable.Rows[e.RowIndex].Cells[0];
checkBoxCell.Value = !(bool)checkBoxCell.Value;
}
private void DgvTableCellValueChanged(object sender, DataGridViewCellEventArgs e)
{
try
{
cbxCheckAll.CheckedChanged -= cbxCheckAll_CheckedChanged;
cbxCheckAll.CheckedChanged -= cbxCheckAll_CheckedChanged;
// Not sure why, but sometimes subscribed twice
bool checkAll = dgvTable.Rows.Count > 0;
foreach (DataGridViewRow row in dgvTable.Rows)
checkAll &= row.Cells[0].Value.Equals(true);
cbxCheckAll.Checked = checkAll;
}
finally
{
cbxCheckAll.CheckedChanged += cbxCheckAll_CheckedChanged;
}
OptionsChanged?.Invoke(this, EventArgs.Empty);
}
private void DGSelection_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
dgvTable.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
#endregion
}
From DGSelection.Designer.cs:
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
this.dgvTable = new System.Windows.Forms.DataGridView();
this.colCheckboxes = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.colText1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.colText2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.cbxCheckAll = new System.Windows.Forms.CheckBox();
this.lbl_empty = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dgvTable)).BeginInit();
this.SuspendLayout();
//
// dgvTable
//
this.dgvTable.AllowUserToAddRows = false;
this.dgvTable.AllowUserToDeleteRows = false;
this.dgvTable.AllowUserToResizeColumns = false;
this.dgvTable.AllowUserToResizeRows = false;
this.dgvTable.AutoSizeRowsMode = System.Windows.Forms.DataGridViewAutoSizeRowsMode.AllCells;
this.dgvTable.BackgroundColor = System.Drawing.SystemColors.Window;
this.dgvTable.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.dgvTable.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
this.dgvTable.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.ControlDark;
dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
dataGridViewCellStyle1.Padding = new System.Windows.Forms.Padding(3);
dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
dataGridViewCellStyle1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
this.dgvTable.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
this.dgvTable.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvTable.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.colCheckboxes,
this.colText1,
this.colText2 });
this.dgvTable.Dock = System.Windows.Forms.DockStyle.Fill;
this.dgvTable.EditMode = System.Windows.Forms.DataGridViewEditMode.EditOnEnter;
this.dgvTable.EnableHeadersVisualStyles = false;
this.dgvTable.Location = new System.Drawing.Point(0, 0);
this.dgvTable.Margin = new System.Windows.Forms.Padding(2);
this.dgvTable.MultiSelect = false;
this.dgvTable.Name = "dgvTable";
this.dgvTable.RowHeadersVisible = false;
this.dgvTable.RowTemplate.Height = 24;
this.dgvTable.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.dgvTable.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
this.dgvTable.Size = new System.Drawing.Size(484, 318);
this.dgvTable.TabIndex = 0;
this.dgvTable.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.DGSelection_CellClick);
this.dgvTable.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.DgvTableCellValueChanged);
this.dgvTable.CurrentCellDirtyStateChanged += new System.EventHandler(this.DGSelection_CurrentCellDirtyStateChanged);
this.dgvTable.SelectionChanged += new System.EventHandler(this.DGSelection_SelectionChanged);
//
// colCheckboxes
//
this.colCheckboxes.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.None;
this.colCheckboxes.Frozen = true;
this.colCheckboxes.HeaderText = "";
this.colCheckboxes.Name = "colCheckboxes";
this.colCheckboxes.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.colCheckboxes.Width = 30;
//
// colText1
//
this.colText1.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.AllCells;
dataGridViewCellStyle2.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
this.colText1.DefaultCellStyle = dataGridViewCellStyle2;
this.colText1.HeaderText = "Option";
this.colText1.Name = "colText1";
this.colText1.ReadOnly = true;
this.colText1.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.colText1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
this.colText1.Width = 57;
//
// colText2
//
this.colText2.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
dataGridViewCellStyle3.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
this.colText2.DefaultCellStyle = dataGridViewCellStyle3;
this.colText2.HeaderText = "Description";
this.colText2.Name = "colText2";
this.colText2.ReadOnly = true;
this.colText2.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.colText2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
//
// cbxCheckAll
//
this.cbxCheckAll.AutoSize = true;
this.cbxCheckAll.BackColor = System.Drawing.SystemColors.ControlDark;
this.cbxCheckAll.Location = new System.Drawing.Point(8, 5);
this.cbxCheckAll.Margin = new System.Windows.Forms.Padding(2);
this.cbxCheckAll.Name = "cbxCheckAll";
this.cbxCheckAll.Size = new System.Drawing.Size(15, 14);
this.cbxCheckAll.TabIndex = 1;
this.cbxCheckAll.UseVisualStyleBackColor = false;
this.cbxCheckAll.CheckedChanged += new System.EventHandler(this.cbxCheckAll_CheckedChanged);
//
// lbl_empty
//
this.lbl_empty.Anchor = ((System.Windows.Forms.AnchorStyles)
(((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Left) |
System.Windows.Forms.AnchorStyles.Right)));
this.lbl_empty.BackColor = System.Drawing.Color.Transparent;
this.lbl_empty.Location = new System.Drawing.Point(3, 25);
this.lbl_empty.Name = "lbl_empty";
this.lbl_empty.Size = new System.Drawing.Size(478, 44);
this.lbl_empty.TabIndex = 2;
this.lbl_empty.Text = "No data defined for the list";
this.lbl_empty.Visible = false;
//
// DGSelection
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.lbl_empty);
this.Controls.Add(this.cbxCheckAll);
this.Controls.Add(this.dgvTable);
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "DGSelectionControl";
this.Size = new System.Drawing.Size(484, 318);
((System.ComponentModel.ISupportInitialize)(this.dgvTable)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
Is there something in our code which is causing this behavior? Or is it a bug in the implementation of DataGridView? I have reproduced this with both .NET Framework v.4.6 & v.4.8.
(Note: reposted from Microsoft Q&A Forum, since I got no responses there.)
I suggest these changes (tested with .Net Framework 4.8):
Don't use the CheckBox CheckedChanged event: it will interfere with the CellValueChanged event when it tries to change the Select All CheckBox state. Use the CellClick event instead.
This will also allow to get rid of all those add handler / remove handler things.
Call RefreshEdit() to update the state of the CheckBox Cell as soon as the Cell is clicked: this will update the CheckBox value immediately (this is the problem you're seeing when clicking inside the Cell's area instead of the CheckBox content: the control is not updated right away).
For more details, see the notes here:
Programmatically check a DataGridView CheckBox that was just unchecked
Remove that CommitEdit(DataGridViewDataErrorContexts.Commit);: if you need to update a value immediately, call the DataGridView.EndEdit() method instead (see those notes on this, too). It's more or less the same thing under the hood, but the name itself - EndEdit - makes its functionality much more understandable and it's easier to remember.
This is how it works now:
private void cbxCheckAll_Click(object sender, EventArgs e)
{
if (dgvTable.Rows.Count == 0) return;
try {
bool checkAll = cbxCheckAll.Checked;
foreach (DataGridViewRow row in dgvTable.Rows) {
row.Cells[0].Value = checkAll;
}
}
finally {
OptionsChanged?.Invoke(this, EventArgs.Empty);
}
}
private void DGSelection_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0) return;
bool currentValue = (bool)dgvTable[0, e.RowIndex].Value;
dgvTable[0, e.RowIndex].Value = !currentValue;
}
private void DgvTableCellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (!dgvTable.IsHandleCreated) return;
cbxCheckAll.Checked = dgvTable.Rows.OfType<DataGridViewRow>().All(r => (bool)r.Cells[0].Value == true);
dgvTable.BeginInvoke(new Action(() => dgvTable.RefreshEdit()));
OptionsChanged?.Invoke(this, EventArgs.Empty);
}
I am creating dynamic textboxes when clicking on a set of different radio button. Below is an example of two radio button onclick event.
protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
{
string servicename = "service1";
if (checkbox1.Checked)
{
InputParameters.InputParameters aa= new InputParameters.InputParameters();
textbox = aa.GetInputFields(servicename);
for (int i=0;i<textbox.Count;i++)
{
// declare a textbox
TextBox CPDT = new TextBox();
CPDT.ID = servicename + i.ToString();
CPDT.CssClass = "form-control";
CPDT.EnableViewState = true;
Label lblCPD=new Label();
lblCPD.ID = "txtDynamiclbl" + servicename+ i.ToString();
lblCPD.CssClass= "form-control-label";
lblCPD.Text= textbox[i].ToString();
lblCPD.EnableViewState = true;
CPDPlaceHolder.Controls.Add(lblCPD);
CPDPlaceHolder.Controls.Add(CPDT);
//this.NumberOfControls++;
}
Button callSoap = new Button();
callSoap.ID = "txtDynamicSearch" + servicename;
callSoap.Text = "Search";
callSoap.CssClass = ".btn-info";
callSoap.CommandArgument = "test";
callSoap.Click += new EventHandler(btnsoap);
callSoap.EnableViewState = true;
CPDPlaceHolder.Controls.Add(callSoap);
}
else
{
}
}
protected void Checkbox2_CheckedChanged(object sender, EventArgs e)
{
string servicename = "service2";
if (checkbox2.Checked)
{
InputParameters.InputParameters aa = new InputParameters.InputParameters();
List<String> textbox = aa.GetInputFields("test1");
// textboxs.AddRange(textbox);
for (int i = 0; i < textbox.Count; i++)
{
// declare a textbox
TextBox CPDT = new TextBox();
CPDT.ID = servicename + i.ToString();
CPDT.CssClass = "form-control";
Label lblCPD = new Label();
lblCPD.ID = "txtDynamiclbl" + servicename + i.ToString();
lblCPD.CssClass = "form-control-label";
lblCPD.Text = textbox[i].ToString();
CPDPlaceHolder.Controls.Add(lblCPD);
CPDPlaceHolder.Controls.Add(CPDT);
}
Button callSoap = new Button();
callSoap.ID = "txtDynamicSearch" + servicename;
callSoap.Text = "Search";
callSoap.CssClass = ".btn-info";
callSoap.CommandArgument = "test1";
callSoap.Click += new EventHandler(btnsoap);
callSoap.EnableViewState = true;
CPDPlaceHolder.Controls.Add(callSoap);
}
else
{
}
}
The textboxes and search button appears as needed. The problem now is when i clicked on the search button a post back occur and all the controls are gone. I have been reading a lot about initialising the controls in page_preinit and i tried the code below.
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
try
{
foreach (string key in keys)
{
TextBox CPDT = new TextBox();
CPDT.ID = "test" + i.ToString();
CPDT.CssClass = "form-control";
Label lblCPD = new Label();
lblCPD.ID = "txtDynamiclbl" + "test" + i.ToString();
lblCPD.CssClass = "form-control-label";
lblCPD.Text = textbox[i].ToString();
CPDPlaceHolder.Controls.Add(lblCPD);
CPDPlaceHolder.Controls.Add(CPDT);
i++;
}
}
catch
{
}
}
In the above function this line only returns the search button and not the texboxes. I am stuck on this issue.
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
T
I try to add a panel to Form, but it never appears. But When I change its type e.g. on TextBox it apears. Anyone know why?
HidePanel = new Panel();
HidePanel.ForeColor = Color.Red;
HidePanel.BackColor = Color.Green;//Form.BackColor;
HidePanel.Location = new System.Drawing.Point(531, 181);
HidePanel.Name = "HidePanel";
HidePanel.Size = new System.Drawing.Size(200, 100);
HidePanel.Visible = true;
HidePanel.TabIndex = 12;
HidePanel.BringToFront();
Form.Controls.Add(HidePanel);
you used Form and it's not true, you should use this instead of Form, try this code.
HidePanel = new Panel();
HidePanel.ForeColor = Color.Red;
HidePanel.BackColor = Color.Green;//Form.BackColor;
HidePanel.Location = new System.Drawing.Point(531, 181);
HidePanel.Name = "HidePanel";
HidePanel.Size = new System.Drawing.Size(200, 100);
HidePanel.Visible = true;
HidePanel.TabIndex = 12;
HidePanel.BringToFront();
this.Controls.Add(HidePanel);
update:
Form2 frm = new Form2();
Panel HidePanel = new Panel();
HidePanel.ForeColor = Color.Red;
HidePanel.BackColor = Color.Green;//Form.BackColor;
HidePanel.Location = new System.Drawing.Point(531, 181);
HidePanel.Name = "HidePanel";
HidePanel.Size = new System.Drawing.Size(200, 100);
HidePanel.Visible = true;
HidePanel.TabIndex = 12;
HidePanel.BringToFront();
frm.Controls.Add(HidePanel);
frm.Show();
I put this code in click event of button1, which declared in form1.
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.
Is there any way to add a System.Windows.Controls.TextBox to GroupBox controls in C#?
I tried the following but it doesn't show up in the groupbox:
public System.Windows.Controls.TextBox textBox6 = new System.Windows.Controls.TextBox();
public System.Windows.Controls.TextBox textBox7 = new System.Windows.Controls.TextBox();
public ElementHost sumtext = new ElementHost();
public ElementHost loctext = new ElementHost();
private void Form1_Load(object sender, EventArgs e)
{
textBox6.Name = "Summary";
textBox7.Name = "Location";
textBox6.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
textBox6.FontSize = 12;
textBox6.SpellCheck.IsEnabled = true;
textBox7.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
textBox7.FontSize = 12;
textBox7.SpellCheck.IsEnabled = true;
groupBox4.Controls.Add(sumtext);
sumtext.Dock = DockStyle.None;
sumtext.Width = 246;
sumtext.Height = 35;
sumtext.Child = textBox6;
sumtext.Location = new Point(3, 33);
sumtext.Visible = true;
sumtext.Enabled = false;
groupBox4.Controls.Add(sumtext);
groupBox4.Controls.Add(loctext);
loctext.Dock = DockStyle.None;
loctext.Width = 246;
loctext.Height = 35;
loctext.Child = textBox7;
loctext.Location = new Point(3, 90);
loctext.Visible = true;
loctext.Enabled = false;
this.Controls.Add(sumtext);
this.Controls.Add(loctext);
}
I need to use System.Windows.Controls.TextBox rather than Form.TextBox as I need it for spell check.
Any help would be greatly appreciated!
I changed the Enabled property of the sumtext, and removed the other box to shorten it:
This code works for me:
public Form1()
{
this.Load += new System.EventHandler(this.Form1_Load);
}
public System.Windows.Controls.TextBox textBox6 = new System.Windows.Controls.TextBox();
public ElementHost sumtext = new ElementHost();
private System.Windows.Forms.GroupBox groupBox4;
private void Form1_Load(object sender, EventArgs e)
{
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.SuspendLayout();
//
// groupBox4
//
this.groupBox4.Location = new System.Drawing.Point(57, 63);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(591, 238);
this.groupBox4.TabIndex = 0;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "groupBox1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(706, 478);
this.Controls.Add(this.groupBox4);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
textBox6.Name = "Summary";
textBox6.FontFamily = new System.Windows.Media.FontFamily("Microsoft Sans Serif");
textBox6.FontSize = 12;
textBox6.SpellCheck.IsEnabled = true;
groupBox4.Controls.Add(sumtext);
sumtext.Dock = DockStyle.None;
sumtext.Width = 246;
sumtext.Height = 35;
sumtext.Child = textBox6;
sumtext.Location = new Point(3, 33);
sumtext.Visible = true;
sumtext.Enabled = true;
groupBox4.Controls.Add(sumtext);
}
Is this code actually getting called? Has groupbox 4 been added to the form yet?
You should not be adding your ElementHost controls to your Form AND your GroupBox, it appears to be confusing .NET. Keeping your original code exactly as-is but commenting out these two lines makes it work:
//this.Controls.Add(sumtext);
//this.Controls.Add(loctext);
Also... I don't think it's hurting anything, but you don't need to do this twice:
//groupBox4.Controls.Add(sumtext);