Reputation: 319
Before you read, here are a couple pages I have looked at before posting this question:
'Button_Click' : is not a member of 'ButtonTest::MainPage'. This question regards C++ and is not useful as far as I am aware. According to the only answer, you need to "define the prototype of MainPage::Button_Click in the MainPage.xaml.h file as a member of the class."
visual basic error BC30456 'Form1' is not a member of 'WindowsApplication1'. This question also experiences the same error, however it is caused by something else. The OP replied to this answer, saying that it fixed the problem:
If you have renamed the startup form1 it is likely you also have to change the Startup form setting. You can find this setting to open 'My Project' in the 'Solution Explorer'. Select the Application section, change the 'startup form' as appropriate.
In my case, I have not renamed anything since making the project, and the code which the error points to is a line which adds a handler in 'popup.g.i.vb'
I also looked on MSDN: Creating Event Handlers for WPF Controls. The page uses the following syntax to react to a button being clicked:
<Button Click="Button2_Click" />
and
Sub Button2_Click(ByVal Sender As Object, ByVal e As RoutedEventArgs)
'Code here
End Sub
This is exactly the same syntax as I have used.
My code
I am currently making a program using WPF and Visual Basic, and I wanted to make a small window pop up if the user causes an error.
I have a WPF window named popup.xaml and I have a class called 'popup' which inherits from 'window'. I want to create a window and then close it when the user clicks the 'ok' button. The equivalent of this in C# would be the following:
popup errorWindow = new popup("Error message here");
errorWindow.ShowDialog();
When this method is called, an override of popup() is called which initialises and also takes the error message as a parameter.
public partial class popup : Window
{
public popup()
{
InitializeComponent();
}
public popup(string errorMessage)
{
InitializeComponent();
this.errorMessage.Text = errorMessage;
}
private void okButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
When the 'ok' button is clicked, the okButton_Click() method is called and the window closes.
In my visual basic application, I have the same WPF code for the error window:
<Window x:Class="FacialRecognitionVB.popup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FacialRecognitionVB"
mc:Ignorable="d"
Title="Error." MinHeight="150" MaxHeight="150" Height="150" MinWidth="300" MaxWidth="300" Width="300" >
<Grid>
<Border VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Gray" />
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5,5,5,5" >
<TextBlock x:Name="errorMessage" FontSize="18" FontWeight="Medium" Text="An error has occured." TextWrapping="Wrap" />
</StackPanel>
<Button x:Name="okButton" Template="{StaticResource ButtonTemplate}" Content="OK" Width="40" VerticalAlignment="Bottom" HorizontalAlignment="Right" Margin="5,5,5,5" Click="okButton_Click" />
</Grid>
Despite this, the event handler for clicking the button is playing up. The VB code for my popup class is almost exactly the same:
Partial Public Class popup
Inherits Window
Public Sub Popup()
End Sub
Public Sub Popup(ByVal message As String)
Dim errorMessage As TextBlock
errorMessage.Text = message
End Sub
Private Sub okButton_Click(sender As Object, e As RoutedEventArgs)
Me.Close()
End Sub
End Class
I create an instance of the class using this code in MainWindow.vb:
Dim errorWindow As New popup()
errorWindow.Popup("An error has occured.")
When I try to build this, I get the error 'okButton_Click' is not a member of 'popup'. On double-clicking the error in the error list it takes me to popup.g.i.vb and highlights the following:
#ExternalSource ("..\..\popup.xaml", 14)
AddHandler okButton.Click, New RoutedEventHandler(AddressOf Me.okButton_Click)
#End ExternalSource
It will not give me any further information other than 'okButton_Click' is not a member of 'popup'
How can I fix this? I actually have several buttons on my main page which work perfectly using visual basic! This one button, for some reason, does not. Is there something different I need to do because it is on a separate window as part of a popup?
Update
I had another look round today and I found '{name}' is not a member of '{classname}' on MSDN which gives the following two solution:
1: Check the name of the member to ensure it is accurate.
2: Use an actual member of the class.
My button is named okButton and the method is okButton_Click(). The error is highlighted in the popup.g.i.vb file, saying that Me.okButton_Click is not a member of my popup class. I do not see how either of the fixes above apply, since I am using the correct name and the member does exist.
Upvotes: 1
Views: 4651
Reputation: 8150
Your constructors are not defined correctly - constructors are always named New
in VB.NET - and you need to call InitializeComponent
from your constructor. You are also doing Dim errorMessage As TextBlock
, which should not be required since it is defined in XAML.
It seems like something has broken the link between the XAML and the code behind file, and it's probably easier to delete all files related to the window and start from scratch, but your code behind should look something like this:
Partial Public Class popup
Inherits Window
Public Sub New()
' This call is required by the designer.
InitializeComponent()
End Sub
Public Sub New(ByVal message As String)
' This call is required by the designer.
InitializeComponent()
errorMessage.Text = message
End Sub
Private Sub okButton_Click(sender As Object, e As RoutedEventArgs)
Me.Close()
End Sub
End Class
In VS2015, the code behind does not have the Partial
or Inherits Window
parts, so those may not be required, but probably don't hurt anything.
Displaying the window should also look like the C# version:
Dim errorWindow As New popup("An error has occured.")
errorWindow.ShowDialog()
Upvotes: 1