substring from form1 to form2....
-
Hey CP, Getting text from one form to another is no problem. But I am having issues showing data from a ComboBox from form1 to display as text in form 2. Alright let's jump right into my example.
string instText = comboInst.GetItemText(comboInst.SelectedItem);
string folder = instText.Substring(0, 4);comboInst is a combo box that has a list of items that is pulled from a directory. All items have a leading 4 digits that I use a lot in form1. But I can't get those 4 values for the life of me to show even in a messagebox on form 2. It just shows blank when using methods that pull information from a textBox.Text. I need to be able to use both instText and folder strings in other forms. Anyone got any ideas? The example I used is located on stackOverflow: [^] Thanks!
-
Hey CP, Getting text from one form to another is no problem. But I am having issues showing data from a ComboBox from form1 to display as text in form 2. Alright let's jump right into my example.
string instText = comboInst.GetItemText(comboInst.SelectedItem);
string folder = instText.Substring(0, 4);comboInst is a combo box that has a list of items that is pulled from a directory. All items have a leading 4 digits that I use a lot in form1. But I can't get those 4 values for the life of me to show even in a messagebox on form 2. It just shows blank when using methods that pull information from a textBox.Text. I need to be able to use both instText and folder strings in other forms. Anyone got any ideas? The example I used is located on stackOverflow: [^] Thanks!
If you want to get the selected Item value, you can directly get the selected value using the combo.SelectedItem propety. but thats not the problem here for you. did you check the contents of instText has the data you selected? have you tried to debug this? what is the content of instText?? moreover in which method you call these lines?
-
If you want to get the selected Item value, you can directly get the selected value using the combo.SelectedItem propety. but thats not the problem here for you. did you check the contents of instText has the data you selected? have you tried to debug this? what is the content of instText?? moreover in which method you call these lines?
Figured it out, dunno how I mucked it up at first, here's how you do it correctly. my button from form1
private void buttonSched_Click(object sender, EventArgs e)
{
string instText = comboInst.GetItemText(comboInst.SelectedItem);
string folder = instText.Substring(0, 4);
Schedule Open = new Schedule(instText);
Open.Show();
}Calling is in form2:
public partial class Form2 : Form
{
private string start;public Form2(string inst) { InitializeComponent(); this.start = inst; }
private void Schedule_Load(object sender, EventArgs e)
{System.Windows.Forms.MessageBox.Show(start);
}
Quite simple, I know, but I was thinking too much into something that didn't really need that deep of thought. Thanks for the looks! Hope this helps someone in the future. BTW - you can alter the string after it's been called to the new form. I.E.
System.Windows.Forms.MessageBox.Show(start.Substring(0, 4));
This is what I needed the code to do, grab the first 4 characters from the string. Now I can use the string raw, or formatted if I like. Thanks again!