Issues displaying a second form C# - c#

I have some issues displaying a second form after clicking a specific button in my first form. The question might sound silly, but I am a newbie to programming...
I have added a new Windows form into my project (Form2), but still, when I use
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Report()
{
InitializeComponent();
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 Report = new Form2();
Report.Show()
}
}
}
I get the following error
Error 1 The type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?)
d:\Projects C#\The Bizzy D\The Bizzy D\Form1.cs 661 13 The Bizzy D
What am I doing wrong then? Any ideas would be appreciated. Thanks!

You forgot a using directive as the error message states. The problem is, that even if you created this second form, the connection between the type name Form2 and the file it contains isn't clear to C#.

Check if your Form1 and Form2 are in the same namespace.

If you Want To Open Form2 On Form1 Button Click Event.
Follow the Below Steps:
Right Click on Project.
Add->Windows Form...
Enter the Form Name : Form2.cs
Write the following Button Click Event Code in Form1.
private void btnShowForm2_Click(object sender, EventArgs e)
{
new Form2().Show();
}

Related

Copying a contex into clipboard C#

I found this code in stackOF but it doesn't work at all and i can't fix that.
would you tell me what's wrong with this code?
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox1.Text);
}
}
}
i think your problem because you named your program with same name of class that copy text to clipboard
take look at this code
using System.Windows.Forms;
namespace Clipboard
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Clipboard.SetText(textBox1.Text); This will not work if you named your namespace Clipboard !
System.Windows.Forms.Clipboard.SetText(textBox1.Text); // you should use this way to confirm you need to access to clipboard not your namespace
}
}
}
i named my program with same name of class(Clipboard)
and i have problem now because the compiler confusing between your program and class that copy a text
So the best way is to specify a unique name each time you create a program :)
There is a namespace conflict with your own. You can either explicitly use the exact Clipboard.SetText() method using the full declaration as per #WaleedKhaled's answer:
System.Windows.Forms.Clipboard.SetText(textBox1.Text);
or else the using statement at the top of your example to say something like:
using WinForms = System.Windows.Forms;
then your line would read:
WinForms.Clipboard.SetText(textBox1.Text);

C# Make a button change a public value

I'm working in VisualStudio.
I have this Form:
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 Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
}
public static int signal = 0;
public void button1_Click(object sender, EventArgs e)
{
}
}
}
And this User Control:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Test
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
if (Form1.signal == 1)
{
MessageBox.Show("Signal received!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
}
}
I bust my head to try to display the 'MessageBox' from 'UserControl1' when 'button1' from 'Form1' is clicked. Basically, I want to change the value of 'signal' to 1 when the 'button1' is pressed. I'm newbie but I'm pressed by time here so a good help will be very welcome. Any ideas? Thank you!
The button1_Click is the event that is going to be triggered when you click on the button from the Form.
Either create an event like another user suggested, or refer to this question and create a custom message box with your UserControl as its content.
When your UserControl is loaded it sees Form1.signal as 0 since you initialized it with that value. Your UserControl1 will never be aware of the change on Form1.
What you need to do is make listener function in UserControl1 that will be triggered, let's say every 10milliseconds and that will check if Form1.signal == 1.
Check Interval library for that, and I suggest you find time to learn a bit of C#. The language is awesome.
The best way to deal with communication between containers is to implement an observer class
The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
(wikipedia)
the way i do this is creating an Observer class:
1 public delegate void dlFuncToBeImplemented(int signal);
2 public static event dlFuncToBeImplemented OnFuncToBeImplemented;
3 public static void FuncToBeImplemented(int signal)
4 {
5 OnFuncToBeImplemented(signal);
6 }
so basically: first line says that there would be a function that somebody else will implement
second line is creating an event that occur when the delegated function will call
and the third line is the creation of the function that calls the event
so in your UserControl you should add a function like this:
private void ObserverRegister()//will contain all observer function registration
{
Observer.OnFuncToBeImplemented += Observer_OnFuncToBeImplemented;
/*and more observer function registration............*/
}
void Observer_OnFuncToBeImplemented(int signal)//the function that will occur when FuncToBeImplemented(signal) will call
{
MessageBox.Show("Signal received!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
and in your Form you should do something like:
public static int signal = 0;
public void button1_Click(object sender, EventArgs e)
{
Observer.FuncToBeImplemented(signal);//will call the event in the user control
}
and now, you can register this function to a whole bunch of other controls and containers and they will all get the signal
I hope this would help :)

C# system.management is not loaded

I'm trying to use system.management in a C# windows forms app in VS 2017, but it just won't work.
Here is a code sample:
using System;
using System.Windows.Forms;
using System.Management;
namespace MyWMIapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void okButton_Click(object sender, EventArgs e)
{
ManagementScope scope = new ManagementScope(#”\\MyComputerName\root\CIMV2″);
}
}
}
At: "using System.Management;" it tells me: this is not needed/used.
At: "ManagementScope scope = ......" it tells me: not found (maybe a missing using-Directive), and is red underlined.
Both is obviously wrong. I'm loading it and I'm using it. Why don't both lines "see" each other?
Add reference to System.Management.dll to your project.

C# winform, How to create a form ContainerControl?

Recently I have been researching on how to make your own control. And I have asked in the past (2 days ago) about a Tab Control issue that I am having. Well now I want to create a ContainerControl. Something like the form in the MetroFramework because I want to develop my own custom looking form. So fair this is what I have of the form.
now the problem that I am having is that this is made inside of the form. There isn't a ContainerControl where I just type in like 1 line of code and my form will look like that. In this solution I will need to type in about 30 lines. So when I mean by container control, you change the form object in the form.cs coding and it will change the design of the form.
Example for metro framework.
public partial class Form1 : MetroFramework.Forms.MetroForm
Now if I wanted to create a custom form like the one in the picture. I don't want to add 30 - 50 lines of code per form to do that. I just want to make one form class and then add where the "MetroFramework.Forms.MetroForm" is and replace it with my form. For example
public partial class Form1 : MyFormExampleNamespace.MyForm
And this is what the regular c# winform class structure looks like
public partial class Form1 : Form
So now that I explained what I want, define what I think a form ContainerControl is, and what I want. How could I accomplish my goal? How would I make a ContainerControl. This is what I have tried before
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace GoatUserControls
{
public class GoatForm : Form
{
public GoatForm()
{
this.FormBorderStyle = FormBorderStyle.Fixed3D;
base.FormBorderStyle = FormBorderStyle.Fixed3D;
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// GoatForm
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Name = "GoatForm";
this.Load += new System.EventHandler(this.GoatForm_Load);
this.ResumeLayout(false);
}
private void GoatForm_Load(object sender, EventArgs e)
{
}
//protected override BorderStyle()
// {
// }
}
}
And this is what I have in the main form
public partial class Form1 : GoatUserControls.GoatForm
So how would I make a form ContainerControl?

c# Add listView items from another form [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to add items in a listView from an antoher form but I don't no how I can do that. I have try this but it gives an error.
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Local_Host
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Form1 = this;
}
public static Form1 _Form1;
public void AddItem(object value)
{
listView1.Items.Add(value);
}
}
}
Form2
private void button1_Click(object sender, EventArgs e)
{
Form1._Form1.AddItem(textBox1.Text); //error
}
The error is in the function
public void AddItem(object value)
{
listView1.Items.Add(value);
}
you pass an object to this function and try to add it to the ListViewItemCollection, but there is no overload of the Add method of the ListViewItemCollection that accepts an object
Change it to
public void AddItem(string value)
{
listView1.Items.Add(value);
}
This will solve the immediate compilation problem, but you will have hard time working with that static variable. If your plan were to pass values from form2 to form1 it is better that you keep the created instance of form1 and use it to pass values through the AddItem method otherwise you will end to add that values to other instances of the Form1 (the latter instance created will receive the new string)
Instead pass the textbox value to Form1 constructor like
Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1(textBox1.Text);
frm.show();
}
Then in Form1 add the value to your listview like
public partial class Form1 : Form
{
public Form1(string listview_val)
{
InitializeComponent();
this.listView1.Items.Add(listview_val);
}
}

Categories

Resources