-1

I'm trying to pass a value from one form to the other and it works if I assign text directly, but does not work when I get a selected item from a drop box.

Form 2 (does not work)

public string CBPortname()
{
    string commName;
    commName = CommPortComboBox.GetItemText(CommPortComboBox.SelectedItem);            
    return commName; **//This seems to have the value if I hover over it. but is blank in the message box**
}

       

Form 1 code

Form2 bob = new Form2();
string CBPN = bob.CBPortName();
        
MessageBox.Show(CBPN.ToString());

If I change it so that text is assigned it seems to work. `

string commName;
commName = "COM1";          
return commName;

`

Jamiec
  • 133,658
  • 13
  • 134
  • 193
George
  • 3
  • 3
  • 1
    *`Form2 bob = new Form2();`* -`new` being the operative word. If I look out the window and see I left my cellphone in my Bentley, whipping out the magic lamp and saying "I wish for a new Bentley in the kitchen!" *poof, a new Bentley appears in the kitchen* won't mean my cellphone is now inside the house.. You'll need to use the *existing* form2 you have somewhere, that the user has manipulated the combobox of already, rather than making a new one. If you didn't keep any accessible reference to it (like a class level varaible) when you made it, that's what you need to fix – Caius Jard Jan 06 '22 at 17:05
  • Ok So I have the messagebox working by using this https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form, but when I try doing serialport1.portname it will not update the port name. If I asign port name COM4 it seems to work. – George Jan 06 '22 at 18:51
  • *when I try doing serialport1.portname it will not update* - you need to post full code statements, not fragments surrounded by that far-more-woolly language we humans speak, with verbs like "doing" that have no meaning in C#. Edit your question so that it contains the full code that you're currently struggling with – Caius Jard Jan 06 '22 at 19:08
  • I have a dropdown box on form 2. I retrieve the communications ports from the PC that are valid. I have four communications ports COM1, COM2 COM3 etc. If I select on COM4 it will pass the value from form2 to form1 and messagebox.show "CBPN" as earlier. That's not a problem anymore, it shows COM4 in the messsage box . If I assign serialport1.Portname to CBPN and then serialport1.open(), it will not change the serialport1.portname. It stays at COM1. Sorry it is my second post on here. – George Jan 06 '22 at 19:38
  • Is form2 some kind of settings form? If not, what is it? (I wouldn't have to ask if it'd been renamed to a sensible name after it was created: always rename your forms and your controls) – Caius Jard Jan 06 '22 at 19:59
  • Yes I made it so I can select port name baudrate, stopbits etc. I would've put the dropdowns on form1, but I don't have enough real estate. So I have a button that opens up the form 2 dialogue then all the controls are there. I then click ok and try and pass just the first one (port name) seems to be an issue. – George Jan 06 '22 at 21:05

1 Answers1

0

OK, so you're making you a settings form. Let's do a quick tut on how Microsoft intended stuff like this be done.

  • Rename your form SettingsForm. I've made a project with a new form, called SettingsForm, and I've added a combo, a numericupdown and a textbox. If you add these too, you can follow along with these steps

  • Look in the properties grid for the numeric updown. At the top is (Application Settings), expand it to see Property Binding. Click the 3 dots next to Property Binding, a new window opens probably pointing at Value. Drop down next to value and choose New (because you don't have any settings yet)

enter image description here

  • Write something like ComPort in here and choose a reasonable starting value, like 1. OK

  • Same for the Textbox. Here's one I made earlier:

enter image description here

  • For the combo, put some strings into its Items

enter image description here

  • And in the property settting, bind Text, and put a same entry as one of the items

enter image description here

  • Pretty much that's all you have to do. The controls on this form will now edit the values of Properties.Settings.Default.XXX when the user changes them, where XXX is the names you chose. Here are the settings on the Project menu.. Project properties.. Settings tab

enter image description here

  • Here's a lovely Main Form, very simple - two buttons, one shows a messagebox of whatever is selected for Baud, the other opens the settings window to select baud etc

enter image description here enter image description here

  • And in action:

enter image description here enter image description here

  • it's even so magic,that you can open lots of SettingsForms, change one and they all change (not that you need this..) but it shows if you set a value from code, then the UI would change to reflect it too (two way binding)

enter image description here

So.. All you have to do, after binding your controls properties to various settings, is read them into your com port e.g.:

var comPort = new SomeComportLibraryThing();
comPort.Port = "COM" + Properties.Settings.Default.Comport; //it's just a number from 1 to 9, add a string onto the start
comPort.Baud = int.Parse(Properties.Settings.Default.Baud);
comPort.Send(Properties.Settings.Default.ModemInitString);

Or however you need - these are just variables like anything else

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • OH MY GODS!! That is a whole lot easier than doing what I was doing. I was about to break out a notepad and write the settings to a txt file. Thank YOU SOOO MUCH!! Edit doesn't look like newbies can upvote for 15 something or other. So thank you again. – George Jan 07 '22 at 15:25
  • I forgot to mention.. now you can also call `Properties.Settings.Default.Save()` to save the values and they will be loaded next time the app starts. We often do this in the FormClosing event of the main form; allows you to save prefs, which is another bonus. Settings have two kinds: ApplicationScope and UserScope. Application scope ar eread only and can only be set by editing the `yourprogram.exe.config` file. User scoped settings can be set during runtime (so that's why we use them here). App scoed settings go in a .config file next to the exe, but user scoped ones go in a per user folder.. – Caius Jard Jan 07 '22 at 16:24
  • ..under the c:\users\... path somewhere (i forget where, you can find out on SO if you need to know), but if that file is missing they get their starting defaults from the yourprogram.exe.config - just in case youre ever editing that file in future and wondering why the user scoped vars aren't changing - you're probably editing the defaults but the current values are saved elsewhere – Caius Jard Jan 07 '22 at 16:27
  • I have one more question. So I have another settings form this time with a text box for the value. This time when I click on my OK on my settings form with text box. I need to stop the serialport from writing.. private void EnableDBT_Click(object sender, EventArgs e) { SettingsHertzRate DialogBox = new SettingsHertzRate(); DialogBox.Show(); string TenthsOfASeconds = (Properties.Settings.Default.DataRateSetting); serialPort1.Write(" $PAMTC,EN,DBT,1," + (TenthsOfASeconds) + "\r\n"); } – George Jan 07 '22 at 20:08
  • The button click on my form 1 is Enble DBT, which sends the datastring,, but I need it not to send the data string until I hit ok on my other form2. It sends it straight away. – George Jan 07 '22 at 20:12
  • I think you can change DialogBox.Show to DialogBox.ShowDialog to achieve what you want (but it's not completely clear to me what the question is) – Caius Jard Jan 07 '22 at 20:32
  • ShowDialog will pause the code at the point of the Show which the window is open, then the code carries on when the form that was ShowDialog'd is closed – Caius Jard Jan 07 '22 at 20:33
  • Yeah that works perfecto. Basically what I have is an acoustic transducer which emits and receives sound through water. Sort of like an altimeter. But used under water. It will give you the height from the transducer to the sea/river bed via an RS422 signal, although converted to RS232 for my comm port. The company that supplies the thing gives you an instruction set that can be applied in any terminal program, but it gets annoying having to copy and paste the commands in. – George Jan 08 '22 at 01:38
  • So while I got some time I'm writing a program to do it via buttons. I have other issues, but I'm going to at least try and figure out before I ask. I've gotten far enough to where everything works, it's just buggy somewhere around the serial event handler. – George Jan 08 '22 at 01:40
  • Makes sense to me - there would be a lot more work for software engineers if more people took the "I'm doing something really repetitive with a computer; surely there's some way to make the computer do it itself.." thought path! – Caius Jard Jan 08 '22 at 08:44
  • Ok I have one last question on this and I think I'm all good. I want to add two labels to form 1. One is Comm port the other is baudrate. I can set the text of the label on the form load event to Properties.Settings.Default.xxx in form1, but I need to update the label on form1 when my settings form is closed. Is there an easy way to do that on closing my settings form? I say closing the form, but I mean press ok on my settings form. – George Jan 10 '22 at 16:26
  • Why not just bind the text property of the label to the same setting that the textbox is bound to? Labels are read ony so it'll only ever be a one way binding (the label will show the current value in the setting, as soon as the setting changes the label will too) – Caius Jard Jan 10 '22 at 16:35