我正在嘗試將值從一個表單傳遞到另一個表單,如果我直接分配文本,它會起作用,但是當我從下拉框中獲取選定的專案時不起作用。
表格 2(無效)
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**
}
表格 1 代碼
Form2 bob = new Form2();
string CBPN = bob.CBPortName();
MessageBox.Show(CBPN.ToString());
如果我更改它以便分配文本,它似乎可以作業。`
string commName;
commName = "COM1";
return commName;
`
uj5u.com熱心網友回復:
好的,所以你正在制作一個設定表單。讓我們快速了解一下微軟打算如何完成這樣的事情。
重命名您的表單 SettingsForm。我用一個名為 SettingsForm 的新表單創建了一個專案,并添加了一個組合、一個 numericupdown 和一個文本框。如果您也添加這些,您可以按照以下步驟操作
在屬性網格中查找數字 updown。頂部是(應用程式設定),展開它以查看屬性系結。單擊 Property Binding 旁邊的 3 個點,將打開一個新視窗,可能指向 Value。下拉值旁邊并選擇新建(因為您還沒有任何設定)

在這里寫ComPort之類的東西,并選擇一個合理的起始值,比如1。OK
文本框也是如此。這是我之前做的一個:

- 對于組合,將一些字串放入其專案中

- 并在屬性設定中,系結文本,并將相同的條目作為其中一項

- 幾乎這就是你所要做的。此表單上的控制元件現在將編輯
Properties.Settings.Default.XXX用戶更改它們時的值,其中 XXX 是您選擇的名稱。這是專案選單上的設定.. 專案屬性.. 設定選項卡

- 這是一個可愛的主表單,非常簡單 - 兩個按鈕,一個顯示為波特選擇的任何內容的訊息框,另一個打開設定視窗以選擇波特等

- 在行動中:

- 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)

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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/405201.html
標籤:
