从一个表单中选择值并在第二个表单上显示 [英] selecting values from one form and showing it on 2nd form
问题描述
我被困在我的项目中我在我的表格中有航空预订系统主题1
我在两个不同的组合框中选择源和目的地我希望显示这个选定的来源和目的地是在DataGridView1中加载下一个form2中的用户选择。
(我是vb初学者给我提示小代码作为示例我将根据我的项目使用它)
谢谢
I am Stuck here in my project I have topic of airline reservation system in my form1
I am selecting source and destination in two different combobox and I want to show this selected source and destination which is selected by user in next form2 on load in DataGridView1.
(well i am beginner in vb give me hint with small code as example i ll make it in use as per to my project)
Thank you
推荐答案
保存公共String变量中两个组合框的值
form1上的代码,其中有2个组合框
save value of both combo box in a public String variable
Code on form1 where there are 2 combo box
public strSource as string=""
public strDest as string=""
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
strSource =ComboBox1.Text
End Sub
Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
strDest =ComboBox2.Text
End Sub
现在在Form2上有DataGridView1在form2上写下代码加载
Now on Form2 where there is DataGridView1 write below code on form2 load
DataGridView1.Rows.Add(form1.strSource,form1.strDest)
i假设DataGridView1中有两个coloums
Happy Coding .....
i assume there are two coloums in DataGridView1
Happy Coding.....
具体如何做到这一点取决于你如何显示两个表单的范围。
如果您创建Form2并从Form1中显示它(例如按下按钮),那么你可以写一个Form2构造函数接受信息作为参数并显示它,或者您可以向Form2添加显示信息的公共属性。然后,您可以在创建实例时以及在调用ShowDialog之前设置信息。
如果您已经打开,创建并显示Form2(通过Show方法)从Form1,然后创建上面的属性并使用您显示的Form2实例来设置显示信息。
如果Form2是在Form1外创建的(由父母创建)例如,然后最好的方法是在Form1中创建一个事件,告诉父母获取信息并将其传递给Form2(再次通过Form2属性)
Exactly how you do this depends to an extent on how you display the two forms.
If you create the Form2 and display it from your Form1 (on a button click for example) then you can either write a Form2 constructor that accepts the information as parameters and displays it, or you can add public properties to Form2 which display the information. You can then set the information when you create the instance, and before you call ShowDialog.
If you already have Form2 open, created and displayed (via the Show method) from Form1, then create the properties as above and use Form2 instance you displayed to set the display information via those.
If the Form2 is created outside Form1 (by a parent form for example) then the best way is to create an event in Form1 which tells the parent to get the info and pass it to Form2 (again via Form2 properties)
If Len(Trim(combobx1.Text)) = Len(Trim(combobx2.Text)) Then
MessageBox.Show("Please select another city", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
combobx2.Focus()
Exit Sub
End If
这是解决方案,因为你们支持你们。如果你想从两个组合框中使用相同的文本值,那么使用这个解决方案
this is solution which worked guys thnx for ur support. use this solution if u ant want to have same text value from two combobox to b used
这篇关于从一个表单中选择值并在第二个表单上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!