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);
}
}
Related
Problem
I have an issue with moving the PrintPage function (SampleForm_PrintPage) to a new class (PrintPageDesign) also design of the PrintPage uses data from the main form and i have not been able to pull the data in to the new class.
Why?
I'm moving all PrintPage functions to individual classes as there are multiple page designs required in the application, having them all in the same main form seems hard to review and update when each page design requires any change.
Sample Code
To simplify my problem i have created a sample solution in visual basic,
Form1.cs (form code):
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace Sample_Print
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void BTN_Print_Click(object sender, EventArgs e)
{
PrintDialog PD_SamplePage = new PrintDialog();
PrintDocument Doc_SamplePage = new PrintDocument();
Doc_SamplePage.PrintPage += SampleForm_PrintPage;
PD_SamplePage.Document = Doc_SamplePage;
Doc_SamplePage.Print();
}
protected void SampleForm_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.CompositingMode = CompositingMode.SourceOver;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
e.Graphics.DrawString(TB_Name.Text.ToString(), new Font("Roboto Condensed",12, FontStyle.Bold), Brushes.Black, 10, 10);
}
}
}
Requirement
i would like to move function
SampleForm_PrintPage
to class PrintPageDesign , currently there is only visual studio generated code is in the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Sample_Print
{
class PrintPageDesign
{
}
}
i have tried several ways to get the value from the text box outside of the main form but resulted in null.
any help is highly appreciated.
As commented above, you can use partial classes to separate the Main Form members, methods and functionalities.
Press Shift+Alt+C to add a new class. Rename the file to PrintPageDesign and hit Add.
In the new class, add the partial modifier and change the name to Main (the exact name of the main Form). Note, we are creating a partial class here and not deriving from the Main form.
Now you are within the Main Form context and you can access its members.
Example
The Main Form class:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Windows.Forms;
namespace Sample_Print
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void BTN_Print_Click(object sender, EventArgs e) => PrintJob1();
}
}
The PrintPageDesign class:
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.ComponentModel;
namespace Sample_Print
{
partial class Main
{
private void PrintJob1(bool preview = false)
{
using (var doc = new PrintDocument())
{
doc.PrintPage += (s, e) =>
{
var g = e.Graphics;
var r = new Rectangle(e.MarginBounds.X, e.MarginBounds.Y,
e.MarginBounds.Width, 32);
using (var sf = new StringFormat())
using (var fnt = new Font("Roboto Condensed", 12, FontStyle.Bold))
{
sf.Alignment = StringAlignment.Near;
sf.LineAlignment = StringAlignment.Center;
g.DrawString(TB_Name.Text, fnt, Brushes.Black, r, sf);
r.Y += r.Height;
foreach (Control c in Controls)
{
g.DrawString(c.Name, fnt, Brushes.Black, r, sf);
r.Y += r.Height;
}
// ...
}
};
if (preview)
using (var ppd = new PrintPreviewDialog() { Document = doc })
ppd.ShowDialog();
else
{
using (var pd = new PrintDialog() { Document = doc })
{
if (pd.ShowDialog() == DialogResult.OK)
pd.Document.Print();
}
}
}
}
}
}
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;
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;
}
}
I use OPC-UA SDK of treager. I have variable created be PLC S7-1200
.
It displayed the value on Console.WriteLine, but I cant show it on listView
.
Can someone help me show the variable changes on listView1 ?
If I note listView1.Items.Add(itm); It will show that
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Opc.Ua.Client;
using Opc.UaFx;
using Opc.UaFx.Client;
namespace Test_OPC
{
public partial class OPCUA : Form
{
private readonly OpcClient client;
public static OpcValue isRunning;
string[] arr = new string[4];
ListViewItem itm;
public OPCUA()
: base()
{
this.client = new OpcClient("opc.tcp://192.168.1.200:4840");
InitializeComponent();
}
private void OPCUA_Load(object sender, EventArgs e)
{
client.Connect();
OpcSubscription subscription = client.SubscribeDataChange("ns=4;i=15",HandleDataChanged);
//ListViewItem listView1 = new ListViewItem();
//ListViewItem itemHienThi = new ListViewItem();
//Add Item vào ListView
arr[0] = "01";
arr[1] = "100";
arr[2] = "10";
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
}
private void HandleDataChanged(object sender,OpcDataChangeReceivedEventArgs e)
{
OpcMonitoredItem item = (OpcMonitoredItem)sender;
//Add the attribute name/value to the list view.
arr[0] = item.NodeId.ToString();
arr[1] = e.Item.Value.ToString();
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
Console.WriteLine("Data Change from NodeId '{0}': {1}",item.NodeId,e.Item.Value);
}
}
}
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();
...