Getting a value from another form's combobox [duplicate] - c#

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 5 years ago.
I can't get value from form2 combobox. I checked that combobox modifier set to 'Public'.
I get the 'Object not referenced to instance of a object' error. What is the mistake?
//Form1
private DialogIO dio;
private void uosIO_ValueChanged(object sender, EventArgs e)
{
dio = new DialogIO();
dio.Show(); // Open Form2 and Set Some value on Combobox
}
// BGWorker get run call from form2 and run it on form1.
private void bgwCustomIO_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
MessageBox.Show(dio.uceMinHour.Text); // Show Combobox Value
}

consider check for null also...
private void bgwCustomIO_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
if (dio == null || string.IsNullOrEmpty(dio.uceMinHour.Text))
{
return;
}
MessageBox.Show(dio.uceMinHour.Text); // Show Combobox Value
}

Related

How do I send a message from a different section of code from where the new instance of the WebSocket class was created in websocket-sharp C# [duplicate]

This question already has answers here:
How can I use a local variable in a method from another method?
(1 answer)
"X" does not exist in this context
(4 answers)
The name '...' does not exist in the current context
(5 answers)
Closed 2 years ago.
This is my first post on Stack Overflow, so please excuse me if my question isn't very clear. To add more context, I want to open a connection to a WebSocket when the connect button is clicked and for a message to be sent from the connection opened by the connect button when the sent button is clicked. The problem is that I can not access the ws variable that I have created in the connect button from the send message button. The code below may give you more of an idea of what I want to do:
private void buttonConnect_Click(object sender, EventArgs e)
{
var ws = new WebSocket(textBoxSocketUrl.Text);
ws.Connect();
}
private void buttonSendMessage_Click(object sender, EventArgs e)
{
ws.Send(textBoxMessage.Text);
}
Thanks.
This is expected, that you can not see ws variable, because it was created in another "context". In order to get it, ws should be part of your class. Try to change your code to something like this:
private WebSocket ws;
private void buttonConnect_Click(object sender, EventArgs e)
{
ws = new WebSocket(textBoxSocketUrl.Text);
ws.Connect();
}
private void buttonSendMessage_Click(object sender, EventArgs e)
{
ws.Send(textBoxMessage.Text);
}

How to get rid of System.ObjectDisposedException when showing a form for the second time?

I have a form with a full picture which helps players to see where to move their puzzles in the game. When I show it for the second time, after I closed it, System.ObjectDisposedException is thrown.
I tried to use Hide() method when FormClosed event happened but it did not help. Also, I deleted the pictureBox from the control because I thought that it was causing this exception but is did not help either.
Original_px OrPix = new Original_px();
private void showFullPictureToolStripMenuItem_Click(object sender, EventArgs e)
{
OrPix.Show();
}
I want this form to work without this exception
If you close the form, through the close upper right icon or through code calling the form Close() method then the variable OrPix will reference a closed and disposed object.
You cannot reuse it without reinitilizing the variable with new Original_px();.
You need to know when the form is closed and you can receive this information handling the FormClosed event and set that variable to null.
So, when you need to display it again (or for the first time) you should check to see if the variable is null and reinitialize it
Original_px OrPix = null;
private void showFullPictureToolStripMenuItem_Click(object sender, EventArgs e)
{
if(OrPix == null)
{
OrPix = new Original_px();
OrPix.FormClosed += PixClosed;
}
OrPix.Show();
}
private void PixClosed(object sender, FormClosedEventArgs e)
{
OrPix = null;
}

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;
}

C# WPF How to use only numeric value in a textbox [duplicate]

This question already has answers here:
How do I get a TextBox to only accept numeric input in WPF?
(33 answers)
Closed 5 years ago.
I use a WPF application I would like to allow only numbers in a textbox. In a WindowsForm application I would know how I would have to make it.
Unfortunately, there is a KeyPress not in WPF and the "code" functioned also no longer.
How is it right and what is the event?
Here you can see the old code that has worked in Windows Form:
private void tbKreisdurchmesser_KeyPress(object sender, KeyEventArgs e)
{
if (char.IsNumber(e.Key) || e.KeyChar==".")
{
}
else
{
e.Handled = e.KeyChar != (char)Keys.Back;
}
}
You can add Previewtextinput event for textbox and validate the value inside that event using Regex,
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
var textBox = sender as TextBox;
e.Handled = Regex.IsMatch(e.Text, "[^0-9]+");
}

Passing ComboBox values to a method on another form c# [duplicate]

This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 6 years ago.
I have two issues with a method I am calling.
I have frmForm1 & frmForm2.
frmForm1 contains a method as below...
public frmForm1()
{
InitializeComponent();
}
//This method receives the prog name and WOtype names from frmForm2
int progID;
string programName;
public void GetIDandValue(string valName, int ID, string addWOValue)
{
if (valName == "progName")
{
progID = ID;
programName = addWOValue;
}
}
private void button1_Click(object sender, EventArgs e)
{
frmForm2 loadfrmForm2 = new frmForm2();
loadfrmForm2.Show();
}
Then from frmForm2 (which is opened from a btn click on frmForm1), I am trying to send values back to the method on frmForm1 so they can be used.
private void button1_Click(object sender, EventArgs e)
{
selectedValueID = Int32.Parse(comboBox1.ValueMember);
selectedValueName = comboBox1.DisplayMember;
string valToSend = "progName";
frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName);
this.Hide();
}
And finally, here's how combobox1 is being initialised...
comboBox1.DisplayMember = dsAddWO.Tables[0].Columns[1].ToString();
comboBox1.ValueMember = dsAddWO.Tables[0].Columns[0].ToString();
comboBox1.DataSource = dsAddWO.Tables[0];
comboBox1.Enabled = true;
Problem 1) combobox1 ValueMember and Displaymember are returning the column headers as values when i try to populate the variables (although the correct data is actually displaying in the combobox on the form).
Problem 2) I cant seem to call the GetIDandValue method from frmForm2, intellisense just doesnt see it.
No doubt im doing something incredibly stupid. Can anyone enlighten me?
You can pass the progID and programName to the constructor of frmForm2
Dose those codes are coded on form2 ?
private void button1_Click(object sender, EventArgs e)
{
selectedValueID = Int32.Parse(comboBox1.ValueMember);
selectedValueName = comboBox1.DisplayMember;
string valToSend = "progName";
frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName);
this.Hide();
}
If so, u'd better get a structor of from1, it seems u don't. like u did with form2: frmForm2 loadfrmForm2 = new frmForm2(); and then call to the method frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName); Totally Like This From1 frmForm1 =new From1();frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName);

Categories