Bug while inserting button form control onto Excel Worksheet - c#

I’m trying to insert a simple Button form control onto worksheet within a Excel VSTO Addin. This code works, but the button it offset down and to the right (as you can see in image below).
Any clue why this is happening and how I can fix it?
Thanks!
Microsoft.Office.Tools.Excel.Worksheet vstoWorksheet = Globals.Factory.GetVstoObject(this.Application.ActiveWorkbook.Worksheets[1]);
System.Windows.Forms.Button button1 = vstoWorksheet.Controls.AddButton(ws.Range["$B$2:$D$6"], "button1");
button1.Left = 50;
button1.Top = 50;
button1.FlatAppearance.BorderSize = 0;
button1.MouseClick += Button1_MouseClick;
button1.Text = "OK";
button1.BackColor = System.Drawing.Color.Red;
button1.FlatStyle = FlatStyle.Flat;

In the code you are setting the location by specifying the following properties:
button1.Left = 50;
button1.Top = 50;
Try to not set them to any value or just comment these two lines of code and the offset will be removed.

Related

C# Windows Forms Combobox not working correctly

I'm currently working on a backup program. On Friday everything worked without any problems and the project was almost finished.
Today I started Visual Studio without doing anything else. And every combo box I add has the drop-down menu shifted to the right.
I have removed all the code and created new combo boxes, but they are still wrong displayed. I never had this problem before.
Maybe it's the Form Designer, I haven't checked it yet. Maybe someone has had this problem before and can help me? I would be very grateful.
public Form1()
{
InitializeComponent();
this.CenterToScreen();
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Font = new Font("Arial", 10, FontStyle.Regular);
this.PathSettingsPanel.BackColor = System.Drawing.Color.LightGray;
this.UsernameComboboxCreate.DropDownStyle = ComboBoxStyle.DropDownList;
this.SelectBackupSend.DropDownStyle = ComboBoxStyle.DropDownList;
this.DtDUsernameSource.DropDownStyle = ComboBoxStyle.DropDownList;
this.DtDUsernameTarget.DropDownStyle = ComboBoxStyle.DropDownList;
this.indicator.Text = string.Empty;
this.SendBackupIndicator.Text = string.Empty;
this.IndicatorSource.Text = string.Empty;
this.IndicatorTarget.Text = string.Empty;
this.panel3.BorderStyle = BorderStyle.FixedSingle;
this.panel4.BorderStyle = BorderStyle.FixedSingle;
this.panel5.BorderStyle = BorderStyle.FixedSingle;
this.ServerBackups.View = View.Details;
this.Updating.Visible = true;
this.ServerBackups.UseCellFormatEvents = true;
Serverpath.Text = Main.ReadPathFromFile("server");
PopulateListBoxes();
StartCopy.DoWork += backgroundWorker1_DoWork;
StartCopy.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
StartCopy.WorkerReportsProgress = true;
StartCopy.WorkerSupportsCancellation = true;
FillProgressBar.DoWork += backgroundWorker2_DoWork;
FillProgressBar.RunWorkerCompleted +=
backgroundWorker2_RunWorkerCompleted;
FillProgressBar.ProgressChanged +=
backgroundWorker2_ProgressChanged;
FillProgressBar.WorkerReportsProgress = true;
FillProgressBar.WorkerSupportsCancellation = true;
}
Edit
I've tried a little around. The problem persists even if I create a new project and copy code and forms.
When I create a combobox outside the tab control, it works as it should. Once I click a combobox inside the tab control it will be displayed incorrectly. After that the box which works at the beginning doesn't work anymore.
Okay the problems seems to be the tabcontrol. I just have no idea why.

Strange empty spaces in FlowLayoutPanel

I have lots of buttons on flowlayoutpanel, and then there's text labels to break the flow. Last button before label and label itself has SetFlowBreak. All works kind of fine, but what I don't understand, is why there is so much space under the text label? If form is resized so narrow that there's only one column of buttons, then the unwanted space disappears. Can someone explain how that space can be removed?
Code:
public Form1()
{
InitializeComponent();
for (int i = 1; i <= 100; i++)
{
Button button = new Button();
button.Text = i.ToString();
button.Width = 150;
button.Height = 50;
button.Margin = new Padding(5);
flowLayoutPanel1.Controls.Add(button);
if (i % 10 == 0)
{
flowLayoutPanel1.SetFlowBreak(button, true);
Label label = new Label();
label.Text = "Some random text";
label.AutoSize = true;
label.Margin = new Padding(5, 5, 0, 0);
label.BackColor = ColorTranslator.FromHtml("#ccc");
flowLayoutPanel1.Controls.Add(label);
flowLayoutPanel1.SetFlowBreak(label, true);
}
}
}
And couple of images to show what I mean:
Image1: Strange space under the Label
Image2: No space under the Label when the form is resized (this is how I'd like this to work)
Thank you Hans! I thinks this is a real answer, as it solved my problem: (quote from comments)
It is a bug, same one as this one. The extra space is the height of the next label. The workaround is exactly the same, just add a dummy control with a Width of 0 after the label. – Hans Passant
So first I removed flowbreak after the real label:
flowLayoutPanel1.SetFlowBreak(label, true);
And then replaced it with the following code, and the mysterious space disappeared!
Label dummyLabel = new Label();
dummyLabel.Width = 0;
dummyLabel.Height = 0;
dummyLabel.Margin = new Padding(0, 0, 0, 0);
flowLayoutPanel1.Controls.Add(dummyLabel);
flowLayoutPanel1.SetFlowBreak(dummyLabel, true);

How to resize WPF About Box

I have trying to use that about bopx in my WPF application http://www.nuget.org/packages/AboutBox/
but i cant figure out how to resize it and how to make it not dragable. I tried that but no way:
About about = new About();
about.Window.Width = 120;
about.Window.Height = 130;
about.Window.MaxWidth = 120;
about.Window.MaxHeight = 130;
about.Window.MinWidth = 120;
about.Window.MinHeight = 130;
about.Window.ResizeMode = ResizeMode.NoResize;
about.Window.WindowStyle = WindowStyle.ToolWindow;
about.Window.WindowState = WindowState.Minimized;
about.Window.AllowDrop = false;
about.Show();
May be some one may help.
Also I would like to display close or OK button to close the window, and want to disable closing window when focus is loosing.
UPDATE:
I ended up by using http://wpfmbx.codeplex.com/ it is exactly what i need
I have not tried that About box but following should be the correct order for heights:
About about = new About();
about.Window.MinWidth = 120;
about.Window.MinHeight = 130;
about.Window.MaxWidth = 120;
about.Window.MaxHeight = 130;
about.Window.Width = 120;
about.Window.Height = 130;
MinWidth/MinHeight takes precedence then comes MaxWidth/MaxHeight and Width/Height. I am not 100 % sure that it is the cause of your problem, just give it a try.
To be able to make it drag-able manually, then you just need to call DragMove(), on MouseDown or some similar event.

labels does not print the entire string

I have dynamically created an array of labels. however, when i tried to set label.text = "hahaha hehehehe hmmmm", it displays only "hahaha" and not anything after the space.
for (int i = 0; i < labelArray.Length; i++)
{
labelArray[i] = new Label();
labelArray[i].BackColor = Color.Bisque;
labelArray[i].Font = new Font(labelArray[i].Font.FontFamily, labelArray[i].Font.Size + 5, FontStyle.Bold);
labelArray[i].Location = new Point(25, temp);
labelArray[i].Name = "searchLabel" + i.ToString();
labelArray[i].Text = "hahahahaha";
labelArray[i].MouseEnter += new EventHandler(main_MouseEnter);
labelArray[i].MouseLeave += new EventHandler(main_MouseLeave);
searchPanel.Controls.Add(labelArray[i]);
temp += 40; ;
}
have I missed out any anything? btw, this is the "initialization" done in Form_load and I edited the label.text in a TextChanged event. many thanks!
edit: I have since fixed the problem by setting autosize to true.
I have tried to display e.g. "hahahahahahahahaha hmmmmmm hehehehehehehehhe" and "ha hmmmmmmm hehehehehehhe" and in both cases only the first word gets displayed so I dont think it is being truncated..
the code which i used to set the text is simply:
labelArray[11].Text = "hahahahahahahahahaha eheheheheh hmmmmm";
try setting AutoSize attribute to true
labelArray[i].AutoSize = true;

Creating Dynamic ComboBox in C#

I'm new to Visual Studio 2010 C# and I'm creating an application where the user will select the number of textboxes will be shown in a form. For example, if the user will select "2" automatically there will be 2 boxes will be shown in the form.
This is the screenshots that I want to create.
I guess what you need to know is dynamic creation of controls.
To do what you want here you need to:
Create a control
Add control to form
Set control location, size and anything else you need
It would go something like this:
Texbox texbox = new Textbox();
Controls.Add(textbox);
textbox.Top = 20;
textbox.Left = 200;
textbox.Width = 200;
textbox.Name = "textbox1";
So that there's something left for you to do, you should repeat steps above in a loop, and calculate location of each textbox so that they're not stacked up.
comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int i = 0;
int y = 0;
while (i < int.Parse(comboBox1.SelectedItem.ToString()))
{
System.Windows.Forms.TextBox tt = new System.Windows.Forms.TextBox();
y = y + 30;
tt.Location = new System.Drawing.Point(0, y);
this.Controls.Add(tt);
i++;
}
}
Hope this helps

Categories