ryan1107 Posted August 6, 2004 Posted August 6, 2004 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! Quote
Joe Mamma Posted August 6, 2004 Posted August 6, 2004 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 Quote 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.
ryan1107 Posted August 6, 2004 Author Posted August 6, 2004 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? Quote
Joe Mamma Posted August 6, 2004 Posted August 6, 2004 (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 August 6, 2004 by Joe Mamma Quote 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.
ryan1107 Posted August 16, 2004 Author Posted August 16, 2004 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! Quote
Administrators PlausiblyDamp Posted August 16, 2004 Administrators Posted August 16, 2004 If you are changing the text from inside the form itself you could have done either Me.Text = "New text" 'or just Text = "New Text" without having to go through all the type casting. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ryan1107 Posted August 16, 2004 Author Posted August 16, 2004 Thanks, but I needed to change the text at runtime. Quote
Administrators PlausiblyDamp Posted August 16, 2004 Administrators Posted August 16, 2004 Which is exactly what my code does. It is a simpler form of your code. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ryan1107 Posted August 16, 2004 Author Posted August 16, 2004 Thank you! I see my original mistake. Me.Text = "New Text" instead of Form1.Text = "New Text" PlausiblyDamp - Much appreciated! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.