Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Current ComboBox control doesn't have Locked Property as same as the Locked property in ComboBox in VB6.

Locked= false : prevent user changing text in combo box.

 

I tried to add this by inheriting ComboBox.

and overried OnKeyPress.

But, followings.. do work...

Please help..

 

bool locked=false;
public bool MyLocked{
 set{locked = value;}
}
protected override void OnKeyDown(KeyEventArgs e)
{
  if(locked)
  {
e.Handled=true;
  }
}

 

 

second try : this looks worked but, not..

It doesn't work when user type fast..


private string tempText;
	private bool byKeyStroke=false;
	protected override void OnKeyDown(KeyEventArgs e)
	{
		tempText = this.Text;
		byKeyStroke = true;
		if(textLocked)
		{
			e.Handled=true;
		}
	}
	protected override void OnTextChanged(EventArgs e)
	{
		if(textLocked && this.byKeyStroke)
		{				
			this.Text = tempText;
			byKeyStroke = false;
		}
	}
	

Sun Certified Web component Developer,

Microsoft Certified Solution Developer .NET,

Software Engineer

Posted

thanks..

But, those doesn't work as I need..

so, its there any ways doing it by handling event as I wrote previously.

Sun Certified Web component Developer,

Microsoft Certified Solution Developer .NET,

Software Engineer

Posted

Thank you for considering

Actually I tried with all 3 of Key events..

All don't work..

after

e.Handle = true;

still the OnTextChanged event called and text is modified.

 

e.Handle = true;

should prevent further event as I know..but, not..

Sun Certified Web component Developer,

Microsoft Certified Solution Developer .NET,

Software Engineer

Posted

I ran a quick test to see if it was working on my machine and using the KeyPress worked, this is what I did

 

private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (this.comboBox1.Tag.ToString() == "LOCK")
{
	e.Handled = true;
}
}

 

I used the tag property as a quick way of testing.

 

This didn't allow me to type anything into the comboBox but it still allows you to select an item. If you don't want them to be able to select items then the easiest way to do this is by setting the enabled property to False, the only draw back is that it greys out the control.

Posted
Can I ask why setting the dropdown style doesn't work for you. I have lots of forms using that method, and found it worked OK.

 

Thank you for your comment.

 

the reason I don't want to use DropDownStyle is..

 

this.skyComboBox1.DropDownStyle=ComboBoxStyle.DropDownList;

this.skyComboBox1.Text = "Current Text"; //this is not shown

 

As shown above I can't set Text on Combobox.

 

I want some text to be shown on text area..

but, after chaing DropDownList style I can't set text..

Sun Certified Web component Developer,

Microsoft Certified Solution Developer .NET,

Software Engineer

Posted (edited)

Thank you pendragon.

You are absolutely right.

I'm sorry I missed referring that I'm inheriting ComboBox class.

 

I'm inheriting from ComboBox so that I can reuse it without handling event on every client. I can just set TextLocked property.

This make exactly SkyComboBox as same as ComboBox in VB6.

( I dont' use the name 'Locked' property since VS.NET use it for Form modification lock functionality.)

 

I don't know why this approach doesn't work, meaning if I use it inside of subclass.

 

Please check following class..

and feel free to comment about other methods..

public class DriverClass{
 private SkyComboBox skybox;
 ....
 public void NeedTextLocked(){
    skybox.TextLocked = true;
}
}

public class SkyComboBox:ComboBox{

 private bool textLocked;
 public bool TextLocked{
    set{ textLocked = value;}
    get{return textLocked;}
 }
 protected override void OnKeyPress(KeyPressEventArgs e)
 {
       if(textLocked)
       {
             e.Handled = true; //this doesn't work!!!!!!!!!!!!!!!
         }
  }

  //I created this since combo.SelectedItem = itemObject;
  //doesn't make the itemObject selected.
  public void SetSelectedItem(object item)
 {
     for(int i=0; i< this.Items.Count; i++)
     {
if(this.Items[i] == item){	
                   this.SelectedIndex = i;
               }
      }
  }

 //I created this since combo.SelectedText = "item Text"
 //doesn't make the item selected.
 public void SetSelectedText(string text)
 {
        text = text.Trim();
         for(int i=0; i< this.Items.Count; i++)
        {
Debug.WriteLine(this.Items[i].ToString());
if(this.Items[i].ToString().Equals(text))
   this.SelectedIndex = i;
         }
   }
}

Edited by goodmorningsky

Sun Certified Web component Developer,

Microsoft Certified Solution Developer .NET,

Software Engineer

Posted (edited)

About the DropDownList, The only reason I know for it not setting the text is If it can't be found in the ComboBox's Item List.

 

As for you class this is the test I did

 

using System;
using System.Windows.Forms;

namespace MyComboBox
{

public class MyComboBox : ComboBox
{
	private bool isLocked;

	public bool IsLocked 
	{
		get 
		{
			return this.isLocked;
		}
		set 
		{
			this.isLocked = value;
		}
	}

	public MyComboBox()
	{

	}

	protected override void OnKeyPress(KeyPressEventArgs e)
	{
		base.OnKeyPress(e);
		if(this.isLocked == true)
		{
			e.Handled = true; 
		}
	}

}
}

 

This seems to be the same as yours but it works here, even with a button to toggle IsLocked (But as I said before it still lets me change the combobox by selecting an Item from the List). Have you done a messagebox to check the value of textLocked in the OnKeyPress event to make sure it is true, apart from that I don't know what to suggest :confused:

 

To select the Index, Item and text on the combobox I use the following

 

this.MyComboBox.SelectedIndex = this.MyComboBox.FindStringExact(text);

 

To make it so that they can't select an Item in the ComboBox I added the following to the class

 

 


private int iIndex = -1;

protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
if(this.isLocked == true)
{
	this.SelectedIndex = this.iIndex;
}
else 
{
	this.iIndex = this.SelectedIndex;
}
}

Edited by pendragon
Posted

Thanks..

Now I got it..

I forgot calling base method!!!

 

I often forget what I have to do certainly...when coding^^--> very bugging... I fill like Dolry..(watch finding nemo for this name)^^;;

Sun Certified Web component Developer,

Microsoft Certified Solution Developer .NET,

Software Engineer

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...