Hi, I am struggling to adapt to vb.net.
I need to know how to call another forms control from another form
e.g. if i was on form 2 and clicked a control, and I wanted to call cmdbtn1 on form1, what would I write, like in vb6 this was just 'call form1.cmdbtn1_click '
But every time I try anything like this it says: 'use raise events instead , not a member of windows.forms.form2'
I really need help on this so I would very much appreciate it, thanks.VB.net raise events, calling other forms controls?
You could put whatever you want to do in cmdbtn1_click in a class. Then you can access that function from both forms. If you need to access controls on the form then you can just pass the form to the function or create an overrideable function.
EG:
Public Function Overrides DoSomething(frm1 as form)
End Function
Or try
Raise Events form1.cmdbtn1_click
hth
Monday, January 18, 2010
VB.NET, one event for multiple controls?
I came across an example that looks like this:
Public Sub OnClockTick2(ByVal sender As Object, ByVal e As
EventArgs) Handles MyFirstButton.Click, MySecondButton.Click
Console.WriteLine(';Received a click event of first or second button';)
End Sub
which gives me an idea of what I am trying to do. I have a form with multiple textboxes, and I want to do the same thing for each of those textboxes when text is changed in any of them. My question is, however, how do I determine which textbox was changed? Would it have something to do with ';sender'; ?
thanks!!VB.NET, one event for multiple controls?
sender is an Object.
All Objects have an Equals() method for reference comparison.
Compare sender to MyFirstButton with a simple if..else statement.
Public Sub OnClockTick2(ByVal sender As Object, ByVal e As
EventArgs) Handles MyFirstButton.Click, MySecondButton.Click
Console.WriteLine(';Received a click event of first or second button';)
End Sub
which gives me an idea of what I am trying to do. I have a form with multiple textboxes, and I want to do the same thing for each of those textboxes when text is changed in any of them. My question is, however, how do I determine which textbox was changed? Would it have something to do with ';sender'; ?
thanks!!VB.NET, one event for multiple controls?
sender is an Object.
All Objects have an Equals() method for reference comparison.
Compare sender to MyFirstButton with a simple if..else statement.
VB.NET, one event for multiple controls?
I came across an example that looks like this:
Public Sub OnClockTick2(ByVal sender As Object, ByVal e As
EventArgs) Handles MyFirstButton.Click, MySecondButton.Click
Console.WriteLine(';Received a click event of first or second button';)
End Sub
which gives me an idea of what I am trying to do. I have a form with multiple textboxes, and I want to do the same thing for each of those textboxes when text is changed in any of them. My question is, however, how do I determine which textbox was changed? Would it have something to do with ';sender'; ?
thanks!!VB.NET, one event for multiple controls?
sender is an Object.
All Objects have an Equals() method for reference comparison.
Compare sender to MyFirstButton with a simple if..else statement.
Public Sub OnClockTick2(ByVal sender As Object, ByVal e As
EventArgs) Handles MyFirstButton.Click, MySecondButton.Click
Console.WriteLine(';Received a click event of first or second button';)
End Sub
which gives me an idea of what I am trying to do. I have a form with multiple textboxes, and I want to do the same thing for each of those textboxes when text is changed in any of them. My question is, however, how do I determine which textbox was changed? Would it have something to do with ';sender'; ?
thanks!!VB.NET, one event for multiple controls?
sender is an Object.
All Objects have an Equals() method for reference comparison.
Compare sender to MyFirstButton with a simple if..else statement.
Is it posibble to add controls dynamically in Visula studio VB.net? how?
uh, what your talking about... you mean programmaticaly, you find the control you want you make a new instance of it and then you it to your form like so...
yourcontrolhere As New ListView()
Form1.Controls.Add(yourcontrolhere)Is it posibble to add controls dynamically in Visula studio VB.net? how?
Yes, and I'm assuming that you're doing a WinForms application.
Use the Add method of the Controls collection object of your current form, for example:
Dim txtName as New TextBox();
'you can set properties for this control too
...
Me.Controls.Add(txtName);Is it posibble to add controls dynamically in Visula studio VB.net? how?
Yes you can surely do this. Following is a method which adds a button (for example) to your form dynamically. When you will call this method, this will add a button to your form. You can add other controls likewise.
Private Sub CreatButton()
Dim btnYes As System.Windows.Forms.Button
' Create control
btnYes = New System.Windows.Forms.Button()
' Set button properties
btnYes.Name = ';ButtonYes';
btnYes.Size = New System.Drawing.Size(70, 30)
btnYes.Location = New System.Drawing.Point(300, 30)
btn.TabIndex = 1
btnYes.Text = ';YES';
' add to the parent form's controls collection
Me.Controls.Add(btnYes)
End Sub
Hope that answers the question
yourcontrolhere As New ListView()
Form1.Controls.Add(yourcontrolhere)Is it posibble to add controls dynamically in Visula studio VB.net? how?
Yes, and I'm assuming that you're doing a WinForms application.
Use the Add method of the Controls collection object of your current form, for example:
Dim txtName as New TextBox();
'you can set properties for this control too
...
Me.Controls.Add(txtName);Is it posibble to add controls dynamically in Visula studio VB.net? how?
Yes you can surely do this. Following is a method which adds a button (for example) to your form dynamically. When you will call this method, this will add a button to your form. You can add other controls likewise.
Private Sub CreatButton()
Dim btnYes As System.Windows.Forms.Button
' Create control
btnYes = New System.Windows.Forms.Button()
' Set button properties
btnYes.Name = ';ButtonYes';
btnYes.Size = New System.Drawing.Size(70, 30)
btnYes.Location = New System.Drawing.Point(300, 30)
btn.TabIndex = 1
btnYes.Text = ';YES';
' add to the parent form's controls collection
Me.Controls.Add(btnYes)
End Sub
Hope that answers the question
How to change the place of controls in a form of vb.net?
you should view your form in Design view and change the controls' places with your mouse! is it so hard, or I didn't understand?!
VB.NET Looping through user controls on a page?
I have a user control that's added to my page. Inside that user control is a checkbox and on the main page I have a button. When I click that button I need to know which of the checkboxes are checked.
How do I loop through the user controls on a page?VB.NET Looping through user controls on a page?
First of all, you should go over to forums.asp.net. You'll get better help there for .NET related code.
Second of all, I am a C# developer, so I will do my best to write the VB code.
I'm not sure I fully understand your situation. You may want to reword your question more clearly. But I can see two possible scenarios you might be encountering.
Scenario A:
You have multiple instances of the same UserControl on your page and you need to (from the Page) get a list of the CheckBox controls inside these UserControls that are checked?
Scenario B:
You have a single instance of the UserControl on your page and you need to get a list of the CheckBox controls inside it that are checked.
Let me start off by saying, this isn't a very good design. You may want to rethink this to make it work better. A UserControl is a way of encapsulating controls (and their related logic). You should never try to access the controls inside it from an outside class. That kind of breaks the fundamental princible of OOP. However, what you could do is create a public property on your UserControl that would return the selected checkboxes as a List%26lt;CheckBox%26gt; or something.
In C# this would be:
private List%26lt;ListItem%26gt; _SelectedCheckBoxes;
public List%26lt; ListItem %26gt; SelectedCheckBoxes {
get
{
if (_SelectedCheckBoxes == null) {
_SelectedCheckBoxes = new List%26lt;ListItem%26gt;();
foreach (ListItem item in theCheckBoxList.Items) {
if (item.Selected) {
_SelectedCheckBoxes.Add(item);
}
}
}
return _SelectedCheckBoxes;
}
From your Page you would then have a myUserControl.SelectedCheckBoxes property.
Hope some of that helps.VB.NET Looping through user controls on a page?
You do know that you can reference the checkbox directly from the form, right?
Assuming that the GenerateMember property of the checkbox is set to true, you can just do:
If control.CheckBox1.Checked Then ...
But if you really want to loop through all the controls on the user control:
For Each c As Control In control.Controls
聽聽If TypeOf c Is CheckBox Then
聽聽聽聽If CType(c, CheckBox).Checked Then
聽聽聽聽聽聽MsgBox(c.Text)
聽聽聽聽End If
聽聽End If
Nexttopical cream
How do I loop through the user controls on a page?VB.NET Looping through user controls on a page?
First of all, you should go over to forums.asp.net. You'll get better help there for .NET related code.
Second of all, I am a C# developer, so I will do my best to write the VB code.
I'm not sure I fully understand your situation. You may want to reword your question more clearly. But I can see two possible scenarios you might be encountering.
Scenario A:
You have multiple instances of the same UserControl on your page and you need to (from the Page) get a list of the CheckBox controls inside these UserControls that are checked?
Scenario B:
You have a single instance of the UserControl on your page and you need to get a list of the CheckBox controls inside it that are checked.
Let me start off by saying, this isn't a very good design. You may want to rethink this to make it work better. A UserControl is a way of encapsulating controls (and their related logic). You should never try to access the controls inside it from an outside class. That kind of breaks the fundamental princible of OOP. However, what you could do is create a public property on your UserControl that would return the selected checkboxes as a List%26lt;CheckBox%26gt; or something.
In C# this would be:
private List%26lt;ListItem%26gt; _SelectedCheckBoxes;
public List%26lt; ListItem %26gt; SelectedCheckBoxes {
get
{
if (_SelectedCheckBoxes == null) {
_SelectedCheckBoxes = new List%26lt;ListItem%26gt;();
foreach (ListItem item in theCheckBoxList.Items) {
if (item.Selected) {
_SelectedCheckBoxes.Add(item);
}
}
}
return _SelectedCheckBoxes;
}
From your Page you would then have a myUserControl.SelectedCheckBoxes property.
Hope some of that helps.VB.NET Looping through user controls on a page?
You do know that you can reference the checkbox directly from the form, right?
Assuming that the GenerateMember property of the checkbox is set to true, you can just do:
If control.CheckBox1.Checked Then ...
But if you really want to loop through all the controls on the user control:
For Each c As Control In control.Controls
聽聽If TypeOf c Is CheckBox Then
聽聽聽聽If CType(c, CheckBox).Checked Then
聽聽聽聽聽聽MsgBox(c.Text)
聽聽聽聽End If
聽聽End If
Next
VB.NET Coping with many controls?
I've come to a point in project that there are roughly 500-600 controls on one form, these are all organized into groupbox/panels/tabs. The problem I'm getting is that I've run out of space and now just moving one panel to the side is connecting with a group box in another one you cant even see, could anybody please point me in the right direction on how combat this?
Is it possible to turn automatic parenting of controls? I'm pretty happy with my layout, I just need to move things about now and again.VB.NET Coping with many controls?
That many controls? Are they all necessary? Could you not simplify your form or have more than one form?
Instead of having tabs, have another form, then on Userform1 you can insert a button to show Userform2 instead of selecting a tab?
Then you could have multiple tabs (you can have more than one tab-page-book on one form) on multiple forms.
There's more than one way to skin a cat. Is the way you're doing it now the best/easiest way?
Is it possible to turn automatic parenting of controls? I'm pretty happy with my layout, I just need to move things about now and again.VB.NET Coping with many controls?
That many controls? Are they all necessary? Could you not simplify your form or have more than one form?
Instead of having tabs, have another form, then on Userform1 you can insert a button to show Userform2 instead of selecting a tab?
Then you could have multiple tabs (you can have more than one tab-page-book on one form) on multiple forms.
There's more than one way to skin a cat. Is the way you're doing it now the best/easiest way?
Subscribe to:
Posts (Atom)