Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How do you change the Form.Text at runtime?

 

When I try Form.Text = "new text" I get a compile error - "Reference to a non-shared member requires an object reference."

 

Thanks!

Posted
How do you change the Form.Text at runtime?

 

When I try Form.Text = "new text" I get a compile error - "Reference to a non-shared member requires an object reference."

 

Thanks!

Form is a class. . . change the text of the instance. . .

Assuming you are doing VB:

 

internal to your form class:

Me.Text = "new text"

 

external to your form class from a class in your namespace:

Dim frm As New Form1

frm.Text = "new Text"

frm.Show

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
Thanks for the reply. I'm still "getting my feet wet" with OOP. Here's my problem: The form is already displayed. The user has the option to customize the Application Title which displays on the main form. If they change the App Title, I also want to change the text displayed in the Title Bar. If I use frm.Show, it launches another instance of the form. How can I get the current form Title Bar text to change without launching multiple instances of the form?
Posted (edited)
Thanks for the reply. I'm still "getting my feet wet" with OOP. Here's my problem: The form is already displayed. The user has the option to customize the Application Title which displays on the main form. If they change the App Title' date=' I also want to change the text displayed in the Title Bar. If I use frm.Show, it launches another instance of the form. How can I get the current form Title Bar text to change without launching multiple instances of the form?[/quote']this is one way:

you have a class that changes the title of some form, I will call it title changer. . . in your case, it is the Main form, but in reality it is irrelevant what Form it is. You need to have a manner of registering a form with title changer, this can be done by Creating a property or, better yet, pass the form in TitleChanger' s constructor. have a method on TitleChanger that sets the registered forms text. . .

 

aww, hell a simple example should make it clear. . . vb any one???

cant swear this is correct as it came from a translator

 

Imports System
Imports System.Windows.Forms
Namespace FormTitleChange
Public Class TitleChanger
Private _frm As Form

Public Sub New(frm As Form)
_frm = frm
End Sub 'New

Public Property ManagedForm() As Form
Get
Return _frm
End Get
Set
_frm = value
End Set
End Property


Public Property FormText() As String
Get
Return GetFormText()
End Get
Set
SetFormText(value)
End Set
End Property


Public Sub SetFormText([text] As String)
_frm.Text = [text]
End Sub 'SetFormText


Public Function GetFormText() As String
Return _frm.Text
End Function 'GetFormText



Public Sub SetFormTextViaUserInput()
End Sub 'SetFormTextViaUserInput
End Class 'TitleChanger
_ ' Put Some Code here to Get User Input to change text


Public Class Form1
Inherits Form
Private _titleChanger As TitleChanger

Public Sub New()
_titleChanger = New TitleChanger(Me)
End Sub 'New


Public Sub PromptForNewTitle()
_titleChanger.SetFormTextViaUserInput()
End Sub 'PromptForNewTitle
End Class 'Form1 
End Namespace 'FormTitleChange 

Damn. . . look how much nicer the C# code is:

using System;
using System.Windows.Forms;
namespace FormTitleChange
{
public class TitleChanger
{
Form _frm;
public TitleChanger(Form frm)
{
_frm = frm;
}
public Form ManagedForm
{
get
{
return _frm;
}
set
{
_frm = value;
}
}
public string FormText
{
get
{
return GetFormText();
}
set
{
SetFormText(value);
}
}
public void SetFormText(string text)
{
_frm.Text = text;
}
public string GetFormText()
{
return _frm.Text;
}

public void SetFormTextViaUserInput()
{
// Put Some Code here to Get User Input to change text
}

}
public class Form1:Form
{
TitleChanger _titleChanger;
public Form1()
{
_titleChanger = new TitleChanger(this);
}
public void PromptForNewTitle() 
{
_titleChanger.SetFormTextViaUserInput();
}

}
}

Edited by Joe Mamma

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

  • 2 weeks later...
Posted

Thanks for the reply!

 

I found a much easier way, for those that are interested...

 

Dim objVar as Object
objVar = Me
CType(objVar, Form).Text = "New Dialog Title Text"

 

works like a charm!

 

Peace!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...