UserControl is blank when added to panel (sometimes) - c#

I have an App written in .Net Framework 4.7.2 that sporadically, when a new user control is needed to be added to a flow panel, it's added successfully (and bumped to the top of the panel), but the user control is blank. Here's how I create it:
int shortcutNumber = NextShortcutNumber();
ctrlIssue newIssueCtrl = new ctrlIssue(issueData, shortcutNumber);
newIssueCtrl.IssueDeleted += new System.EventHandler(this.ctrlIssue_IssueDeletedHandler);
newIssueCtrl.Visible = false;
panIssues.Controls.Add(newIssueCtrl);
panIssues.Controls.SetChildIndex(newIssueCtrl, 0); // Always put it at the top
newIssueCtrl.Visible = true;
newIssueCtrl.Target();
I've diagnosed it down to that Control.Created is set to false. So ControlCreated is not called? I'm trying a workaround now like so:
if (!newIssueCtrl.Created)
{
newIssueCtrl.CreateControl();
}
We'll see if it works, but it's weird. Does anyone have any insight on why ControlCreated wouldn't be called? It seems sporadic and inconsistent, when it starts happening, it happens about 80% of the time.
Thanks!
EDIT: There was an ask to see the ctrlIssue constructor. There's fancy in it:
public ctrlIssue(Issue data, int shortcut)
{
InitializeComponent();
mIssueData = data;
chkPlayPause.TabIndex = 2;
lblProject.Text = mIssueData.Fields.Project.Name;
chkPlayPause.Text = mIssueData.Key;
lblSummary.Text = mIssueData.Fields.Summary;
InitializeToolTips();
UpdateShortcut(shortcut);
mSummaryFontRegular = lblSummary.Font;
mSummaryFontHover = new Font(mSummaryFontRegular, FontStyle.Underline);
mProjectFontRegular = lblProject.Font;
mProjectFontHover = new Font(mProjectFontRegular, FontStyle.Underline);
}

Related

Dynamically Generate Groupboxes

I'm working on an inventory program and have finished the main functionality as a command line console app. I am now working on a version for winforms. I want to enable it to dynamically generate a Groupbox that holds some textboxes. I'd rather not design 50+ lines of multiple textboxes. Keep in mind I'm rather new to programming, having started with C# a year ago. I know next to nothing on Winforms.
I've tried to use dynamic item = new Groupbox();as a similar method allowed generation of objects at runtime. In the command line app, the way it works is that based on information given, a certain amount of objects are passed into the list _AllItems. I was thinking of generating the Groupboxes by using:
private void InitializeGroupBox()
{
foreach (Product product in Product._AllItems)
{
dynamic Item = new GroupBox();
}
}
But I have the feeling I'm nowhere near the correct method. Thanks to anybody who helps.
You will need to learn a bit more, but here is what I usually do to achieve what you asked.
internal class DynamicForm : Form
{
private FlowLayoutPanel mFlowLayoutPanel;
public DynamicForm()
{
mFlowLayoutPanel = new FlowLayoutPanel();
mFlowLayoutPanel.Dock = DockStyle.Fill;
// Add to this Form
this.Controls.Add(mFlowLayoutPanel);
InitializeGroupBox();
}
private void InitializeGroupBox()
{
mFlowLayoutPanel.SuspendLayout(); // Performance
for (int i = 1; i <= 20; i++) {
var groupBox = new GroupBox();
groupBox.Text = "GroupBox #" + i;
groupBox.Size = new Size(200, 50);
var textBox = new TextBox();
textBox.Dock = DockStyle.Fill;
// Add the TextBox to GroupBox
groupBox.Controls.Add(textBox);
// Add to this Form
mFlowLayoutPanel.Controls.Add(groupBox);
}
mFlowLayoutPanel.ResumeLayout(); // after suspend, resume!
}
}

Bring a control to front

I have a user control that uses a textbox and a list box. List box isn't visible, it only becomes visible when user starts typing or click in text box.
I have added the user control to a group box which is on the form.
Now when the listox becomes visible, it stays inside the group box, and can't see the full height. I wan't it float on top so that i can see the full height.
I have looked around, implemented some solutions but nothing worked for me.
Constructor for the user control
namespace YarCustomControl
{
public partial class YarCustom : TextBox
{
public YarCustom()
{
InitializeComponent();
_code = "";
_id = -1;
//list box handling
listBox = new ListBox();
listBox.Visible = false;
listBox.Font = this.Font;
listBox.Location = this.Location;
listBox.BorderStyle = BorderStyle.Fixed3D;
listBox.Resize += new EventHandler(listBox_Resize);
//listBox.SelectedValueChanged += new EventHandler(listBox_SelectedValueChanged);
listBox.KeyDown += new KeyEventHandler(listBox_KeyDown);
listBox.Click += new EventHandler(listBox_Click);
//test => no affect on listbox
this.Controls.Add(listBox);
listBox.Visible = false;
}
}
}
and the following method makes the listbox visible. Both SetchildIndex (commented and not commented) throw an error
private void makeListBoxVisible()
{
Form parentForm = (this.FindForm() as Form);
//parentForm.Controls.SetChildIndex(listBox, 0);
this.Controls.SetChildIndex(listBox, 0);
listBox.Visible = true;
listBox.BringToFront();
}
What is the best approach for handling something like this?
My environment is VS2010 and WinForms.
Now when the listox becomes visible, it stays inside the group box,
and can't see the full height. I wan't it float on top so that i can
see the full height.
Simply put it directly on the Form.

TableLayoutPanel: How can i navigate with tab key through dynamically added controls?

I have a form which contains a dynamically added TableLayoutPanel, which contains some dynamically added Labels, TextBox, CheckBox. I am obtaining exactly the visualization I would like to have, but I am struggling to get the "tab key" to work for moving from one control to the other.
I have tried to add a:
control.TabIndex = tabIndex++;
control.TabStop = true;
But this doesn't seem to have any impact...
This is the (tested) stub code:
class MyForm : Form
{
public MyForm()
{
InitializeComponent();
string[] titles = {"first","second"};
var myLayout = new TableLayoutPanel();
myLayout.AutoSize = true;
int myTabIndex = 1; //Not really necessary
int rowNumber = 0;
foreach (var title in titles)
{
var label = new Label();
label.Text = title;
myLayout.Controls.Add(label, 0, rowNumber);
var control = new TextBox();
control.TabIndex = myTabIndex++; //Not really necessary
myLayout.Controls.Add(control, 1, rowNumber);
rowNumber++;
}
this.Controls.Add(myLayout);
}
}
This is the window I get, and I am not able to navigate from first to second field using the tab key.
Update:
Applying Visual Studio 2013 Update 5 did not help.
Something must have been corrupted in my project. The best I could do is to move on with a new clean Windows Form project, and everything now is working.

Clicking on List Item on Dropdown

I'm having a problem in interacting with a custom dropdown control. It works fine the 1st 6 times, but after that, since the screen is resized, it could no longer locate and click the option in the dropdown control, returning an exception - can't click on hidden control. I tried putting in a itemField.DrawHighlight(); on the control I'm looking for, and it finds it, however it can't click on it. I also tried a to scroll down, but it seems to be not working.
bool addItemCheck = false;
int scrollCheck = 0;
while (Check == false)
{
var addItem= new HtmlButton(window);
addItem.SearchProperties.Add(HtmlButton.PropertyNames.Id, "add-new-item");
Mouse.Click(addItem);
scrollCheck = scrollCheck + 1;
if (scrollCheck > 6)
{
Mouse.MoveScrollWheel(window, -100);
}
var itemDropDown = new HtmlSpan(window);
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.Class, "item-dropdown");
itemDropDown .SearchProperties.Add(HtmlSpan.PropertyNames.InnerText, "Select an Item");
Mouse.Click(itemDropDown );
addItemCheck = itemDropDown.Exists;
}
bool itemBoxCheck = false;
HtmlCustom itemBox = null;
while (itemBoxCheck == false)
{
itemBox = new HtmlCustom(window);
itemBox.SearchProperties.Add(HtmlCustom.PropertyNames.Id, "item-listbox");
var itemField = new HtmlCustom(itemBox);
itemField .SearchProperties.Add(HtmlCustom.PropertyNames.InnerText, item);
Mouse.Click(itemField);
itemBoxCheck = itemBox.Exists;
}
I would really appreciate any help. Thank you.
Try calling the method InsureClickable() on the control before attempting to click on it.
for example:
itemDropDown.EnsureClickable();
Mouse.Click(itemDropDown);
Edit:
if this doesn't work you'll have to scroll down to the item.
try using:
Mouse.MoveScrollWheel()
if that doesn't work also you'll have to map the scroll control and click on it.

I Can't get a pic loaded onto my web form

I'm new to developing with C# and asp.net. I am trying to laod five images onto my web form I also need to do it code behind and not just drag and drop. I can't get them to work. I have tried:
dicePic[i] = new System.Web.UI.WebControls.Image();
//dicePic[0] = Image.FromFile(HttpContext.Current.Server.MapPath("/images/1.gif"));
I also set the properties like this:
dicePic[0].ID = "Dice1";
dicePic[0].Style["position"] = "absolute";
dicePic[0].Style["top"] = "80px";
dicePic[0].Style["left"] = "80px";
dicePic[0].Visible = true;
dicePic[0].Width = 50;
dicePic[0].Height = 50;
dicePic[0].ImageUrl = "~/images/1.gif";//HttpContext.Current.Server.MapPath("/images/1.gif");
dicePic[0].Controls.Add(dicePic[0]);
Any help would be great. Thanks
replace this line :dicePic[0].Controls.Add(dicePic[0]); by this.Controls.Add(dicePic[0]);
You should add it to a container in your page (or to the page itself).
if you are doing this inside a Page put
this.Controls.Add(dicePic[i]);
after
dicePic[i] = new System.Web.UI.WebControls.Image();

Categories

Resources