overloaded method match has some invalid arguments - c#

I keep getting this error - The best overloaded method match for HomeInventory2.Form1.Form1(System.Collections.Generic.IEnumerable) has some invalid arguments - for the starred section. At the same time I am also getting this error in the same spot - Argument 1: cannot convert from 'string' to System.Collections.Generic.IEnumerable
**Sorry - I should have added, the code sends a string to another form where it is split up and placed into separate text boxes.
namespace HomeInventory2
{
public partial class Form2 : Form
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Run**(new Form1(richTextBox1.Text))**;
}
}
}

First off, you probably don't want to call Application.Run here - you already have a form.
I suspect you want to split the rich text box content by lines, then pass this to the new form and show it. If that is the case, you can do:
private void button2_Click(object sender, EventArgs e)
{
// richTextBox1.Lines is a string[], which works with IEnumerable<string> (per line)
var form = new Form1(richTextBox1.Lines);
// Just show the form
form.Show();
}

Related

Sending an input String to another form in c#

So I have one form:
I want to then turn that into a variable on form 2 that is printed on for Form 1 code I have
private void button1_Click(object sender, EventArgs e)
{
//have user type term to search, and bring that input to form2
Form1.Enable = false;
Form newForm = new Form2(textBox1); //send data to new form
newForm.ShowDialog();
thats on the button click where i assume that where it sends the data
but for the text box itself i have :
private void txtBox1_TextChanged(object sender, EventArgs e)
{
Form newForm = new Form2(textBox1); //send data to new form
}
for form 2 idk how to put it in the other text box as the string I am extremely new to c# as i am coming from python but here is my form 2 code:
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
this.Form2.Text = textBox1;
}
note I have this in form 2 : public Form2(string textBox1)
and I want it to display in here:
note I am new so please be kind
Something wonky here, you say you have modified the Form2 constructor to accept a string but then you seem to send a textbox into it instead:
private void button1_Click(object sender, EventArgs e)
{
...
Form newForm = new Form2(textBox1); //send data to new form
I would make this:
Form2 newForm = new Form2(textBox1.Text); //send data to new form
And then make your Form2's constructor set the text in the form2 textbox:
public Form2(string tx){ //you must have this already
{
InitializeComponent();
someOtherTextboxOnForm2.Text = tx; //do it after InitializeComponent call
Please call your controls something different than "Form2", "textbox1" - youre allowed to rename them, it takes seconds to do and your code won't degenerate into an incomprehensible mess after about 10 controls

How do I solve this error: 'Cannot access a disposed object. Object name: 'IncomeForm'.'?

What I'm trying to do is get user input from a form (IncomeForm) by using a textbox (TextBoxIncomePrice) and after pressing the button (ButtonConfirmIncome), the LabelIncome label in the form MainPage should change to the value input by the user.
Everything works as intented, except for when I try to reopen the IncomeForm by clicking the AddIncomeButton. I get the error shown in the title. It should be able to reopen and accept a new value no matter how many times you close the IncomeForm.
Main Form (MainPage):
IncomeForm incomeForm = new IncomeForm();
private void incomeForm_FormClosed(object sender, FormClosedEventArgs e)
{
LabelIncome.Text = incomeForm.TextBoxIncomePrice.Text;
}
private void AddIncomeButton_Click(object sender, EventArgs e)
{
incomeForm.FormClosed += new FormClosedEventHandler(incomeForm_FormClosed);
incomeForm.Show();
}
Add Income Form (IncomeForm):
private void ButtonConfirmIncome_Click(object sender, EventArgs e)
{
this.Close();
}

winforms: Label showing systems.collections.genericlist '1 instead if data [duplicate]

This question already has answers here:
Convert a list to a string in C#
(14 answers)
Closed 4 years ago.
I've referred to this link (thread 1) but I get an error. For my example, I only have one list in one class. The class name is Savestate. I have 2 forms.
Form1 contain a textbox where the string that I saved inside will be transferred to the list when I press button1. Button1 will also open
Form2 where there is a label that should reflect the string in the textbox. However, the label will reflect systems.collection...
Below is my code.
Savestate: class name
public static List<string> number = new List<string>();
Form1
private void button1_click(object sender, System.EventArgs e)
{
Savestate.number.Add(textbox1.Text);
Formscollection.Form1.Hide(); //Form 1 and Form 2 saved in another class called formscollection
Formscollection.Form2.Show();
}
Form2 (Show the systems.collections..)
private void Form2_VisibleChanged(object sender, EventArgs e)
{
label1.Text = Savestate. number.ToString();
}
I tried another code based on other forums but I received an error
Form2 (got error: cannot implicitly convert type void to string)
private void Form2_VisibleChanged(object sender, EventArgs e)
{
foreach (string item in Savestate.number)
{
label1.Text = Console.WriteLine(item)
}
}
Hope to get help. Thanks.
You should display items of the list.
private void Form2_VisibleChanged(object sender, EventArgs e)
{
label1.Text = string.Join(", ", Savestate.number);
}
Or approach you already tried, but remove Console.WriteLine
private void Form2_VisibleChanged(object sender, EventArgs e)
{
foreach (string item in Savestate.number)
{
label1.Text += item;
}
}
you're trying to get a vlaue of the list. you can only get the last value of the LIST. and in the second code example you did wrong. Because Console.WriteLine doesnt work in win forms. Try to use this code:
foreach (string item in Savestate.number)
{
label1.Text += item;
}

System.FormatException occurred in mscorlib.dll when converting it int32

I know, i know there are lots and lots of questions asking on here about this error, each with their own response, but its easier to work off a response regarding your own code rather than someone else's
I have been working on this program for some time for a college assignment, and as soon as i started putting in the class to calculate the totals of things it now crashes,
I don't know where to look so i'll post my main code
enter code here
namespace Till
public partial class MainWindow : Window
{
Calculator calc = new Calculator();
public MainWindow()
{
InitializeComponent();
}
public bool User;
public bool tillopen = false;
private void button_Click(object sender, RoutedEventArgs e)
{
//button clone thingy
Button btn = (Button)sender;
label.Content = label.Content + btn.Content.ToString();
Console.Beep(); // makes the buttons beep
}
private void clear_Click(object sender, RoutedEventArgs e)
{
// Clear
label.Content = "";
}
private void Button_Submit_Click(object sender, RoutedEventArgs e)
{
// submit
listView.Items.Add(label.Content);
label.Content = "";
calc.setSoldItems(Convert.ToInt32(label.Content)); /// it breaks on this line///
}
private void button13_Click(object sender, RoutedEventArgs e)
{
//void sale
label.Content = "";
listView.Items.Clear();
}
private void button15_Click(object sender, RoutedEventArgs e)
{
//pound
label.Content = "1.00";
}
private void button12_Click(object sender, RoutedEventArgs e)
{
//till open close
tillopen = true;
}
private void button16_Click(object sender, RoutedEventArgs e)
{
Login m = new Login();
m.Show();
this.Close();
}
private void button14_Click(object sender, RoutedEventArgs e)
{
label.Content = "2.00"; // 2 pound
}
private void button17_Click(object sender, RoutedEventArgs e)
{
label.Content = calc.finish();
}
}
I have tried to re-create the error in another WPF (converting a to an int32) and it works fine, i know this is an issue with my code itself, i have tried using other machine and using different versions of visual studio itself, so we came to the assumption its this code itself and not a broken dll file
So before I sit down with my Teacher and spend all day going though my code step by step im asking around for help in order to save both of our time, This assignment is due in in 3 weeks, and it decides to break on me now.
thankies
To replicate this error, i press a number button on my Windows form, them hit the submit button i created (which starts the conversion) If a copy of my class which handles all of this is needed im happy to post it
In the method button_click, you have assigned value as
label.Content = label.Content + btn.Content.ToString();
which is a string value to the label and the values are concatenated instead of add.
and when you are reading it, you are converting it in Int32. which will give exception as it will not contain any integer value to it.
You can add the value like this:
label.Content = (Convert.ToInt32(label.Content) + Convert.ToInt32(btn.Content)).ToString();
and check before converting if the label has blank values in it, if it has do not convert them, and only convert the value if it has some values it will not give any error. Also do not assign any values other that numerical digits.
You are calculating:
Convert.ToInt32(label.Content)
but on the line before you set:
label.Content = "";
so this means you are calculating
Convert.ToInt32("")
which gives you a FormatException.
Perhaps you should use the value of label.Content before you overwrite it?

Opening Image directly into program

I made a basic picture viewer program in C# windows from , following a tutorial.
The program works fine but I want to open it like default windows photo viewer. I tried to open an image with the program directly but that opened the program and the image box was empty.
The image box works fine when images are browsed to open inside the program but how to make it work externally?
Extra : And is there a way to make it full screen?
Sorry for bad english.
P.S: Consider me very noob when helping. Thank you :)
namespace Basic_Picture_Viewer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void showButton_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Load(openFileDialog1.FileName);
}
}
private void clearButton_Click(object sender, EventArgs e)
{
pictureBox1.Image = null;
}
private void backgroundButton_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.BackColor = colorDialog1.Color;
}
}
private void closeButton_Click(object sender, EventArgs e)
{
ActiveForm.Close();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
else
pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void rotateButton_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
Image img = pictureBox1.Image;
img.RotateFlip(RotateFlipType.Rotate90FlipNone);
pictureBox1.Image = img;
}
}
}
Okay, so in your Program.cs file, implement the commmand line arguments according to the link in the comment above, and pass it to the form.
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if(args.Length > 0)
Application.Run(new Form1(args[0]));
else
Application.Run(new Form1());
}
Then in your form, change the constructor to
public Form1(String fileName = null)
{
InitializeComponent();
if (fileName != null)
{
// Add validation to ensure file exists here
this.WindowState = FormWindowState.Maximized;
pictureBox1.Load(fileName);
}
}
You'll either want a try/catch block or something to check for the existence of the file before attempting to open it. Under the use case you describe I would consider a missing file an exceptional case, so that seems like a plan to me.
The statement public Form1(String fileName = null) specifies one optional argument.Optional arguments are arguments that have a default value in the function,you always have the option to either call the function with or without argument,if you specify any argument the new argument is used in place of default argument,and in case you don't specify an argument when calling the function the default argument is used.The default value to optinal argument must always be a value that is a constant at compile time.To better clarify that let me take an example.Consider we have a function that adds two numbers.
private int AddNumbers(int a=10,int b=15)
{
int c=a+b;
return c;
}
We have specifies two optional arguments for the function,the above functions doesn't flag any error but as I said optional arguments must have a default value which is know at design time so the function below flags an error that follows Default parameter values must be compile time constant. because it uses default values that will be known at runtime.
int z,x;
private int AddNumbers(int a=z,int b=x)
{
int c=a+b;
return c;
}
Consider that the variables z and x are calculated using some logic at runtime,but are not unknown at compile time.This would flag the error.
Next,let me tell you differences in a normal function and a functional with optional parameters.The first function that compiles with no errors can be called in two ways;
**By passing some arguments when calling**
AddNumbers(5,15);
This will return 20.
**By calling the function without specifying any arguments**
AddNumbers();
This will return 25,remember we defined 10,15 as default arguments.Both the calls are valid and will compile without any errors.
So now I hope you've found answer to your question,if you would like to read more have a look here.

Categories