ToolStripControlHost's AutoSize does not function correctly - c#

In the code below, I tried to display a popup above the button when the button is pressed, but the AutoSize of ToolStripControlHost does not work properly and the entire contents are not displayed.
In addition to that, the popup is displayed slightly above the button, even though the button location is specified.
How can I solve this problem?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var panel1 = new Panel();
var label1 = new Label();
label1.Text = "12345\nabsde\nlllllllllllllllllA\nVWXYZ\nZZZZZZZZZZZZZZA";
label1.BackColor = Color.Red;
label1.Margin = Padding.Empty;
label1.AutoSize = true;
label1.Size = Size.Empty;
label1.Location = new Point(0, 0);
panel1.Controls.Add(label1);
var toolStripControlHost = new ToolStripControlHost(panel1);
toolStripControlHost.Margin = Padding.Empty;
toolStripControlHost.Padding = Padding.Empty;
toolStripControlHost.BackColor = SystemColors.Info;
toolStripControlHost.AutoSize = true;
toolStripControlHost.Size = Size.Empty;
var toolStripDropDown = new ToolStripDropDown();
toolStripDropDown.Margin = Padding.Empty;
toolStripDropDown.Padding = Padding.Empty;
toolStripDropDown.DropShadowEnabled = false;
toolStripDropDown.AutoSize = true;
toolStripDropDown.Size = Size.Empty;
toolStripDropDown.Items.Add(toolStripControlHost);
toolStripDropDown.Show(this, button1.Location, ToolStripDropDownDirection.AboveRight);
}
}
}

I noticed that the font changes when a control is inserted into toolStripControlHost, so I inserted the following code to try it out and make the font explicit, and it works fine now.
toolStripControlHost.Font = DefaultFont;

Related

How to display a popup on mouse hover of an HTML element in the WebBrowser control of a Form application

In the following code, C# and js are linked together and ToolStripDropDown is used to create a popup, but the mouseleave event of js does not fire when the mouse leaves the HTML element.
However, when I move the mouse over the popup, the mouseleave event fires.
If you move the mouse away from other directions, the mouseleave event will not fire.
Also, it seems to fire when the popup is not shown on the C# side.
I tried writing a code to focus on the WebBrowser control after Show, but even so, mouseleave of js doesn't fire.
I wonder why this is.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp5
{
public partial class Form1 : Form
{
public ToolStripDropDown toolStripDropDown;
public Form1()
{
InitializeComponent();
toolStripDropDown = new ToolStripDropDown();
toolStripDropDown.Margin = Padding.Empty;
toolStripDropDown.Padding = Padding.Empty;
toolStripDropDown.DropShadowEnabled = false;
webBrowser1.ObjectForScripting = new TestClasss(this);
webBrowser1.DocumentText = #"<script>
window.onload = function() {
var elm = document.createElement('div');
elm.innerHTML = 'test';
document.body.appendChild(elm);
elm.onmouseover = function() {
window.external.ShowPopup(this.getBoundingClientRect().left, this.getBoundingClientRect().top);
};
elm.onmouseleave = function()
{
window.external.ClosePopup();
};
};
</script>";
}
}
[ComVisible(true)]
public class TestClasss
{
private Form1 viewer;
public TestClasss(Form1 viewer)
{
this.viewer = viewer;
}
public void ShowPopup(int x, int y)
{
var panel1 = new Panel();
panel1.BackColor = Color.Red;
var label1 = new Label();
label1.Text = "popup";
panel1.Controls.Add(label1);
var toolStripControlHost = new ToolStripControlHost(panel1);
toolStripControlHost.Margin = Padding.Empty;
toolStripControlHost.Padding = Padding.Empty;
viewer.toolStripDropDown.Items.Clear();
viewer.toolStripDropDown.Items.Add(toolStripControlHost);
viewer.toolStripDropDown.Show(viewer.webBrowser1, new Point(x, y), ToolStripDropDownDirection.AboveRight);
}
public void ClosePopup()
{
viewer.toolStripDropDown.Close();
}
}
}
Try this code
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Body.MouseOver += new HtmlElementEventHandler(Body_MouseOver);
}
void Body_MouseOver(object sender, HtmlElementEventArgs e)
{
if (e.ToElement != null && e.ToElement.TagName == "H1" && e.ToElement.GetAttribute("processed") != "true")
{
string[] words = e.ToElement.InnerHtml.Split(' ');
e.ToElement.InnerHtml = "";
for (int i = 0; i < words.Length; i++)
e.ToElement.InnerHtml += "<span> " + words[i] + " </span>";
foreach (HtmlElement el in e.ToElement.GetElementsByTagName("span"))
el.MouseOver += new HtmlElementEventHandler(e_MouseOver);
e.ToElement.SetAttribute("processed", "true");
}
}
void e_MouseOver(object sender, HtmlElementEventArgs e)
{
toolStripTextBox1.Text = e.ToElement.InnerText;
}
}

c# winforms Instantiate of dynamically created Pictureboxes doesn't works

I'm trying to instantiate two pictureboxes created at runtime. I've assigned those to a panel, and I try to instantiate this in the form load event.
Here is my Class with the two pictureboxes and the panel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace AudioNodeGUI.Klassen
{
public class DateiÖffnenUndAuswählenSteuerelement : Form
{
public int xLoc = 0;
public int yLoc = 0;
public Point loc1;
public Point loc2;
public Panel elementpanel;
public DateiÖffnenUndAuswählenSteuerelement()
{
this.loc1 = new Point(xLoc, yLoc);
this.loc2 = new Point(xLoc + 41, yLoc);
this.elementpanel = new Panel
{
Name = "elementpanel",
Size = new Size(154, 22),
Location = new Point(xLoc, yLoc),
};
}
public Panel Zeichnen()
{
PictureBox browseImage = new PictureBox()
{
Name = "browseimage",
Size = new Size(41, 22),
Location = loc1,
Image = Image.FromFile(#"F:\AudioNodeGUI\images\Browse_used_files.jpg"),
Visible = true,
};
PictureBox fileImage = new PictureBox()
{
Name = "fileimage",
Size = new Size(113, 22),
Location = loc2,
Image = Image.FromFile(#"F:\AudioNodeGUI\images\Filebutton1.jpg"),
Visible = true,
};
browseImage.Click += new EventHandler(browseImage_Clicked);
fileImage.Click += new EventHandler(fileImage_Clicked);
this.elementpanel.Controls.Add(browseImage);
this.elementpanel.Controls.Add(fileImage);
return elementpanel;
}
public void browseImage_Clicked(object sender, System.EventArgs e)
{
}
public void fileImage_Clicked(object sender, System.EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Audio Files(*.wav; *.mp3; *.aif; *.aiff)";
}
}
}
And in the form load event, I've tried to instantiate the custom control:
private void AudioNodeWindow_Load(object sender, EventArgs e)
{
AudioNodeGUI.Klassen.DateiÖffnenUndAuswählenSteuerelement element = new AudioNodeGUI.Klassen.DateiÖffnenUndAuswählenSteuerelement();
Panel panel = element.Zeichnen();
this.Controls.Add(panel);
}
But when I ran the programm, the control wasn't shown. Do you know where i have a mistake?

How to call Input Dialog (Gorkem Gencay) from other class

how can i use 'Gorkem Gencay' InputDialog calling it from another Class?
Edit: Inserted all the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace InputDialog
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static DialogResult ShowInputDialog(ref string input)
{
System.Drawing.Size size = new System.Drawing.Size(200, 70);
Form inputBox = new Form();
inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
inputBox.ClientSize = size;
inputBox.Text = "Name";
System.Windows.Forms.TextBox textBox = new TextBox();
textBox.Size = new System.Drawing.Size(size.Width - 10, 23);
textBox.Location = new System.Drawing.Point(5, 5);
textBox.Text = input;
inputBox.StartPosition = FormStartPosition.CenterParent;
inputBox.Controls.Add(textBox);
Button okButton = new Button();
okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
okButton.Name = "okButton";
okButton.Size = new System.Drawing.Size(75, 23);
okButton.Text = "&OK";
okButton.Location = new System.Drawing.Point(size.Width - 80 - 80, 39);
inputBox.Controls.Add(okButton);
Button cancelButton = new Button();
cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
cancelButton.Name = "cancelButton";
cancelButton.Size = new System.Drawing.Size(75, 23);
cancelButton.Text = "&Cancel";
cancelButton.Location = new System.Drawing.Point(size.Width - 80, 39);
inputBox.Controls.Add(cancelButton);
inputBox.AcceptButton = okButton;
inputBox.CancelButton = cancelButton;
DialogResult result = inputBox.ShowDialog();
input = textBox.Text;
return result;
}
private void Form1_Load(object sender, EventArgs e)
{
string input = "hede";
ShowInputDialog(ref input);
}
}
}
I'm trying the following istead of using the private void Form1_Load(object sender, EventArgs e), but don't work:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InputDialog
{
class Class1
{
Form1 frm = new Form1();
string input = "hede";
frm.ShowInputDialog(ref input);
}
}
That ShowInputDialog method is defined as static so you need to use class name and not an object name when calling it. Assuming ShowInputDialog is defined in the Form1 class, you should invoke it as follows:
string input = "hede";
Form1.ShowInputDialog(ref input);
By the way, that method is defined as private so you have to make it public or internal.
The Class1 definition also has an error. You can't call procedural code (frm.ShowInputDialog(ref input);) out of procedural context. Define a method and put your dialog invocation code it this method:
class Class1
{
public static void TestDialogCall()
{
string input = "hede";
Form1.ShowInputDialog(ref input);
}
}

Controls not appearing despite being defined

I'm trying to create a Windows Form Control programatically, from scratch without using the WinForms Designer, but for some odd reason, none of the controls seem to be initialized.
When controls are dragged into the designer (and obviously with the InitializeComponent() method uncommented), they appear, but anything done programically outside doesn't appear.
I've went through to debug it and the code runs without any errors, but the label doesn't appear.
Question: Am I missing something?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinForm_Console
{
public partial class Form1 : Form
{
public Form1()
{
// InitializeComponent();
Initialize();
}
Label label1 = new Label();
public void Initialize()
{
this.Text = "WinForm Console Application";
SuspendLayout();
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(129, 112);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(35, 13);
label1.TabIndex = 0;
label1.Text = "label1";
label1.Show();
ResumeLayout(false);
PerformLayout();
}
}
}
Note that some of this code was copied from the designer, after my original attempt failed (which is the same thing without the extra information; initial label, size, logic suspending, etc.)
You have to put the control in the parent control Form. After creating the label, do this.
this.Controls.Add(label1);
Like this,
public void Initialize()
{
this.Text = "WinForm Console Application";
SuspendLayout();
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(129, 112);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(35, 13);
label1.TabIndex = 0;
label1.Text = "label1";
label1.Show();
this.Controls.Add(label1); //very very very important line!
ResumeLayout(false);
PerformLayout();
}

How do I access the text in a text box from a button click event handler

Im trying to write this simple winform menu and I need to add the contents of the NBox text box to a string so I can display it when a button is pressed, however I keep getting the error that NBox does not exist in the current context. So, how would i got about making the contents of the text box available at the press of a button?
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
//namespace game{
class MainM : Form{
public MainM(){
Text = "Adventures Main Menu";
Size = new Size(400,400);
//NameBox
TextBox NBox = new TextBox();
NBox.Location = new Point(145, 100);
NBox.Size = new Size(200, 30);
//Title Label
Label title = new Label();
title.Text = "ADVENTURE THE GAME";
title.Location = new Point(145, 30);
title.Size = new Size(200,60);
title.Font = new Font(defaultFont.FontFamily, defaultFont.Size, FontStyle.Bold);
//The main menu Buttons and all that jazz
Button credits = new Button();
Button start = new Button();
//Credits Button
credits.Text = "Credits";
credits.Size = new Size(75,20);
credits.Location = new Point(145,275);
credits.Click += new EventHandler(this.credits_button_click);
//Start Button
start.Text = "Start";
start.Size = new Size(75,20);
start.Location = new Point(145,200);
start.Click += new EventHandler(this.start_button_click);
//Control addition
this.Controls.Add(title);
this.Controls.Add(credits);
this.Controls.Add(start);
this.Controls.Add(NBox);
}
public void test(){
//The Main Window
}
private void credits_button_click(object sender, EventArgs e){
MessageBox.Show("Created by: Me");
}
private void start_button_click(object sender, EventArgs e){
this.Hide();
string name = NBox.Text;
MessageBox.Show(name);
//Process.Start("TextGame.exe");
}
public static void Main(){
Application.Run(new MainM());
}
}
//}
First, you need to name the control, that name will be its key in container's Controls collection :
//NameBox
TextBox NBox = new TextBox();
NBox.Location = new Point(145, 100);
NBox.Size = new Size(200, 30);
NBox.Name = "NBox"; //Naming the control
Then you will be able to retrieve it from the container:
private void start_button_click(object sender, EventArgs e){
this.Hide();
TextBox NBox= (TextBox)Controls.Find("NBox", true)[0];//Retrieve controls by name
string name = NBox.Text;
MessageBox.Show(name);
//Process.Start("TextGame.exe");
}
You declared NBox in consturctor and it is visible only inside constructor. You need to move it outside of constructor.
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
//namespace game{
class MainM : Form{
TextBox NBox;
public MainM(){
Text = "Adventures Main Menu";
Size = new Size(400,400);
//NameBox
NBox = new TextBox();
...

Categories

Resources